Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add response body to log #192

Merged
merged 5 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading