Skip to content

Commit

Permalink
Merge pull request #2 from ing-bank/feature/add-with-headers
Browse files Browse the repository at this point in the history
Add with headers option to the PrepareRequest method
  • Loading branch information
survivorbat committed Mar 16, 2024
2 parents 5247d27 + bf213f3 commit f73af36
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestProductController_Post_CreatesProducts(t *testing.T) {
gintestutil.WithJsonBody(t, TestObject{Name: "test"}),
gintestutil.WithMethod(http.MethodPost),
gintestutil.WithUrl("https://my-website.com"),
gintestutil.WithHeaders(http.Header{"X-Example": []string{"A", "B"}}),
gintestutil.WithUrlParams(map[string]any{"category": "barbecue"}),
gintestutil.WithQueryParams(map[string]any{"force": "true"}))

Expand Down
10 changes: 10 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type requestConfig struct {
body io.ReadCloser
urlParams map[string]any
queryParams map[string]any
headers http.Header
}

// applyQueryParams turn a map of string/[]string/maps into query parameter names as expected from the user. Check
Expand Down Expand Up @@ -75,6 +76,8 @@ func PrepareRequest(t TestingT, options ...RequestOption) (*gin.Context, *httpte
return context, writer
}

context.Request.Header = config.headers

query := context.Request.URL.Query()
applyQueryParams(config.queryParams, query, "")
context.Request.URL.RawQuery = query.Encode()
Expand Down Expand Up @@ -104,6 +107,13 @@ func WithMethod(method string) RequestOption {
}
}

// WithHeaders specifies the headers of the request
func WithHeaders(headers http.Header) RequestOption {
return func(config *requestConfig) {
config.headers = headers
}
}

// WithUrl specifies the url to use, defaults to https://example.com
func WithUrl(reqUrl string) RequestOption {
return func(config *requestConfig) {
Expand Down
25 changes: 20 additions & 5 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ func TestPrepareRequest_CreatesExpectedContext(t *testing.T) {
tests := map[string]struct {
options []RequestOption

expectedBody string
expectedUrl string
expectedMethod string
expectedParams gin.Params
expectedError error
expectedBody string
expectedUrl string
expectedMethod string
expectedParams gin.Params
expectedHeaders http.Header
expectedError error
}{
"empty request": {
options: []RequestOption{},
Expand Down Expand Up @@ -81,6 +82,18 @@ func TestPrepareRequest_CreatesExpectedContext(t *testing.T) {
expectedMethod: http.MethodGet,
expectedUrl: "https://maarten.dev?one=two&three=four&three=five",
},
"with headers": {
options: []RequestOption{
WithUrl("https://maarten.dev"),
WithHeaders(http.Header{"X-Test-Header": []string{"A", "B"}}),
},

expectedMethod: http.MethodGet,
expectedUrl: "https://maarten.dev",
expectedHeaders: http.Header{
"X-Test-Header": []string{"A", "B"},
},
},
"maarten.dev url": {
options: []RequestOption{WithUrl("https://maarten.dev")},

Expand Down Expand Up @@ -130,6 +143,8 @@ func TestPrepareRequest_CreatesExpectedContext(t *testing.T) {

assert.Equal(t, testData.expectedMethod, context.Request.Method)
assert.Equal(t, testData.expectedUrl, context.Request.URL.String())
assert.Equal(t, testData.expectedHeaders, context.Request.Header)

assert.ElementsMatch(t, testData.expectedParams, context.Params)

if testData.expectedBody != "" {
Expand Down

0 comments on commit f73af36

Please sign in to comment.