@@ -3,6 +3,7 @@ package filedownloader
33import (
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 \n Content-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 \n test-header: test-header-value\r \n \r \n test-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