Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions github/dependency_graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func TestDependencyGraphService_FetchSBOM_Download(t *testing.T) {
http.Redirect(w, r, downloadServer.URL, http.StatusFound)
})

sbom, redirectURL, resp, err := client.DependencyGraph.FetchSBOM(t.Context(), "owner", "repo", "1234", http.DefaultClient)
sbom, redirectURL, resp, err := client.DependencyGraph.FetchSBOM(t.Context(), "owner", "repo", "1234", newTestHTTPClient())
if !errors.Is(err, tt.wantErr) {
t.Errorf("DependencyGraph.FetchSBOM error = %#v, want %#v", err, tt.wantErr)
}
Expand Down Expand Up @@ -267,7 +267,7 @@ func TestDependencyGraphService_FetchSBOM_AcceptedError(t *testing.T) {
w.WriteHeader(http.StatusAccepted)
})

sbom, redirectURL, resp, err := client.DependencyGraph.FetchSBOM(t.Context(), "owner", "repo", "1234", http.DefaultClient)
sbom, redirectURL, resp, err := client.DependencyGraph.FetchSBOM(t.Context(), "owner", "repo", "1234", newTestHTTPClient())

if _, ok := errors.AsType[*AcceptedError](err); !ok {
t.Errorf("DependencyGraph.FetchSBOM error = %v, want *AcceptedError", err)
Expand All @@ -290,7 +290,7 @@ func TestDependencyGraphService_FetchSBOM_NoRedirect(t *testing.T) {
w.WriteHeader(http.StatusOK)
})

sbom, redirectURL, resp, err := client.DependencyGraph.FetchSBOM(t.Context(), "owner", "repo", "1234", http.DefaultClient)
sbom, redirectURL, resp, err := client.DependencyGraph.FetchSBOM(t.Context(), "owner", "repo", "1234", newTestHTTPClient())
if err == nil {
t.Error("DependencyGraph.FetchSBOM returned no error for a non-redirect response")
}
Expand All @@ -311,12 +311,12 @@ func TestDependencyGraphService_FetchSBOM_requestErrors(t *testing.T) {

const methodName = "FetchSBOM"
testBadOptions(t, methodName, func() (err error) {
_, _, _, err = client.DependencyGraph.FetchSBOM(ctx, "\n", "\n", "\n", http.DefaultClient)
_, _, _, err = client.DependencyGraph.FetchSBOM(ctx, "\n", "\n", "\n", newTestHTTPClient())
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
sbom, redirectURL, resp, err := client.DependencyGraph.FetchSBOM(ctx, "owner", "repo", "1234", http.DefaultClient)
sbom, redirectURL, resp, err := client.DependencyGraph.FetchSBOM(ctx, "owner", "repo", "1234", newTestHTTPClient())
if sbom != nil || redirectURL != "" {
t.Errorf("testNewRequestAndDoFailure %v returned (%v, %v), want (nil, empty)", methodName, sbom, redirectURL)
}
Expand All @@ -328,7 +328,7 @@ func TestDependencyGraphService_fetchSBOMFromURL_unparsableURL(t *testing.T) {
t.Parallel()
client, _, _ := setup(t)

sbom, err := client.DependencyGraph.fetchSBOMFromURL(t.Context(), http.DefaultClient, "\n")
sbom, err := client.DependencyGraph.fetchSBOMFromURL(t.Context(), newTestHTTPClient(), "\n")
if err == nil {
t.Error("DependencyGraph.fetchSBOMFromURL returned no error for an unparsable URL")
}
Expand Down
17 changes: 17 additions & 0 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,26 @@ type raceSafeTestConn struct {
net.Conn
}

// newTestTransport returns a transport with a connection pool of its own.
// Tests run in parallel, and httptest.Server.Close closes idle connections on
// the shared http.DefaultTransport, which can race with another test's
// in-flight request.
func newTestTransport() *http.Transport {
return http.DefaultTransport.(*http.Transport).Clone()
}

// newTestHTTPClient returns an HTTP client backed by newTestTransport, for
// tests that must supply their own follow-redirects client.
func newTestHTTPClient() *http.Client {
return &http.Client{Transport: newTestTransport()}
}

// mustNewClient is a helper function that creates a new Client and fails the test if there is an error.
func mustNewClient(t *testing.T, opts ...ClientOptionsFunc) *Client {
t.Helper()
// Keep the client off the shared http.DefaultTransport unless the caller
// supplies a transport of its own.
opts = append([]ClientOptionsFunc{WithTransport(newTestTransport())}, opts...)
c, err := NewClient(opts...)
if err != nil {
t.Fatal(err)
Expand Down
9 changes: 5 additions & 4 deletions github/repos_releases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ func TestRepositoriesService_DownloadReleaseAsset_FollowRedirect(t *testing.T) {
})

ctx := t.Context()
reader, _, err := client.Repositories.DownloadReleaseAsset(ctx, "o", "r", 1, http.DefaultClient)
reader, _, err := client.Repositories.DownloadReleaseAsset(ctx, "o", "r", 1, newTestHTTPClient())
if err != nil {
t.Errorf("Repositories.DownloadReleaseAsset returned error: %v", err)
}
Expand Down Expand Up @@ -494,7 +494,7 @@ func TestRepositoriesService_DownloadReleaseAsset_FollowMultipleRedirects(t *tes
})

ctx := t.Context()
reader, _, err := client.Repositories.DownloadReleaseAsset(ctx, "o", "r", 1, http.DefaultClient)
reader, _, err := client.Repositories.DownloadReleaseAsset(ctx, "o", "r", 1, newTestHTTPClient())
if err != nil {
t.Errorf("Repositories.DownloadReleaseAsset returned error: %v", err)
}
Expand Down Expand Up @@ -526,7 +526,7 @@ func TestRepositoriesService_DownloadReleaseAsset_FollowRedirectToError(t *testi
})

ctx := t.Context()
resp, loc, err := client.Repositories.DownloadReleaseAsset(ctx, "o", "r", 1, http.DefaultClient)
resp, loc, err := client.Repositories.DownloadReleaseAsset(ctx, "o", "r", 1, newTestHTTPClient())
if err == nil {
t.Error("Repositories.DownloadReleaseAsset did not return an error")
}
Expand Down Expand Up @@ -562,9 +562,10 @@ func TestRepositoriesService_DownloadReleaseAsset_FollowRedirectToErrorClosesOri
})

var closed bool
testTransport := newTestTransport()
followRedirectsClient := &http.Client{
Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
resp, err := http.DefaultTransport.RoundTrip(req)
resp, err := testTransport.RoundTrip(req)
if resp != nil {
resp.Body = &closeRecorder{ReadCloser: resp.Body, closed: &closed}
}
Expand Down
6 changes: 5 additions & 1 deletion scrape/scrape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ func setup(t *testing.T) (client *Client, mux *http.ServeMux) {
mux = http.NewServeMux()
server := httptest.NewServer(mux)

client = NewClient(nil)
// Use a transport dedicated to this test rather than the shared
// http.DefaultTransport. httptest.Server.Close closes idle connections on
// http.DefaultTransport, which would otherwise race with the requests of
// other tests running in parallel.
client = NewClient(http.DefaultTransport.(*http.Transport).Clone())
client.baseURL, _ = url.Parse(server.URL + "/")

t.Cleanup(server.Close)
Expand Down
Loading