Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce request builder with headers #218

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ type WapiRequestBuilder struct {
authCfg AuthConfig
}

type WapiRequestBuilderWithHeaders struct {
HttpRequestBuilder
Header http.Header
}

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

type WapiHttpRequestor struct {
client http.Client
}
Expand Down
19 changes: 19 additions & 0 deletions connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,30 @@ import (
"net/http"
"net/url"
"strings"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestHeaderRequestBuilder_BuildRequest(t *testing.T) {
header := make(http.Header)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add as Describe function similar to other Describe functions?

header.Add("x", "1")
header.Add("y", "2")
requestBuilder := &WapiRequestBuilderWithHeaders{
HttpRequestBuilder: &WapiRequestBuilder{},
Header: header,
}
var obj IBObject
req, _ := requestBuilder.BuildRequest(GET, obj, "ref", nil)

for k := range header {
if req.Header.Get(k) != header.Get(k) {
t.Errorf("Expected header %s to be %s, got %s", k, header.Get(k), req.Header.Get(k))
}
}
}

type FakeRequestBuilder struct {
hostCfg HostConfig
authCfg AuthConfig
Expand Down