Debian Codegraph @

aws/aws_test.go

raw
1 // Copyright 2019 The Go Cloud Development Kit Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package aws_test
16
17 import (
18 "context"
19 "net/url"
20 "reflect"
21 "strings"
22 "testing"
23
24 "github.com/aws/aws-sdk-go-v2/aws"
25 "github.com/aws/aws-sdk-go-v2/aws/retry"
26 gcaws "gocloud.dev/aws"
27 )
28
29 func TestV2ConfigFromURLParams(t *testing.T) {
30 const service = "s3"
31 const region = "us-east-1"
32 const partitionID = "aws"
33 ctx := context.Background()
34 tests := []struct {
35 name string
36 query url.Values
37 wantRegion string
38 wantErr bool
39 wantEndpoint *aws.Endpoint
40 }{
41 {
42 name: "No overrides",
43 query: url.Values{},
44 },
45 {
46 name: "Invalid query parameter",
47 query: url.Values{"foo": {"bar"}},
48 wantErr: true,
49 },
50 {
51 name: "Region",
52 query: url.Values{"region": {"my_region"}},
53 wantRegion: "my_region",
54 },
55 {
56 name: "Endpoint and hostname immutable",
57 query: url.Values{"endpoint": {"foo"}, "hostname_immutable": {"true"}},
58 wantEndpoint: &aws.Endpoint{
59 PartitionID: partitionID,
60 SigningRegion: region,
61 URL: "foo",
62 HostnameImmutable: true,
63 },
64 },
65 {
66 name: "FIPS and dual stack",
67 query: url.Values{"fips": {"true"}, "dualstack": {"true"}},
68 },
69 {
70 name: "anonymous",
71 query: url.Values{"anonymous": {"true"}},
72 },
73 {
74 name: "Rate limit capacity",
75 query: url.Values{"rate_limiter_capacity": {"500"}},
76 },
77 {
78 name: "Request checksum calculation when_supported",
79 query: url.Values{"request_checksum_calculation": {"when_supported"}},
80 },
81 {
82 name: "Request checksum calculation when_required",
83 query: url.Values{"request_checksum_calculation": {"when_required"}},
84 },
85 {
86 name: "Response checksum validation when_supported",
87 query: url.Values{"response_checksum_validation": {"when_supported"}},
88 },
89 {
90 name: "Response checksum validation when_required",
91 query: url.Values{"response_checksum_validation": {"when_required"}},
92 },
93 {
94 name: "Both checksum parameters",
95 query: url.Values{"request_checksum_calculation": {"when_required"}, "response_checksum_validation": {"when_supported"}},
96 },
97 {
98 name: "Invalid request checksum value",
99 query: url.Values{"request_checksum_calculation": {"invalid"}},
100 wantErr: true,
101 },
102 {
103 name: "Invalid response checksum value",
104 query: url.Values{"response_checksum_validation": {"invalid"}},
105 wantErr: true,
106 },
107 {
108 name: "Empty request checksum value",
109 query: url.Values{"request_checksum_calculation": {""}},
110 wantErr: true,
111 },
112 {
113 name: "Empty response checksum value",
114 query: url.Values{"response_checksum_validation": {""}},
115 wantErr: true,
116 },
117 {
118 name: "Uppercase request checksum",
119 query: url.Values{"request_checksum_calculation": {"WHEN_SUPPORTED"}},
120 },
121 {
122 name: "Mixed case response checksum",
123 query: url.Values{"response_checksum_validation": {"When_Required"}},
124 },
125 // Can't test "profile", since AWS validates that the profile exists.
126 }
127
128 for _, test := range tests {
129 t.Run(test.name, func(t *testing.T) {
130 got, err := gcaws.V2ConfigFromURLParams(ctx, test.query)
131 if (err != nil) != test.wantErr {
132 t.Errorf("got err %v want error %v", err, test.wantErr)
133 return
134 }
135 if err != nil {
136 return
137 }
138 if test.wantRegion != "" && got.Region != test.wantRegion {
139 t.Errorf("got region %q, want %q", got.Region, test.wantRegion)
140 }
141
142 // Check checksum configuration based on query parameters
143 if test.query.Has("request_checksum_calculation") {
144 expectedValue := test.query.Get("request_checksum_calculation")
145 var expectedChecksum aws.RequestChecksumCalculation
146 switch strings.ToLower(expectedValue) {
147 case "when_supported":
148 expectedChecksum = aws.RequestChecksumCalculationWhenSupported
149 case "when_required":
150 expectedChecksum = aws.RequestChecksumCalculationWhenRequired
151 }
152 if got.RequestChecksumCalculation != expectedChecksum {
153 t.Errorf("got RequestChecksumCalculation %v, want %v", got.RequestChecksumCalculation, expectedChecksum)
154 }
155 }
156
157 if test.query.Has("response_checksum_validation") {
158 expectedValue := test.query.Get("response_checksum_validation")
159 var expectedChecksum aws.ResponseChecksumValidation
160 switch strings.ToLower(expectedValue) {
161 case "when_supported":
162 expectedChecksum = aws.ResponseChecksumValidationWhenSupported
163 case "when_required":
164 expectedChecksum = aws.ResponseChecksumValidationWhenRequired
165 }
166 if got.ResponseChecksumValidation != expectedChecksum {
167 t.Errorf("got ResponseChecksumValidation %v, want %v", got.ResponseChecksumValidation, expectedChecksum)
168 }
169 }
170
171 if test.wantEndpoint != nil {
172 if got.EndpointResolverWithOptions == nil {
173 t.Fatalf("expected an EndpointResolverWithOptions, got nil")
174 }
175 gotE, err := got.EndpointResolverWithOptions.ResolveEndpoint(service, region)
176 if err != nil {
177 return
178 }
179 if !reflect.DeepEqual(gotE, *test.wantEndpoint) {
180 t.Errorf("got endpoint %+v, want %+v", gotE, *test.wantEndpoint)
181 }
182 }
183
184 // Unfortunately, we can't look at the options set for the rate limiter.
185 r, ok := got.Retryer().(*retry.Standard)
186 if !ok {
187 t.Errorf("expected a standard retryer, got %v, expected retry.Standard", r)
188 }
189 })
190 }
191 }