From 47afa361713781b0fafeae113a9e82df79e6b830 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Va=C5=A1ko?= Date: Wed, 6 Mar 2024 10:45:06 +0100 Subject: [PATCH] fix(http): Make option optional. When no option provided there is nothing to test. Throw a Fail when no options are selected to prevent missuse of new function. --- assert/http_assertions.go | 7 ++++++- assert/http_options.go | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/assert/http_assertions.go b/assert/http_assertions.go index 9e0708cbd..9da3dd2f8 100644 --- a/assert/http_assertions.go +++ b/assert/http_assertions.go @@ -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) @@ -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 } diff --git a/assert/http_options.go b/assert/http_options.go index 422bc5409..f69984ccd 100644 --- a/assert/http_options.go +++ b/assert/http_options.go @@ -8,7 +8,7 @@ import ( ) type builder struct { - code int + code *int body io.ReadCloser expectedBody bytes.Buffer requestHeader http.Header @@ -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 } }