diff --git a/filedownloader/filedownloader.go b/filedownloader/filedownloader.go index d2919dd..731ddfc 100644 --- a/filedownloader/filedownloader.go +++ b/filedownloader/filedownloader.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + "net/http/httputil" "os" "github.com/bitrise-io/go-utils/log" @@ -108,6 +109,10 @@ func download(context context.Context, client HTTPClient, source string, destina }() if resp.StatusCode != http.StatusOK { + responseBytes, err := httputil.DumpResponse(resp, true) + if err == nil { + return fmt.Errorf("unable to download file from: %s. Status code: %d. Response: %s", source, resp.StatusCode, string(responseBytes)) + } return fmt.Errorf("unable to download file from: %s. Status code: %d", source, resp.StatusCode) } diff --git a/filedownloader/filedownloader_test.go b/filedownloader/filedownloader_test.go index 00cf511..a1e7c9a 100644 --- a/filedownloader/filedownloader_test.go +++ b/filedownloader/filedownloader_test.go @@ -3,6 +3,7 @@ package filedownloader import ( "errors" "fmt" + "io" "io/ioutil" "net/http" "net/http/httptest" @@ -35,12 +36,12 @@ func Test_get_Success(t *testing.T) { assertFileContent(t, path, "filecontent1") } -func Test_get_InvalidStatusCode(t *testing.T) { +func Test_get_InvalidStatusCode_NoBody(t *testing.T) { // Given path := givenTempPath(t) url := "http://url.com" statusCode := 404 - expectedErr := fmt.Errorf("unable to download file from: %s. Status code: %d", url, statusCode) + expectedErrString := fmt.Sprintf("unable to download file from: %s. Status code: %d. Response: HTTP/0.0 404 Not Found\r\nContent-Length: 0\r\n\r\n", url, statusCode) mockedHTTPClient := givenHTTPClient( http.Response{ StatusCode: statusCode, @@ -50,6 +51,28 @@ func Test_get_InvalidStatusCode(t *testing.T) { // When err := downloader.Get(path, url) + // Then + require.Equal(t, errors.New(expectedErrString).Error(), err.Error()) + assertFileNotExists(t, path) +} + +func Test_get_InvalidStatusCode_WithBody(t *testing.T) { + // Given + path := givenTempPath(t) + url := "http://url.com" + statusCode := 404 + expectedErr := fmt.Errorf("unable to download file from: %s. Status code: %d. Response: HTTP/0.0 404 Not Found\r\ntest-header: test-header-value\r\n\r\ntest-body", url, statusCode) + mockedHTTPClient := givenHTTPClient( + http.Response{ + StatusCode: statusCode, + Header: http.Header{"test-header": []string{"test-header-value"}}, + Body: io.NopCloser(strings.NewReader("test-body")), + }) + downloader := givenFileDownloader(mockedHTTPClient) + + // When + err := downloader.Get(path, url) + // Then require.Equal(t, expectedErr, err) assertFileNotExists(t, path)