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

Multiple HTTP assertions against one handler. #193

Open
brackendawson opened this issue Oct 7, 2024 · 0 comments
Open

Multiple HTTP assertions against one handler. #193

brackendawson opened this issue Oct 7, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@brackendawson
Copy link

If someone uses more than one HTTP assertion with the same handler with the same args, eg:

assert.HTTPStatusCode(t, handler, "GET", "/path", nil, 200)
assert.HTTPBodyContains(t, handler, "GET", "/path", nil, "hello")

Then this is not testing for state. A stateful handler could satisfy the tests independently but not in a single call.

A better implementation uses httptest from the standard library:

r := httptest.NewRequest("GET", "/path", nil)
w := httptest.NewRecorder()
handler(w, r)
assert.Equal(t, w.Code, 200)
assert.Contains(t, w.Body.String(), "hello")

This was discussed in stretchr/testify#925.

The HTTP assertions are:

  • HTTPBody
  • HTTPBodyContains
  • HTTPBodyNotContains
  • HTTPError
  • HTTPRedirect
  • HTTPStatusCode
  • HTTPSuccess
@Antonboom Antonboom added the enhancement New feature or request label Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants