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

convert s3 handle_200_error_test into an actual unit test #2174

Merged
merged 1 commit into from
Jul 7, 2023
Merged
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
92 changes: 56 additions & 36 deletions service/s3/internal/customizations/handle_200_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package customizations_test

import (
"context"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"

Expand All @@ -19,65 +20,84 @@ 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(`<Error><Type>Sender</Type>
<Code>InvalidGreeting</Code>
<Message>Hi</Message>
<AnotherSetting>setting</AnotherSetting>
<RequestId>foo-id</RequestId></Error>`),
statusCode: 200,
response: &http.Response{
StatusCode: 200,
Body: asReadCloser(
`<Error>
<Type>Sender</Type>
<Code>InvalidGreeting</Code>
<Message>Hi</Message>
<AnotherSetting>setting</AnotherSetting>
<RequestId>foo-id</RequestId>
</Error>`,
),
},
expectedError: "InvalidGreeting",
},
"200NoResponse": {
response: []byte{},
statusCode: 200,
response: &http.Response{
StatusCode: 200,
Body: asReadCloser(""),
},
expectedError: "received empty response payload",
},
"200InvalidResponse": {
response: []byte(`<Error><Type>Sender</Type>
<Code>InvalidGreeting</Code>
<Message>Hi</Message>
<AnotherSetting>setting</AnotherSetting>
<RequestId>foo-id`),
statusCode: 200,
response: &http.Response{
StatusCode: 200,
Body: asReadCloser(
`<Error>
<Type>Sender</Type>
<Code>InvalidGreeting</Code>
<Message>Hi</Message>
<AnotherSetting>setting</AnotherSetting>
<RequestId>foo-id`,
),
},
expectedError: "unexpected EOF",
},
"200SuccessResponse": {
response: []byte(`<CompleteMultipartUploadResult>
<Bucket>bucket</Bucket>
</CompleteMultipartUploadResult>`),
statusCode: 200,
response: &http.Response{
StatusCode: 200,
Body: asReadCloser(
`<CompleteMultipartUploadResult>
<Bucket>bucket</Bucket>
</CompleteMultipartUploadResult>`,
),
},
expectedError: "",
expectedBucket: "bucket",
},
}

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)
Expand Down