Skip to content

Commit 396623c

Browse files
authored
Fixes more unit tests (#59)
* Fixes unit tests * Fixes tests
1 parent 6fad1e5 commit 396623c

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

pkg/download/blob_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ func Test_blobDownload_fails_badCreds(t *testing.T) {
7878
Container: "foocontainer",
7979
})
8080

81+
mockResponse := &http.Response{
82+
StatusCode: http.StatusForbidden,
83+
Status: "The chipmunks do not like you and thus returned a 403",
84+
}
85+
86+
original := MakeHttpRequest
87+
defer func() { MakeHttpRequest = original }()
88+
89+
MakeHttpRequest = func(*http.Request) (*http.Response, error) {
90+
return mockResponse, nil
91+
}
92+
8193
status, _, err := Download(testctx, d)
8294
require.NotNil(t, err)
8395
require.Contains(t, err.Error(), "Please verify the machine has network connectivity")
@@ -94,6 +106,18 @@ func Test_blobDownload_fails_badRequest(t *testing.T) {
94106
Container: "foocontainer",
95107
}}}
96108

109+
mockResponse := &http.Response{
110+
StatusCode: http.StatusBadRequest,
111+
Status: "The chipmunks don't understand you. 400.",
112+
}
113+
114+
original := MakeHttpRequest
115+
defer func() { MakeHttpRequest = original }()
116+
117+
MakeHttpRequest = func(*http.Request) (*http.Response, error) {
118+
return mockResponse, nil
119+
}
120+
97121
status, _, err := Download(testctx, d)
98122
require.NotNil(t, err)
99123
require.Contains(t, err.Error(), "parts of the request were incorrectly formatted, missing, and/or invalid")

pkg/download/downloader.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const (
2727
)
2828

2929
var (
30+
MakeHttpRequest = HttpClientDo
31+
3032
// httpClient is the default client to be used in downloading files from
3133
// Internet. http.Get() uses a client without timeouts (http.DefaultClient)
3234
// so it is dangerous to use it for downloading files from the Internet.
@@ -43,6 +45,10 @@ var (
4345
}}
4446
)
4547

48+
func HttpClientDo(request *http.Request) (*http.Response, error) {
49+
return httpClient.Do(request)
50+
}
51+
4652
// Download retrieves a response body and checks the response status code to see
4753
// if it is 200 OK and then returns the response body. It issues a new request
4854
// every time called. It is caller's responsibility to close the response body.
@@ -56,7 +62,7 @@ func Download(ctx *log.Context, downloader Downloader) (int, io.ReadCloser, erro
5662
ctx.Log("info", fmt.Sprintf("starting download with client request ID %s", requestID))
5763
}
5864

59-
response, err := httpClient.Do(request)
65+
response, err := MakeHttpRequest(request)
6066
if err != nil {
6167
err = urlutil.RemoveUrlFromErr(err)
6268
return -1, nil, errors.Wrapf(err, "http request failed")

0 commit comments

Comments
 (0)