11import { requireRequestsFrom } from "@aws-sdk/aws-util-test/src" ;
22import { DynamoDB } from "@aws-sdk/client-dynamodb" ;
3- import { AwsCredentialIdentity } from "@smithy/types" ;
3+ import type { AccountIdEndpointMode } from "@aws-sdk/core/account-id-endpoint" ;
4+ import type { AwsCredentialIdentity } from "@smithy/types" ;
45import { afterEach , beforeEach , describe , expect , test as it } from "vitest" ;
56
67describe ( "account id endpoint" , ( ) => {
7- const credentialsWithAccountId : AwsCredentialIdentity = {
8- accessKeyId : "INTEG_TEST" ,
9- secretAccessKey : "INTEG_TEST" ,
10- accountId : "123456789012" ,
11- } ;
12- const credentials = {
13- accessKeyId : "INTEG_TEST" ,
14- secretAccessKey : "INTEG_TEST" ,
15- } ;
8+ const region = "us-west-2" ;
169
1710 beforeEach ( async ( ) => {
1811 delete process . env . AWS_ACCOUNT_ID_ENDPOINT_MODE ;
1912 } ) ;
13+
2014 afterEach ( async ( ) => {
2115 delete process . env . AWS_ACCOUNT_ID_ENDPOINT_MODE ;
2216 } ) ;
2317
2418 describe ( "when credentials have account id" , ( ) => {
19+ const credentials : AwsCredentialIdentity = {
20+ accessKeyId : "INTEG_TEST" ,
21+ secretAccessKey : "INTEG_TEST" ,
22+ accountId : "123456789012" ,
23+ } ;
24+
2525 it ( "should default to resolving endpoint with account id when it is available in the client credentials" , async ( ) => {
2626 const ddb = new DynamoDB ( {
27- region : "us-west-2" ,
28- credentials : credentialsWithAccountId ,
27+ region,
28+ credentials,
2929 } ) ;
3030 requireRequestsFrom ( ddb ) . toMatch ( {
3131 hostname : / 1 2 3 4 5 6 7 8 9 0 1 2 .d d b .u s - w e s t - 2 .a m a z o n a w s .c o m / ,
@@ -34,83 +34,58 @@ describe("account id endpoint", () => {
3434 } ) ;
3535
3636 describe ( "config values" , ( ) => {
37- it ( "disabled" , async ( ) => {
38- const ddb = new DynamoDB ( {
39- region : "us-west-2" ,
40- credentials : credentialsWithAccountId ,
41- accountIdEndpointMode : "disabled" ,
42- } ) ;
43- requireRequestsFrom ( ddb ) . toMatch ( {
44- hostname : / d y n a m o d b .u s - w e s t - 2 .a m a z o n a w s .c o m / ,
45- } ) ;
46- await ddb . listTables ( ) ;
47- } ) ;
48- it ( "preferred" , async ( ) => {
49- const ddb = new DynamoDB ( {
50- region : "us-west-2" ,
51- credentials : credentialsWithAccountId ,
52- accountIdEndpointMode : "preferred" ,
53- } ) ;
54- requireRequestsFrom ( ddb ) . toMatch ( {
55- hostname : / 1 2 3 4 5 6 7 8 9 0 1 2 .d d b .u s - w e s t - 2 .a m a z o n a w s .c o m / ,
56- } ) ;
57- await ddb . listTables ( ) ;
58- } ) ;
59- it ( "required" , async ( ) => {
60- const ddb = new DynamoDB ( {
61- region : "us-west-2" ,
62- credentials : credentialsWithAccountId ,
63- accountIdEndpointMode : "required" ,
64- } ) ;
65- requireRequestsFrom ( ddb ) . toMatch ( {
66- hostname : / 1 2 3 4 5 6 7 8 9 0 1 2 .d d b .u s - w e s t - 2 .a m a z o n a w s .c o m / ,
67- } ) ;
68- await ddb . listTables ( ) ;
69- } ) ;
37+ it . each ( [
38+ [ "dynamodb" , "disabled" ] ,
39+ [ "123456789012.ddb" , "preferred" ] ,
40+ [ "123456789012.ddb" , "required" ] ,
41+ ] ) (
42+ "should match prefix '%s' for when accountId endpoint mode is '%s' in config" ,
43+ async ( expectedHostnamePrefix , mode ) => {
44+ const ddb = new DynamoDB ( {
45+ region,
46+ credentials,
47+ accountIdEndpointMode : mode as AccountIdEndpointMode ,
48+ } ) ;
49+ requireRequestsFrom ( ddb ) . toMatch ( {
50+ hostname : `${ expectedHostnamePrefix } .${ region } .amazonaws.com` ,
51+ } ) ;
52+ await ddb . listTables ( ) ;
53+ }
54+ ) ;
7055 } ) ;
7156
7257 describe ( "ENV values" , ( ) => {
73- it ( "disabled" , async ( ) => {
74- process . env . AWS_ACCOUNT_ID_ENDPOINT_MODE = "disabled" ;
75- const ddb = new DynamoDB ( {
76- region : "us-west-2" ,
77- credentials : credentialsWithAccountId ,
78- } ) ;
79- requireRequestsFrom ( ddb ) . toMatch ( {
80- hostname : / d y n a m o d b .u s - w e s t - 2 .a m a z o n a w s .c o m / ,
81- } ) ;
82- await ddb . listTables ( ) ;
83- } ) ;
84- it ( "preferred" , async ( ) => {
85- process . env . AWS_ACCOUNT_ID_ENDPOINT_MODE = "preferred" ;
86- const ddb = new DynamoDB ( {
87- region : "us-west-2" ,
88- credentials : credentialsWithAccountId ,
89- } ) ;
90- requireRequestsFrom ( ddb ) . toMatch ( {
91- hostname : / 1 2 3 4 5 6 7 8 9 0 1 2 .d d b .u s - w e s t - 2 .a m a z o n a w s .c o m / ,
92- } ) ;
93- await ddb . listTables ( ) ;
94- } ) ;
95- it ( "required" , async ( ) => {
96- process . env . AWS_ACCOUNT_ID_ENDPOINT_MODE = "required" ;
97- const ddb = new DynamoDB ( {
98- region : "us-west-2" ,
99- credentials : credentialsWithAccountId ,
100- } ) ;
101- requireRequestsFrom ( ddb ) . toMatch ( {
102- hostname : / 1 2 3 4 5 6 7 8 9 0 1 2 .d d b .u s - w e s t - 2 .a m a z o n a w s .c o m / ,
103- } ) ;
104- await ddb . listTables ( ) ;
105- } ) ;
58+ it . each ( [
59+ [ "dynamodb" , "disabled" ] ,
60+ [ "123456789012.ddb" , "preferred" ] ,
61+ [ "123456789012.ddb" , "required" ] ,
62+ ] ) (
63+ "should match prefix '%s' for when accountId endpoint mode is '%s' in ENV" ,
64+ async ( expectedHostnamePrefix , mode ) => {
65+ process . env . AWS_ACCOUNT_ID_ENDPOINT_MODE = mode ;
66+ const ddb = new DynamoDB ( {
67+ region,
68+ credentials,
69+ } ) ;
70+ requireRequestsFrom ( ddb ) . toMatch ( {
71+ hostname : `${ expectedHostnamePrefix } .${ region } .amazonaws.com` ,
72+ } ) ;
73+ await ddb . listTables ( ) ;
74+ }
75+ ) ;
10676 } ) ;
10777 } ) ;
10878
10979 describe ( "when credentials do not have account id" , ( ) => {
80+ const credentials = {
81+ accessKeyId : "INTEG_TEST" ,
82+ secretAccessKey : "INTEG_TEST" ,
83+ } ;
84+
11085 it ( "it follows that the hostname will not have it either" , async ( ) => {
11186 const ddb = new DynamoDB ( {
112- region : "us-west-2" ,
113- credentials : credentials ,
87+ region,
88+ credentials,
11489 } ) ;
11590 requireRequestsFrom ( ddb ) . toMatch ( {
11691 hostname : / d y n a m o d b .u s - w e s t - 2 .a m a z o n a w s .c o m / ,
@@ -121,17 +96,17 @@ describe("account id endpoint", () => {
12196 it ( "will fail if required by ENV" , async ( ) => {
12297 process . env . AWS_ACCOUNT_ID_ENDPOINT_MODE = "required" ;
12398 const ddb = new DynamoDB ( {
124- region : "us-west-2" ,
125- credentials : credentials ,
99+ region,
100+ credentials,
126101 } ) ;
127102 const error = await ddb . listTables ( ) . catch ( ( e ) => e ) ;
128103 expect ( error . name ) . toEqual ( "EndpointError" ) ;
129104 } ) ;
130105
131106 it ( "will fail if required by config" , async ( ) => {
132107 const ddb = new DynamoDB ( {
133- region : "us-west-2" ,
134- credentials : credentials ,
108+ region,
109+ credentials,
135110 accountIdEndpointMode : "required" ,
136111 } ) ;
137112 const error = await ddb . listTables ( ) . catch ( ( e ) => e ) ;
0 commit comments