Skip to content

Commit

Permalink
feat: add NewJsonResponseOrPanic function to simplify calls with Resp…
Browse files Browse the repository at this point in the history
…onderFromMultipleResponses
  • Loading branch information
kilianpaquier committed Apr 13, 2024
1 parent c72d04a commit cff7f0c
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,31 @@ func NewJsonResponse(status int, body any) (*http.Response, error) { // nolint:
return response, nil
}

// NewJsonResponseOrPanic is like [NewJsonResponse] but panics in case or error.
//
// It simplifies the call of [ResponderFromMultipleResponses], avoiding the use of a
// temporary variable and an error check, and so can be used in such context:
//
// httpmock.RegisterResponder(
// "GET",
// "/test/path",
// httpmock.ResponderFromMultipleResponses([]*http.Response{
// httpmock.NewJsonResponseOrPanic(200, &MyFirstResponseBody),
// httpmock.NewJsonResponseOrPanic(200, &MySecondResponseBody),
// }),
// )
//
// To pass the content of an existing file as body use [File] as in:
//
// httpmock.NewJsonResponseOrPanic(200, httpmock.File("body.json"))
func NewJsonResponseOrPanic(status int, body any) *http.Response {
response, err := NewJsonResponse(status, body)
if err != nil {
panic(err)
}
return response
}

// NewJsonResponder creates a [Responder] from a given body (as an
// any that is encoded to JSON) and status code.
//
Expand Down

0 comments on commit cff7f0c

Please sign in to comment.