File: manual_protocol_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go-v2 1.30.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 662,428 kB
  • sloc: java: 16,875; makefile: 432; sh: 175
file content (405 lines) | stat: -rw-r--r-- 11,047 bytes parent folder | download
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package s3

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

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

// This file replicates the tests in https://github.com/smithy-lang/smithy/blob/main/smithy-aws-protocol-tests/model/restXml/services/s3.smithy,
// which we cannot generate through normal protocoltest codegen due to
// requirement on handwritten source in S3.

type capturedRequest struct {
	r *http.Request
}

func (cr *capturedRequest) Do(r *http.Request) (*http.Response, error) {
	cr.r = r
	return &http.Response{ // returns are moot, for request tests only
		StatusCode: 400,
		Body:       http.NoBody,
	}, nil
}

func TestS3Protocol_ListObjectsV2_Request(t *testing.T) {
	for name, tt := range map[string]struct {
		Options          func(*Options)
		OperationOptions func(*Options)
		Input            *ListObjectsV2Input

		ExpectMethod string
		ExpectHost   string
		ExpectPath   string
		ExpectQuery  []string
	}{
		"S3DefaultAddressing": {
			Options: func(o *Options) {
				o.Region = "us-west-2"
			},
			OperationOptions: func(o *Options) {},
			Input: &ListObjectsV2Input{
				Bucket: aws.String("mybucket"),
			},

			ExpectMethod: "GET",
			ExpectHost:   "mybucket.s3.us-west-2.amazonaws.com",
			ExpectPath:   "/",
			ExpectQuery:  []string{"list-type=2"},
		},
		"S3VirtualHostAddressing": {
			Options: func(o *Options) {
				o.Region = "us-west-2"
				o.UsePathStyle = false
			},
			OperationOptions: func(o *Options) {},
			Input: &ListObjectsV2Input{
				Bucket: aws.String("mybucket"),
			},

			ExpectMethod: "GET",
			ExpectHost:   "mybucket.s3.us-west-2.amazonaws.com",
			ExpectPath:   "/",
			ExpectQuery:  []string{"list-type=2"},
		},
		"S3PathAddressing": {
			Options: func(o *Options) {
				o.Region = "us-west-2"
				o.UsePathStyle = true
			},
			OperationOptions: func(o *Options) {},
			Input: &ListObjectsV2Input{
				Bucket: aws.String("mybucket"),
			},

			ExpectMethod: "GET",
			ExpectHost:   "s3.us-west-2.amazonaws.com",
			ExpectPath:   "/mybucket",
			ExpectQuery:  []string{"list-type=2"},
		},
		"S3VirtualHostDualstackAddressing": {
			Options: func(o *Options) {
				o.Region = "us-west-2"
				o.UsePathStyle = false
				o.EndpointOptions.UseDualStackEndpoint = aws.DualStackEndpointStateEnabled
			},
			OperationOptions: func(o *Options) {},
			Input: &ListObjectsV2Input{
				Bucket: aws.String("mybucket"),
			},

			ExpectMethod: "GET",
			ExpectHost:   "mybucket.s3.dualstack.us-west-2.amazonaws.com",
			ExpectPath:   "/",
			ExpectQuery:  []string{"list-type=2"},
		},
		"S3VirtualHostAccelerateAddressing": {
			Options: func(o *Options) {
				o.Region = "us-west-2"
				o.UsePathStyle = false
				o.UseAccelerate = true
			},
			OperationOptions: func(o *Options) {},
			Input: &ListObjectsV2Input{
				Bucket: aws.String("mybucket"),
			},

			ExpectMethod: "GET",
			ExpectHost:   "mybucket.s3-accelerate.amazonaws.com",
			ExpectPath:   "/",
			ExpectQuery:  []string{"list-type=2"},
		},
		"S3VirtualHostDualstackAccelerateAddressing": {
			Options: func(o *Options) {
				o.Region = "us-west-2"
				o.UsePathStyle = false
				o.EndpointOptions.UseDualStackEndpoint = aws.DualStackEndpointStateEnabled
				o.UseAccelerate = true
			},
			OperationOptions: func(o *Options) {},
			Input: &ListObjectsV2Input{
				Bucket: aws.String("mybucket"),
			},

			ExpectMethod: "GET",
			ExpectHost:   "mybucket.s3-accelerate.dualstack.amazonaws.com",
			ExpectPath:   "/",
			ExpectQuery:  []string{"list-type=2"},
		},
		"S3OperationAddressingPreferred": {
			Options: func(o *Options) {
				o.Region = "us-west-2"
				o.UsePathStyle = true
			},
			OperationOptions: func(o *Options) {
				o.UsePathStyle = false
			},
			Input: &ListObjectsV2Input{
				Bucket: aws.String("mybucket"),
			},

			ExpectMethod: "GET",
			ExpectHost:   "mybucket.s3.us-west-2.amazonaws.com",
			ExpectPath:   "/",
			ExpectQuery:  []string{"list-type=2"},
		},
	} {
		t.Run(name, func(t *testing.T) {
			var r capturedRequest
			svc := New(Options{HTTPClient: &r}, tt.Options)

			svc.ListObjectsV2(context.Background(), tt.Input, tt.OperationOptions)
			if r.r == nil {
				t.Fatal("captured request is nil")
			}

			req := r.r
			if tt.ExpectMethod != req.Method {
				t.Errorf("expect method: %v != %v", tt.ExpectMethod, req.Method)
			}
			if tt.ExpectHost != req.URL.Host {
				t.Errorf("expect host: %v != %v", tt.ExpectHost, req.URL.Host)
			}
			if tt.ExpectPath != req.URL.RawPath {
				t.Errorf("expect path: %v != %v", tt.ExpectPath, req.URL.RawPath)
			}
			for _, q := range tt.ExpectQuery {
				if !strings.Contains(req.URL.RawQuery, q) {
					t.Errorf("query %v is missing %v", req.URL.RawQuery, q)
				}
			}
		})
	}
}

func TestS3Protocol_DeleteObjectTagging_Request(t *testing.T) {
	for name, tt := range map[string]struct {
		ClientOptions    func(*Options)
		OperationOptions func(*Options)
		Input            *DeleteObjectTaggingInput

		ExpectMethod string
		ExpectHost   string
		ExpectPath   string
		ExpectQuery  []string
	}{
		"S3EscapeObjectKeyInUriLabel": {
			ClientOptions: func(o *Options) {
				o.Region = "us-west-2"
			},
			OperationOptions: func(o *Options) {},
			Input: &DeleteObjectTaggingInput{
				Bucket: aws.String("mybucket"),
				Key:    aws.String("my key.txt"),
			},

			ExpectMethod: "DELETE",
			ExpectHost:   "mybucket.s3.us-west-2.amazonaws.com",
			ExpectPath:   "/my%20key.txt",
			ExpectQuery:  []string{"tagging"},
		},
		"S3EscapePathObjectKeyInUriLabel": {
			ClientOptions: func(o *Options) {
				o.Region = "us-west-2"
			},
			OperationOptions: func(o *Options) {},
			Input: &DeleteObjectTaggingInput{
				Bucket: aws.String("mybucket"),
				Key:    aws.String("foo/bar/my key.txt"),
			},

			ExpectMethod: "DELETE",
			ExpectHost:   "mybucket.s3.us-west-2.amazonaws.com",
			ExpectPath:   "/foo/bar/my%20key.txt",
			ExpectQuery:  []string{"tagging"},
		},
	} {
		t.Run(name, func(t *testing.T) {
			var r capturedRequest
			svc := New(Options{HTTPClient: &r}, tt.ClientOptions)

			svc.DeleteObjectTagging(context.Background(), tt.Input, tt.OperationOptions)
			if r.r == nil {
				t.Fatal("captured request is nil")
			}

			req := r.r
			if tt.ExpectMethod != req.Method {
				t.Errorf("expect method: %v != %v", tt.ExpectMethod, req.Method)
			}
			if tt.ExpectHost != req.URL.Host {
				t.Errorf("expect host: %v != %v", tt.ExpectHost, req.URL.Host)
			}
			if tt.ExpectPath != req.URL.RawPath {
				t.Errorf("expect path: %v != %v", tt.ExpectPath, req.URL.RawPath)
			}
			for _, q := range tt.ExpectQuery {
				if !strings.Contains(req.URL.RawQuery, q) {
					t.Errorf("query %v is missing %v", req.URL.RawQuery, q)
				}
			}
		})
	}

}

func TestS3Protocol_GetObject_Request(t *testing.T) {
	for name, tt := range map[string]struct {
		ClientOptions    func(*Options)
		OperationOptions func(*Options)
		Input            *GetObjectInput

		ExpectMethod string
		ExpectHost   string
		ExpectPath   string
		ExpectQuery  []string
	}{
		"S3PreservesLeadingDotSegmentInUriLabel": {
			ClientOptions: func(o *Options) {
				o.Region = "us-west-2"
				o.UsePathStyle = false
			},
			OperationOptions: func(o *Options) {},
			Input: &GetObjectInput{
				Bucket: aws.String("mybucket"),
				Key:    aws.String("../key.txt"),
			},

			ExpectMethod: "GET",
			ExpectHost:   "mybucket.s3.us-west-2.amazonaws.com",
			ExpectPath:   "/../key.txt",
		},
		"S3PreservesEmbeddedDotSegmentInUriLabel": {
			ClientOptions: func(o *Options) {
				o.Region = "us-west-2"
				o.UsePathStyle = false
			},
			OperationOptions: func(o *Options) {},
			Input: &GetObjectInput{
				Bucket: aws.String("mybucket"),
				Key:    aws.String("foo/../key.txt"),
			},

			ExpectMethod: "GET",
			ExpectHost:   "mybucket.s3.us-west-2.amazonaws.com",
			ExpectPath:   "/foo/../key.txt",
		},
	} {
		t.Run(name, func(t *testing.T) {
			var r capturedRequest
			svc := New(Options{HTTPClient: &r}, tt.ClientOptions)

			svc.GetObject(context.Background(), tt.Input, tt.OperationOptions)
			if r.r == nil {
				t.Fatal("captured request is nil")
			}

			req := r.r
			if tt.ExpectMethod != req.Method {
				t.Errorf("expect method: %v != %v", tt.ExpectMethod, req.Method)
			}
			if tt.ExpectHost != req.URL.Host {
				t.Errorf("expect host: %v != %v", tt.ExpectHost, req.URL.Host)
			}
			if tt.ExpectPath != req.URL.RawPath {
				t.Errorf("expect path: %v != %v", tt.ExpectPath, req.URL.RawPath)
			}
			for _, q := range tt.ExpectQuery {
				if !strings.Contains(req.URL.RawQuery, q) {
					t.Errorf("query %v is missing %v", req.URL.RawQuery, q)
				}
			}
		})
	}

}

type mockHTTPResponse struct {
	resp *http.Response
}

func (m *mockHTTPResponse) Do(r *http.Request) (*http.Response, error) {
	return m.resp, nil
}

func TestS3Protocol_GetBucketLocation_Response(t *testing.T) {
	for name, tt := range map[string]struct {
		Response *http.Response
		Expect   *GetBucketLocationOutput
	}{
		"GetBucketLocationUnwrappedOutput": {
			Response: &http.Response{
				StatusCode: 200,
				Body:       io.NopCloser(strings.NewReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<LocationConstraint xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">us-west-2</LocationConstraint>")),
			},
			Expect: &GetBucketLocationOutput{
				LocationConstraint: types.BucketLocationConstraintUsWest2,
			},
		},
	} {
		t.Run(name, func(t *testing.T) {
			svc := New(Options{
				Region:     "us-west-2",
				HTTPClient: &mockHTTPResponse{tt.Response},
			})

			out, err := svc.GetBucketLocation(context.Background(), &GetBucketLocationInput{
				Bucket: aws.String("bucket"),
			})
			if err != nil {
				t.Fatalf("get bucket location: %v", err)
			}

			if tt.Expect.LocationConstraint != out.LocationConstraint {
				t.Errorf("LocationConstraint %v != %v", tt.Expect.LocationConstraint, out.LocationConstraint)
			}
		})
	}
}

func TestS3Protocol_Error_NoSuchBucket(t *testing.T) {
	for name, tt := range map[string]struct {
		Response *http.Response
	}{
		"GetBucketLocationUnwrappedOutput": {
			Response: &http.Response{
				StatusCode: 400,
				Body:       io.NopCloser(strings.NewReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error>\n\t<Type>Sender</Type>\n\t<Code>NoSuchBucket</Code>\n</Error>")),
			},
		},
	} {
		t.Run(name, func(t *testing.T) {
			svc := New(Options{
				Region:     "us-west-2",
				HTTPClient: &mockHTTPResponse{tt.Response},
			})

			_, err := svc.GetObject(context.Background(), &GetObjectInput{
				Bucket: aws.String("bucket"),
				Key:    aws.String("key"),
			})
			if err == nil {
				t.Fatal("call operation: expected error, got none")
			}

			// of note: we don't actually return a *types.NoSuchBucket in this
			// case, but we DO capture the right error code
			var terr interface {
				ErrorCode() string
			}
			if !errors.As(err, &terr) {
				t.Errorf("error does not implement ErrorCode(), was %v", err)
			}
			if actual := terr.ErrorCode(); actual != "NoSuchBucket" {
				t.Errorf("error code, expected NoSuchBucket, was %v", actual)
			}
		})
	}

}