File: handle_200_error_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go-v2 1.24.1-2~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 554,032 kB
  • sloc: java: 15,941; makefile: 419; sh: 175
file content (128 lines) | stat: -rw-r--r-- 3,082 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package customizations_test

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

	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/internal/awstesting/unit"

	"github.com/aws/aws-sdk-go-v2/service/s3"
)

type EndpointResolverFunc func(region string, options s3.EndpointResolverOptions) (aws.Endpoint, error)

func (fn EndpointResolverFunc) ResolveEndpoint(region string, options s3.EndpointResolverOptions) (endpoint aws.Endpoint, err error) {
	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       *http.Response
		expectedError  string
		expectedBucket string
	}{
		"200ErrorBody": {
			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: &http.Response{
				StatusCode: 200,
				Body:       asReadCloser(""),
			},
			expectedError: "received empty response payload",
		},
		"200InvalidResponse": {
			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: &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) {
			options := s3.Options{
				Credentials:  unit.StubCredentialsProvider{},
				Retryer:      aws.NopRetryer{},
				Region:       "mock-region",
				UsePathStyle: true,
				HTTPClient:   &mockHTTPClient{c.response},
			}

			svc := s3.New(options)
			resp, err := svc.CompleteMultipartUpload(context.Background(), &s3.CompleteMultipartUploadInput{
				UploadId:     aws.String("mockID"),
				RequestPayer: "requester",
				Bucket:       aws.String("bucket"),
				Key:          aws.String("mockKey"),
			})

			if len(c.expectedError) != 0 {
				if err == nil {
					t.Fatalf("expected error, got none")
				}

				if e, a := c.expectedError, err.Error(); !strings.Contains(a, e) {
					t.Fatalf("expected %v, got %v", e, a)
				}
			}

			if len(c.expectedBucket) != 0 {
				if e, a := c.expectedBucket, *resp.Bucket; !strings.EqualFold(e, a) {
					t.Fatalf("expected bucket name to be %v, got %v", e, a)
				}
			}
		})
	}
}