From 654ea6b769f3920cc18e0dc4382eddaae9306a39 Mon Sep 17 00:00:00 2001 From: Luc Talatinian Date: Fri, 7 Jul 2023 12:59:42 -0400 Subject: [PATCH] convert s3 handle_200_error_test into an actual unit test with a fake HTTP client, instead of talking to an actual loopback server which was causing issues with dns resolution in ep2 --- .../customizations/handle_200_error_test.go | 92 +++++++++++-------- 1 file changed, 56 insertions(+), 36 deletions(-) diff --git a/service/s3/internal/customizations/handle_200_error_test.go b/service/s3/internal/customizations/handle_200_error_test.go index 6dbb3262042..57c062f8388 100644 --- a/service/s3/internal/customizations/handle_200_error_test.go +++ b/service/s3/internal/customizations/handle_200_error_test.go @@ -2,8 +2,9 @@ package customizations_test import ( "context" + "io" + "io/ioutil" "net/http" - "net/http/httptest" "strings" "testing" @@ -19,41 +20,71 @@ func (fn EndpointResolverFunc) ResolveEndpoint(region string, options s3.Endpoin return fn(region, options) } +type mockHTTPClient struct { + r *http.Response +} + +func (m *mockHTTPClient) Do(*http.Request) (*http.Response, error) { + return m.r, nil +} + +var _ s3.HTTPClient = &mockHTTPClient{} + +func asReadCloser(s string) io.ReadCloser { + return ioutil.NopCloser(strings.NewReader(s)) +} + func TestErrorResponseWith200StatusCode(t *testing.T) { cases := map[string]struct { - response []byte - statusCode int + response *http.Response expectedError string expectedBucket string }{ "200ErrorBody": { - response: []byte(`Sender - InvalidGreeting - Hi - setting - foo-id`), - statusCode: 200, + response: &http.Response{ + StatusCode: 200, + Body: asReadCloser( + ` + Sender + InvalidGreeting + Hi + setting + foo-id + `, + ), + }, expectedError: "InvalidGreeting", }, "200NoResponse": { - response: []byte{}, - statusCode: 200, + response: &http.Response{ + StatusCode: 200, + Body: asReadCloser(""), + }, expectedError: "received empty response payload", }, "200InvalidResponse": { - response: []byte(`Sender - InvalidGreeting - Hi - setting - foo-id`), - statusCode: 200, + response: &http.Response{ + StatusCode: 200, + Body: asReadCloser( + ` + Sender + InvalidGreeting + Hi + setting + foo-id`, + ), + }, expectedError: "unexpected EOF", }, "200SuccessResponse": { - response: []byte(` - bucket - `), - statusCode: 200, + response: &http.Response{ + StatusCode: 200, + Body: asReadCloser( + ` + bucket + `, + ), + }, expectedError: "", expectedBucket: "bucket", }, @@ -61,23 +92,12 @@ func TestErrorResponseWith200StatusCode(t *testing.T) { for name, c := range cases { t.Run(name, func(t *testing.T) { - server := httptest.NewServer(http.HandlerFunc( - func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(c.statusCode) - w.Write(c.response) - })) - defer server.Close() - options := s3.Options{ - Credentials: unit.StubCredentialsProvider{}, - Retryer: aws.NopRetryer{}, - Region: "mock-region", - EndpointResolver: EndpointResolverFunc(func(region string, options s3.EndpointResolverOptions) (e aws.Endpoint, err error) { - e.URL = server.URL - e.SigningRegion = "us-west-2" - return e, err - }), + Credentials: unit.StubCredentialsProvider{}, + Retryer: aws.NopRetryer{}, + Region: "mock-region", UsePathStyle: true, + HTTPClient: &mockHTTPClient{c.response}, } svc := s3.New(options)