Skip to content

Commit

Permalink
Add response body to log (#192)
Browse files Browse the repository at this point in the history
* Add response body to log

* Update filedownloader.go

* pribt whole request

* pr change requests

* changed variables because of the linter
  • Loading branch information
trapacska authored Feb 14, 2024
1 parent 494b3cc commit 3ae412a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions filedownloader/filedownloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"net/http/httputil"
"os"

"github.com/bitrise-io/go-utils/log"
Expand Down Expand Up @@ -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)
}

Expand Down
27 changes: 25 additions & 2 deletions filedownloader/filedownloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package filedownloader
import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit 3ae412a

Please sign in to comment.