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

agent/http: use httptest.Server for handler tests #308

Merged
merged 4 commits into from
Aug 21, 2023
Merged

Conversation

nadiamoe
Copy link
Member

Description

Fixes #305

This PR makes minor changes to http proxy handler tests so they are performed with httptest.Server instead of the fake client. This aligns existing tests with the newly added metric tests, and provides more coverage as implementation details of the whole Go HTTP stack is tested, instead of just the handler.

Checklist:

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works.
  • I have run linter locally (make lint) and all checks pass.
  • I have run tests locally (make test) and all tests pass.
  • I have run relevant e2e test locally (make e2e-xxx for agent, disruptors, kubernetes or cluster related changes)
  • Any dependent changes have been merged and published in downstream modules

resp := recorder.Result()
proxyServer := httptest.NewServer(handler)

req, err := http.NewRequest(tc.method, proxyServer.URL+tc.path, bytes.NewReader(tc.body))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading this in the diff, it is not fully clear to me whether tc.body was intended to be the request body in the first place, specially given that all requests are (currently) GET requests. Should I get rid of this and add a new requestBody field to the test table?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC the body was part of the response generated by the fake client:

Body:          io.NopCloser(strings.NewReader(string(f.body)))

Passing it as part of the request was an error in the test code. What I don't understand is your proposal of getting rid of it, as it is should be returned by the upstream server's handler.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand the code correctly, the request we're performing here would be the client request, meaning the first request of the flow. Am I right? If this is the case, I believe the body for that request belongs to the test case (if we want to add it).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've renamed the fields to clarify these are used to control the upstream server response, and fixed the bug where they were being sent by the client 371bb82

Does this align with what you said?

@nadiamoe nadiamoe requested a review from pablochacin August 21, 2023 09:53
handler := &httpHandler{
upstreamURL: *upstreamURL,
client: client,
client: http.DefaultClient,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

client was introduced only for testing. If we are now always using the DefaultClient, maybe we could simply remove this field.

resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("making request to proxy: %v", err)
}

if tc.expectedStatus != resp.StatusCode {
t.Errorf("expected status code '%d' but '%d' received ", tc.expectedStatus, resp.StatusCode)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you are using t.Fatal above, shouldn't we use it uniformly at least in this test?

Copy link
Collaborator

@pablochacin pablochacin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nadiamoe nadiamoe merged commit cd40acc into main Aug 21, 2023
@nadiamoe nadiamoe deleted the httptest-proxy branch August 21, 2023 11:58
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.

Homogenize the http proxy tests
2 participants