diff --git a/README.md b/README.md index cd8d0b7..39e8c2f 100644 --- a/README.md +++ b/README.md @@ -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"})) diff --git a/context.go b/context.go index 146852a..cfe62f4 100644 --- a/context.go +++ b/context.go @@ -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 @@ -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() @@ -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) { diff --git a/context_test.go b/context_test.go index 5879535..6804b83 100644 --- a/context_test.go +++ b/context_test.go @@ -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{}, @@ -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")}, @@ -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 != "" {