Skip to content

Commit 7dec1ec

Browse files
authored
Set a default HTTP client for DefaultFetcher in DownloadFile method if none is set (#686)
* if no http client is set on the Fetcher's client field,set the default client Signed-off-by: Meredith Lancaster <malancas@github.com> * add test for nil http client scenario Signed-off-by: Meredith Lancaster <malancas@github.com> --------- Signed-off-by: Meredith Lancaster <malancas@github.com>
1 parent 2e05c61 commit 7dec1ec

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

metadata/fetcher/fetcher.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ func (d *DefaultFetcher) DownloadFile(urlPath string, maxLength int64, _ time.Du
7070
req.Header.Set("User-Agent", d.httpUserAgent)
7171
}
7272

73+
// For backwards compatibility, if the client is nil, use the default client.
74+
if d.client == nil {
75+
d.client = http.DefaultClient
76+
}
77+
7378
operation := func() ([]byte, error) {
7479
// Execute the request.
7580
res, err := d.client.Do(req)

metadata/fetcher/fetcher_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,9 @@ func TestDownloadFile_Retry(t *testing.T) {
170170
})
171171
}
172172
}
173+
174+
func TestDownloadFile_NoHTTPClientSet(t *testing.T) {
175+
fetcher := DefaultFetcher{}
176+
_, err := fetcher.DownloadFile("https://jku.github.io/tuf-demo/metadata/1.root.json", 512000, 0)
177+
assert.NoError(t, err)
178+
}

0 commit comments

Comments
 (0)