Skip to content

Commit

Permalink
feat(tdhttp): add TestAPI.DefaultHeader & TestAPI.DefaultHook methods
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
  • Loading branch information
maxatome committed Mar 25, 2024
1 parent f641479 commit 856e80a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
43 changes: 40 additions & 3 deletions helpers/tdhttp/test_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ func (ta *TestAPI) AutoDumpResponse(enable ...bool) *TestAPI {
}

// DefaultRequestParams allows to define header values, query params,
// cookies and hooks to automatically set in each future requests sent
// by [TestAPI.Request], [TestAPI.Get], [TestAPI.Head],
// cookies and hooks to be automatically set for each future requests
// sent by [TestAPI.Request], [TestAPI.Get], [TestAPI.Head],
// [TestAPI.Options], [TestAPI.Post], [TestAPI.PostForm],
// [TestAPI.PostMultipartFormData], [TestAPI.Put], [TestAPI.Path],
// [TestAPI.Delete] and all derived JSON and XML methods.
Expand All @@ -190,6 +190,8 @@ func (ta *TestAPI) AutoDumpResponse(enable ...bool) *TestAPI {
//
// See [TestAPI.AddDefaultRequestParams] to add new default params
// instead of entirely replacing them.
//
// See also [TestAPI.DefaultHook] and [TestAPI.DefaultHeader].
func (ta *TestAPI) DefaultRequestParams(newRequestParams ...any) *TestAPI {
ta.t.Helper()
header, qp, cookies, hook, err := collateRequestParams(newRequestParams)
Expand All @@ -203,8 +205,41 @@ func (ta *TestAPI) DefaultRequestParams(newRequestParams ...any) *TestAPI {
return ta
}

// DefaultHook allows to define a hook to be automatically called for
// each future requests sent by [TestAPI.Request], [TestAPI.Get],
// [TestAPI.Head], [TestAPI.Options], [TestAPI.Post],
// [TestAPI.PostForm], [TestAPI.PostMultipartFormData], [TestAPI.Put],
// [TestAPI.Path], [TestAPI.Delete] and all derived JSON and XML
// methods.
//
// Previously defined hooks are discarded.
//
// See also [TestAPI.DefaultRequestParams] and
// [TestAPI.AddDefaultRequestParams].
func (ta *TestAPI) DefaultHook(hook func(*http.Request) error) *TestAPI {
ta.defaultHook = hook
return ta
}

// DefaultHeader allows to define header values to be automatically
// set for each future requests sent by [TestAPI.Request],
// [TestAPI.Get], [TestAPI.Head], [TestAPI.Options], [TestAPI.Post],
// [TestAPI.PostForm], [TestAPI.PostMultipartFormData], [TestAPI.Put],
// [TestAPI.Path], [TestAPI.Delete] and all derived JSON and XML
// methods.
//
// Previously defined header values are discarded.
//
// See also [TestAPI.DefaultRequestParams] and
// [TestAPI.AddDefaultRequestParams].
func (ta *TestAPI) DefaultHeader(h http.Header) *TestAPI {
header, _, _, _, _ := collateRequestParams([]any{h})
ta.defaultHeader = header
return ta
}

// AddDefaultRequestParams allows to define header values, query
// params, cookies and hooks to automatically set in each future
// params, cookies and hooks to be automatically set for each future
// requests sent by [TestAPI.Request], [TestAPI.Get], [TestAPI.Head],
// [TestAPI.Options], [TestAPI.Post], [TestAPI.PostForm],
// [TestAPI.PostMultipartFormData], [TestAPI.Put], [TestAPI.Path],
Expand All @@ -218,6 +253,8 @@ func (ta *TestAPI) DefaultRequestParams(newRequestParams ...any) *TestAPI {
//
// See [TestAPI.DefaultRequestParams] to entirely replace default
// params instead of modifying them.
//
// See also [TestAPI.DefaultHook] and [TestAPI.DefaultHeader].
func (ta *TestAPI) AddDefaultRequestParams(newRequestParams ...any) *TestAPI {
ta.t.Helper()
header, qp, cookies, hook, err := collateRequestParams(newRequestParams)
Expand Down
36 changes: 36 additions & 0 deletions helpers/tdhttp/test_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,42 @@ bingo%CR
}
}`))

// Replace previously defined hooks
ta.DefaultHook(func(r *http.Request) error {
r.Header.Add("X-Hook", "4")
return nil
})
ta.Get("/hq/json",
func(r *http.Request) error {
r.Header.Add("X-Hook", "3")
return nil
}).
CmpJSONBody(td.JSON(`{
"header": SuperMapOf({
"X-Hook": ["3", "4"],
"X-Zip": ["test"],
"Cookie": ["cook9=val9"]
}),
"query_params": {
"x": ["y"]
}
}`))

// Replace previously defined header values
ta.DefaultHeader(http.Header{"x-zip": {"new"}, "x-test": {"yo"}})
ta.Get("/hq/json").
CmpJSONBody(td.JSON(`{
"header": SuperMapOf({
"X-Hook": ["4"],
"X-Zip": ["new"],
"X-Test": ["yo"],
"Cookie": ["cook9=val9"]
}),
"query_params": {
"x": ["y"]
}
}`))

t.Run("hook failures", func(t *testing.T) {
tt := tdutil.NewT("test")
ta := tdhttp.NewTestAPI(tt, mux)
Expand Down

0 comments on commit 856e80a

Please sign in to comment.