Replace client.Get with http.NewRequestWithContext in tests#46
Open
JamieMagee wants to merge 1 commit intomainfrom
Open
Replace client.Get with http.NewRequestWithContext in tests#46JamieMagee wants to merge 1 commit intomainfrom
JamieMagee wants to merge 1 commit intomainfrom
Conversation
client.Get doesn't take a context, so the noctx linter flags it. Switch the three call sites in proxy_test.go to build an explicit request with context.Background() and use client.Do instead.
There was a problem hiding this comment.
Pull request overview
Updates proxy integration tests to ensure outbound HTTP requests are created with an explicit context (to satisfy noctx linting) by replacing client.Get(url) with http.NewRequestWithContext(...)+client.Do(req).
Changes:
- Replace
client.Get(url)withhttp.NewRequestWithContext(context.Background(), ...)andclient.Do(req)inTestProxyHTTPRequest. - Apply the same request construction pattern in both HTTP and HTTPS subtests within
TestIPRestrictions.
Comments suppressed due to low confidence (1)
proxy_test.go:50
- If
client.Do(req)returns an error, the test logs it but continues and then dereferencesrsp.Body, which will panic whenrspis nil. Fail fast (t.Fatalf/require.NoError) or return before usingrsp.
rsp, err := client.Do(req)
if err != nil {
t.Errorf("making proxied request: %v", err)
}
defer rsp.Body.Close()
assert.Equal(t, 200, rsp.StatusCode)
Comment on lines
+42
to
47
| t.Errorf("initializing new request: %v", err) | ||
| } | ||
|
|
||
| rsp, err := client.Do(req) | ||
| if err != nil { | ||
| t.Errorf("making proxied request: %v", err) |
There was a problem hiding this comment.
If NewRequestWithContext fails, the test currently logs the error but continues, leaving req nil and causing client.Do(req) to panic. Use a fatal assertion (t.Fatalf/require.NoError) or return immediately after reporting the error.
This issue also appears on line 45 of the same file.
Suggested change
| t.Errorf("initializing new request: %v", err) | |
| } | |
| rsp, err := client.Do(req) | |
| if err != nil { | |
| t.Errorf("making proxied request: %v", err) | |
| t.Fatalf("initializing new request: %v", err) | |
| } | |
| rsp, err := client.Do(req) | |
| if err != nil { | |
| t.Fatalf("making proxied request: %v", err) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three places in
proxy_test.gocallclient.Get(url), which doesn't accept a context. Thenoctxlinter catches this because any outgoing HTTP request should carry a context for cancellation and tracing.Each call site now builds an
http.Requestwithhttp.NewRequestWithContext(context.Background(), ...)and usesclient.Do(req)instead.