1 package endpoints
2
3 import (
4 "reflect"
5 "regexp"
6 "testing"
7
8 "github.com/aws/aws-sdk-go-v2/aws"
9 )
10
11 func TestEndpointResolve(t *testing.T) {
12 defs := Endpoint{
13 Hostname: "service.{region}.amazonaws.com",
14 SignatureVersions: []string{"v4"},
15 }
16
17 e := Endpoint{
18 Protocols: []string{"http", "https"},
19 SignatureVersions: []string{"v4"},
20 CredentialScope: CredentialScope{
21 Region: "us-west-2",
22 Service: "service",
23 },
24 }
25
26 resolved := e.resolve("aws", "us-west-2", defs, Options{})
27
28 if e, a := "https://service.us-west-2.amazonaws.com", resolved.URL; e != a {
29 t.Errorf("expect %v, got %v", e, a)
30 }
31 if e, a := "aws", resolved.PartitionID; e != a {
32 t.Errorf("expect %v, got %v", e, a)
33 }
34 if e, a := "service", resolved.SigningName; e != a {
35 t.Errorf("expect %v, got %v", e, a)
36 }
37 if e, a := "us-west-2", resolved.SigningRegion; e != a {
38 t.Errorf("expect %v, got %v", e, a)
39 }
40 if e, a := "v4", resolved.SigningMethod; e != a {
41 t.Errorf("expect %v, got %v", e, a)
42 }
43 }
44
45 func TestEndpointMergeIn(t *testing.T) {
46 expected := Endpoint{
47 Hostname: "other hostname",
48 Protocols: []string{"http"},
49 SignatureVersions: []string{"v4"},
50 CredentialScope: CredentialScope{
51 Region: "region",
52 Service: "service",
53 },
54 }
55
56 actual := Endpoint{}
57 actual.mergeIn(Endpoint{
58 Hostname: "other hostname",
59 Protocols: []string{"http"},
60 SignatureVersions: []string{"v4"},
61 CredentialScope: CredentialScope{
62 Region: "region",
63 Service: "service",
64 },
65 })
66
67 if e, a := expected, actual; !reflect.DeepEqual(e, a) {
68 t.Errorf("expect %v, got %v", e, a)
69 }
70 }
71
72 var testPartitions = Partitions{
73 {
74 ID: "part-id-1",
75 RegionRegex: func() *regexp.Regexp {
76 reg, _ := regexp.Compile(`^(us)\-\w+\-\d+$`)
77 return reg
78 }(),
79 Defaults: Endpoint{
80 Hostname: "service.{region}.amazonaws.com",
81 Protocols: []string{"https"},
82 SignatureVersions: []string{"v4"},
83 },
84 IsRegionalized: true,
85 Endpoints: Endpoints{
86 "us-west-1": {},
87 "us-west-1-alt": {
88 Hostname: "service-alt.us-west-1.amazonaws.com",
89 Protocols: []string{"http"},
90 SignatureVersions: []string{"vFoo"},
91 CredentialScope: CredentialScope{
92 Region: "us-west-1",
93 Service: "foo",
94 },
95 },
96 },
97 },
98 {
99 ID: "part-id-2",
100 RegionRegex: func() *regexp.Regexp {
101 reg, _ := regexp.Compile(`^(cn)\-\w+\-\d+$`)
102 return reg
103 }(),
104 Defaults: Endpoint{
105 Protocols: []string{"https"},
106 SignatureVersions: []string{"v4"},
107 CredentialScope: CredentialScope{
108 Service: "foo",
109 },
110 },
111 IsRegionalized: false,
112 PartitionEndpoint: "partition",
113 Endpoints: Endpoints{
114 "partition": {
115 Hostname: "some-global-thing.amazonaws.com.cn",
116 CredentialScope: CredentialScope{
117 Region: "cn-east-1",
118 },
119 },
120 "fips-partition": {
121 Hostname: "some-global-thing-fips.amazonaws.com.cn",
122 CredentialScope: CredentialScope{
123 Region: "cn-east-1",
124 },
125 },
126 },
127 },
128 {
129 ID: "part-id-3",
130 RegionRegex: func() *regexp.Regexp {
131 reg, _ := regexp.Compile(`^(eu)\-\w+\-\d+$`)
132 return reg
133 }(),
134 Defaults: Endpoint{
135 Hostname: "service.{region}.amazonaws.com",
136 Protocols: []string{"https"},
137 SignatureVersions: []string{"v4"},
138 CredentialScope: CredentialScope{
139 Service: "foo",
140 },
141 },
142 IsRegionalized: true,
143 },
144 }
145
146 func TestResolveEndpoint(t *testing.T) {
147 var cases = map[string]struct {
148 Region string
149 Options Options
150 Expected aws.Endpoint
151 }{
152 "modeled region with no endpoint overrides": {
153 Region: "us-west-1",
154 Expected: aws.Endpoint{
155 PartitionID: "part-id-1",
156 URL: "https://service.us-west-1.amazonaws.com",
157 SigningRegion: "us-west-1",
158 SigningMethod: "v4",
159 },
160 },
161 "modeled region with no endpoint overrides and https disabled": {
162 Region: "us-west-1",
163 Options: Options{DisableHTTPS: true},
164 Expected: aws.Endpoint{
165 PartitionID: "part-id-1",
166 URL: "http://service.us-west-1.amazonaws.com",
167 SigningRegion: "us-west-1",
168 SigningMethod: "v4",
169 },
170 },
171 "modeled region with endpoint overrides": {
172 Region: "us-west-1-alt",
173 Expected: aws.Endpoint{
174 PartitionID: "part-id-1",
175 URL: "http://service-alt.us-west-1.amazonaws.com",
176 SigningRegion: "us-west-1",
177 SigningName: "foo",
178 SigningMethod: "vFoo",
179 },
180 },
181 "partition endpoint": {
182 Region: "cn-central-1",
183 Expected: aws.Endpoint{
184 PartitionID: "part-id-2",
185 URL: "https://some-global-thing.amazonaws.com.cn",
186 SigningRegion: "cn-east-1",
187 SigningName: "foo",
188 SigningMethod: "v4",
189 },
190 },
191 "specified partition endpoint": {
192 Region: "partition",
193 Expected: aws.Endpoint{
194 PartitionID: "part-id-2",
195 URL: "https://some-global-thing.amazonaws.com.cn",
196 SigningRegion: "cn-east-1",
197 SigningName: "foo",
198 SigningMethod: "v4",
199 },
200 },
201 "fips partition endpoint": {
202 Region: "fips-partition",
203 Expected: aws.Endpoint{
204 PartitionID: "part-id-2",
205 URL: "https://some-global-thing-fips.amazonaws.com.cn",
206 SigningRegion: "cn-east-1",
207 SigningName: "foo",
208 SigningMethod: "v4",
209 },
210 },
211 "region with unmodeled endpoints": {
212 Region: "eu-west-1",
213 Expected: aws.Endpoint{
214 PartitionID: "part-id-3",
215 URL: "https://service.eu-west-1.amazonaws.com",
216 SigningRegion: "eu-west-1",
217 SigningName: "foo",
218 SigningMethod: "v4",
219 },
220 },
221 }
222 for name, tt := range cases {
223 t.Run(name, func(t *testing.T) {
224 endpoint, err := testPartitions.ResolveEndpoint(tt.Region, tt.Options)
225 if err != nil {
226 t.Errorf("expected no error, got %v", err)
227 }
228 if e, a := tt.Expected, endpoint; !reflect.DeepEqual(e, a) {
229 t.Errorf("expected %v, got %v", e, a)
230 }
231 })
232 }
233 }
234
View as plain text