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

Refactor test suite to make real requests to a real server #131

Merged
merged 9 commits into from
Jun 28, 2023

Conversation

mccutchen
Copy link
Owner

@mccutchen mccutchen commented Jun 27, 2023

In the course of validating #125, we discovered that using the stdlib's httptest.ResponseRecorder mechanism to drive the vast majority of our unit tests led to some slight brokenness due to subtle differences in the way those "simulated" requests are handled vs "real" requests to a live HTTP server, as explained in this comment.

That prompted me to do a big ass refactor of the entire test suite, swapping httptest.ResponseRecorder for interacting with a live server instance via httptest.Server.

This should make the test suite more accurate and reliable in the long run by ensuring that the vast majority of tests are making actual HTTP requests and reading responses from the wire.

Note that updating these tests also uncovered a few minor bugs in existing handler code, fixed in a separate commit for visibility.

P.S. I'm awfully sorry to anyone who tries to merge or rebase local test changes after this refactor lands, that is goign to be a nightmare. If you run into issues resolving conflicts, feel free to ping me and I can try to help!

@mccutchen mccutchen changed the title Giant test refactor Refactor test suite to make real HTTP requests to a real HTTP server Jun 27, 2023
@mccutchen mccutchen changed the title Refactor test suite to make real HTTP requests to a real HTTP server Refactor test suite to make real requests to a real server Jun 27, 2023
@codecov
Copy link

codecov bot commented Jun 27, 2023

Codecov Report

Merging #131 (9d33189) into main (499044e) will increase coverage by 0.21%.
The diff coverage is 100.00%.

❗ Current head 9d33189 differs from pull request most recent head 93d1b1b. Consider uploading reports for the commit 93d1b1b to get more accurate results

@@            Coverage Diff             @@
##             main     #131      +/-   ##
==========================================
+ Coverage   97.84%   98.06%   +0.21%     
==========================================
  Files           8        8              
  Lines        1482     1495      +13     
==========================================
+ Hits         1450     1466      +16     
+ Misses         23       21       -2     
+ Partials        9        8       -1     
Impacted Files Coverage Δ
httpbin/helpers.go 95.50% <ø> (+1.10%) ⬆️
httpbin/handlers.go 99.44% <100.00%> (+0.01%) ⬆️

@mccutchen mccutchen merged commit 0de8ec9 into main Jun 28, 2023
@mccutchen mccutchen deleted the giant-test-refactor branch June 28, 2023 05:24
mccutchen added a commit that referenced this pull request Jun 29, 2023
A set of test suite improvements following up on #131:
- Make sure we always fully consume and close response bodies, to allow
the shared HTTP client to reuse connections to the local test server
whenever possible
- Make order of arguments to equality assertions consistent, so failed
test output makes sense
mccutchen added a commit that referenced this pull request Jul 1, 2023
Standardize on structured JSON error responses everywhere we can, with
only one exception where the error is a warning for humans to read.

Fixes #108 by adding a check to every request in the test suite to
ensure that errors are never served with a Content-Type that might
enable XSS or other vulnerabilities.

While we're at it, continue refining the test suite and further adopting
some of the testing helpers added in #131.
sonbui00 added a commit to sonbui00/go-httpbin that referenced this pull request Aug 7, 2023
Signed-off-by: Son Bui <sonbv00@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant