Skip to content

Commit 3ae412a

Browse files
authored
Add response body to log (#192)
* Add response body to log * Update filedownloader.go * pribt whole request * pr change requests * changed variables because of the linter
1 parent 494b3cc commit 3ae412a

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

filedownloader/filedownloader.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"net/http"
9+
"net/http/httputil"
910
"os"
1011

1112
"github.com/bitrise-io/go-utils/log"
@@ -108,6 +109,10 @@ func download(context context.Context, client HTTPClient, source string, destina
108109
}()
109110

110111
if resp.StatusCode != http.StatusOK {
112+
responseBytes, err := httputil.DumpResponse(resp, true)
113+
if err == nil {
114+
return fmt.Errorf("unable to download file from: %s. Status code: %d. Response: %s", source, resp.StatusCode, string(responseBytes))
115+
}
111116
return fmt.Errorf("unable to download file from: %s. Status code: %d", source, resp.StatusCode)
112117
}
113118

filedownloader/filedownloader_test.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package filedownloader
33
import (
44
"errors"
55
"fmt"
6+
"io"
67
"io/ioutil"
78
"net/http"
89
"net/http/httptest"
@@ -35,12 +36,12 @@ func Test_get_Success(t *testing.T) {
3536
assertFileContent(t, path, "filecontent1")
3637
}
3738

38-
func Test_get_InvalidStatusCode(t *testing.T) {
39+
func Test_get_InvalidStatusCode_NoBody(t *testing.T) {
3940
// Given
4041
path := givenTempPath(t)
4142
url := "http://url.com"
4243
statusCode := 404
43-
expectedErr := fmt.Errorf("unable to download file from: %s. Status code: %d", url, statusCode)
44+
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)
4445
mockedHTTPClient := givenHTTPClient(
4546
http.Response{
4647
StatusCode: statusCode,
@@ -50,6 +51,28 @@ func Test_get_InvalidStatusCode(t *testing.T) {
5051
// When
5152
err := downloader.Get(path, url)
5253

54+
// Then
55+
require.Equal(t, errors.New(expectedErrString).Error(), err.Error())
56+
assertFileNotExists(t, path)
57+
}
58+
59+
func Test_get_InvalidStatusCode_WithBody(t *testing.T) {
60+
// Given
61+
path := givenTempPath(t)
62+
url := "http://url.com"
63+
statusCode := 404
64+
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)
65+
mockedHTTPClient := givenHTTPClient(
66+
http.Response{
67+
StatusCode: statusCode,
68+
Header: http.Header{"test-header": []string{"test-header-value"}},
69+
Body: io.NopCloser(strings.NewReader("test-body")),
70+
})
71+
downloader := givenFileDownloader(mockedHTTPClient)
72+
73+
// When
74+
err := downloader.Get(path, url)
75+
5376
// Then
5477
require.Equal(t, expectedErr, err)
5578
assertFileNotExists(t, path)

0 commit comments

Comments
 (0)