Skip to content

Commit

Permalink
Introduce request builder with headers (#218)
Browse files Browse the repository at this point in the history
* introduce new request builder with headers

* add tests in existing way and add initializer for request builder with headers

* use existing request builder to create request builder with headers
  • Loading branch information
smalisetti-infoblox authored Jan 11, 2024
1 parent c6208bb commit 4632604
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
25 changes: 25 additions & 0 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,31 @@ type WapiRequestBuilder struct {
authCfg AuthConfig
}

type WapiRequestBuilderWithHeaders struct {
HttpRequestBuilder
header http.Header
}

func NewWapiRequestBuilderWithHeaders(wrb *WapiRequestBuilder, header http.Header) (*WapiRequestBuilderWithHeaders, error) {
return &WapiRequestBuilderWithHeaders{
HttpRequestBuilder: wrb,
header: header,
}, nil
}

func (wrbh *WapiRequestBuilderWithHeaders) BuildRequest(r RequestType, obj IBObject, ref string, queryParams *QueryParams) (req *http.Request, err error) {
req, err = wrbh.HttpRequestBuilder.BuildRequest(r, obj, ref, queryParams)
if err != nil {
return req, err
}
for h, values := range wrbh.header {
for _, v := range values {
req.Header.Add(h, v)
}
}
return req, nil
}

type WapiHttpRequestor struct {
client http.Client
}
Expand Down
37 changes: 37 additions & 0 deletions connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,43 @@ var _ = Describe("Connector", func() {
})
})

Describe("WapiRequestBuilderWithHeaders", func() {
host := "172.22.18.66"
version := "2.2"
port := "443"
username := "myname"
password := "mysecrete!"
hostCfg := HostConfig{
Host: host,
Version: version,
Port: port,
}
authCfg := AuthConfig{
Username: username,
Password: password,
}

header := make(http.Header)
header.Add("x", "1")
header.Add("y", "2")

wrb, _ := NewWapiRequestBuilder(hostCfg, authCfg)
wrbh, err := NewWapiRequestBuilderWithHeaders(wrb, header)
if err != nil {
panic("NewWapiRequestBuilderWithHeaders() is not expected to return an error")
}

Describe("BuildRequest", func() {
It("should set given headers to request", func() {
var obj IBObject
req, _ := wrbh.BuildRequest(GET, obj, "ref", nil)
for k := range header {
Expect(header.Get(k)).To(Equal(req.Header.Get(k)))
}
})
})
})

Describe("Connector Object Methods", func() {

host := "172.22.18.66"
Expand Down

0 comments on commit 4632604

Please sign in to comment.