Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert http testing functions to builder pattern #426

Closed
twifkak opened this issue May 20, 2020 · 0 comments
Closed

Convert http testing functions to builder pattern #426

twifkak opened this issue May 20, 2020 · 0 comments
Assignees
Labels

Comments

@twifkak
Copy link
Member

twifkak commented May 20, 2020

There are a bunch of cryptically named functions to issue fake HTTP requests to a handler and record their responses:

func Get(t *testing.T, handler http.Handler, target string) *http.Response {
return GetH(t, handler, target, http.Header{})
}
func GetH(t *testing.T, handler http.Handler, target string, headers http.Header) *http.Response {
return GetBHH(t, handler, target, "", nil, headers)
}
func GetHH(t *testing.T, handler http.Handler, target string, host string, headers http.Header) *http.Response {
return GetBHH(t, handler, target, host, nil, headers)
}
func GetBHH(t *testing.T, handler http.Handler, target string, host string, body io.Reader, headers http.Header) *http.Response {
rec := httptest.NewRecorder()
method := ""
if body != nil {
method = "POST"
headers.Set("Content-Type", "application/x-www-form-urlencoded")
}
req := httptest.NewRequest(method, target, body)
for name, values := range headers {
for _, value := range values {
req.Header.Add(name, value)
}
}
if host != "" {
req.Host = host
}
handler.ServeHTTP(rec, req)
return rec.Result()
}

Instead, they should follow a builder pattern, something like Request(t, handler, "/blah").Headers({...}).Body("foo").Get(). That way the names are not unreadable, and we don't have a combinatorial explosion of the different variants of Get().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants