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: Use httptest for HTTP layer testing #1092

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jcscottiii
Copy link
Collaborator

@jcscottiii jcscottiii commented Jan 24, 2025

This PR improves the reliability and maintainability of our HTTP tests by transitioning from direct calls to the generated OpenAPI server methods to using the httptest package for true HTTP-level testing.

Motivation:

Currently, our tests bypass the HTTP layer, which limits their ability to identify issues with request handling, middleware, and serialization. This refactor provides a more accurate representation of real-world scenarios and enables better testing of components like the upcoming auth middleware.

Changes:

  • httptest Integration: All HTTP handler tests now utilize httptest to simulate actual HTTP requests and responses. This involves updating test inputs to *http.Request and expected outputs to *http.Response.
  • Centralized Assertions: Common assertion logic has been extracted into helper functions like assertTestServerRequest and assertMockCallCount to reduce code duplication and improve consistency across tests.
  • OpenAPI Updates: Added TODO notes to deprecate outdated parameters in the OpenAPI document to maintain API consistency with other parameters.

Benefits:

  • Increased Test Accuracy: Tests now accurately reflect the HTTP request lifecycle.
  • Improved Code Maintainability: Centralized assertions reduce redundancy and simplify test updates.
  • Enhanced Testability: Enables easier testing of middleware and other HTTP-related components.

Future Work:

  • This refactor facilitates the implementation of the auth middleware in a subsequent PR. Needed for the saved searches work.

Right now, our http unit tests aren't really http tests. They were great
go get started with it is not the long term solution. Also, I am looking
at adding the auth middleware, I will need to change some things around.
It would be great to actually assert the http layer before those changes.
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