Skip to content

Commit

Permalink
fix(http): Make option optional.
Browse files Browse the repository at this point in the history
When no option provided there is nothing to test. Throw a Fail when no
options are selected to prevent missuse of new function.
  • Loading branch information
Martin Vaško committed Mar 6, 2024
1 parent 1837bd6 commit 47afa36
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion assert/http_assertions.go
Expand Up @@ -175,6 +175,11 @@ func HTTP(t TestingT, handler http.HandlerFunc, method, url string, values url.V
h.Helper()
}

if options == nil {
Fail(t, fmt.Sprintf("No options selected, no assertions can be executed"))
return false
}

b := &builder{}
for _, option := range options {
err := option(b)
Expand All @@ -194,7 +199,7 @@ func HTTP(t TestingT, handler http.HandlerFunc, method, url string, values url.V
req.Header = b.requestHeader
req.URL.RawQuery = values.Encode()
handler(w, req)
if w.Code != b.code {
if b.code != nil && w.Code != *b.code {
Fail(t, fmt.Sprintf("Expected HTTP success status code for %q but received %d", url+"?"+values.Encode(), w.Code))
return false
}
Expand Down
4 changes: 2 additions & 2 deletions assert/http_options.go
Expand Up @@ -8,7 +8,7 @@ import (
)

type builder struct {
code int
code *int
body io.ReadCloser
expectedBody bytes.Buffer
requestHeader http.Header
Expand All @@ -24,7 +24,7 @@ func WithCode(code int) HttpOption {
return errors.New("Given HTTP code is outside range of possible values assignement")
}

b.code = code
b.code = &code
return nil
}
}
Expand Down

0 comments on commit 47afa36

Please sign in to comment.