Skip to content

Commit 08c6c21

Browse files
authored
test(token-providers): add integ test for fromEnvSigningName (#7241)
* test(token-providers): add integ test for fromEnvSigningName * test(token-providers): add interaction with authSchemePreference
1 parent 6e8cd3d commit 08c6c21

File tree

4 files changed

+104
-5
lines changed

4 files changed

+104
-5
lines changed

packages/token-providers/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"clean": "rimraf ./dist-* && rimraf *.tsbuildinfo",
1616
"extract:docs": "api-extractor run --local",
1717
"test": "yarn g:vitest run",
18-
"test:watch": "yarn g:vitest watch"
18+
"test:watch": "yarn g:vitest watch",
19+
"test:integration": "yarn g:vitest run -c vitest.config.integ.ts",
20+
"test:integration:watch": "yarn g:vitest watch -c vitest.config.integ.ts"
1921
},
2022
"keywords": [
2123
"aws",
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { requireRequestsFrom } from "@aws-sdk/aws-util-test/src";
2+
import { Bedrock } from "@aws-sdk/client-bedrock";
3+
import { BedrockRuntime } from "@aws-sdk/client-bedrock-runtime";
4+
import { afterEach, beforeEach, describe, it } from "vitest";
5+
6+
describe("from env signing name integration", () => {
7+
const mockBearerToken = "mock-bearer-token";
8+
const credentials = {
9+
accessKeyId: "INTEG",
10+
secretAccessKey: "INTEG",
11+
};
12+
const region = "us-west-2";
13+
14+
beforeEach(() => {
15+
delete process.env.AWS_BEARER_TOKEN_BEDROCK;
16+
});
17+
18+
afterEach(() => {
19+
delete process.env.AWS_BEARER_TOKEN_BEDROCK;
20+
});
21+
22+
function expectBearerToken(client: Bedrock | BedrockRuntime) {
23+
requireRequestsFrom(client).toMatch({
24+
headers: {
25+
authorization: `Bearer ${mockBearerToken}`,
26+
},
27+
});
28+
}
29+
30+
function expectSigV4(client: Bedrock | BedrockRuntime) {
31+
requireRequestsFrom(client).toMatch({
32+
hostname: /bedrock/,
33+
headers: {
34+
authorization: /AWS4-HMAC-SHA256 Credential/,
35+
},
36+
});
37+
}
38+
39+
describe("when environment bearer token is set", () => {
40+
it("bedrock", async () => {
41+
process.env.AWS_BEARER_TOKEN_BEDROCK = mockBearerToken;
42+
const client = new Bedrock({ region, credentials });
43+
expectBearerToken(client);
44+
await client.listCustomModels();
45+
});
46+
47+
it("bedrock runtime", async () => {
48+
process.env.AWS_BEARER_TOKEN_BEDROCK = mockBearerToken;
49+
const client = new BedrockRuntime({
50+
region,
51+
credentials,
52+
});
53+
expectBearerToken(client);
54+
await client.listAsyncInvokes();
55+
});
56+
57+
it("can be overridden by auth scheme preference", async () => {
58+
process.env.AWS_BEARER_TOKEN_BEDROCK = mockBearerToken;
59+
const client = new Bedrock({ region, credentials, authSchemePreference: ["sigv4"] });
60+
expectSigV4(client);
61+
await client.listCustomModels();
62+
});
63+
});
64+
65+
describe("when environment bearer token is not set", () => {
66+
it("bedrock", async () => {
67+
const client = new Bedrock({ region, credentials });
68+
expectSigV4(client);
69+
await client.listCustomModels();
70+
});
71+
72+
it("bedrock runtime", async () => {
73+
const client = new BedrockRuntime({
74+
region,
75+
credentials,
76+
});
77+
expectSigV4(client);
78+
await client.listAsyncInvokes();
79+
});
80+
});
81+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
include: ["**/*.integ.spec.ts"],
6+
environment: "node",
7+
},
8+
});

private/aws-util-test/src/requests/test-http-handler.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ export class TestHttpHandler implements HttpHandler {
122122
this.check(m.hostname, request.hostname);
123123
this.check(m.port, request.port);
124124
this.check(m.path, request.path);
125-
this.checkAll(m.query, request.query);
125+
this.checkAll(m.query ?? {}, request.query, "query");
126126

127-
this.checkAll(m.headers, request.headers);
127+
this.checkAll(m.headers ?? {}, request.headers, "header");
128128
this.check(m.body, request.body);
129129
this.check(m.method, request.method);
130130

@@ -189,7 +189,11 @@ export class TestHttpHandler implements HttpHandler {
189189
this.assertions++;
190190
}
191191

192-
private checkAll(matchers?: Record<string, Matcher> | Map<RegExp | string, Matcher>, observed?: any) {
192+
private checkAll(
193+
matchers: Record<string, Matcher> | Map<RegExp | string, Matcher>,
194+
observed: any,
195+
type: "header" | "query"
196+
) {
193197
if (matchers == null) {
194198
return;
195199
}
@@ -201,7 +205,11 @@ export class TestHttpHandler implements HttpHandler {
201205
if (key.startsWith("/") && key.endsWith("/")) {
202206
key = new RegExp(key);
203207
} else {
204-
this.check(matcher, observed[key]);
208+
const matchingValue =
209+
type === "header"
210+
? observed[Object.keys(observed).find((k) => k.toLowerCase() === String(key).toLowerCase()) ?? ""]
211+
: observed[key];
212+
this.check(matcher, matchingValue);
205213
}
206214
}
207215
if (key instanceof RegExp) {

0 commit comments

Comments
 (0)