forked from lucacasonato/deno_aws_sign_v4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_test.ts
87 lines (83 loc) · 2.99 KB
/
mod_test.ts
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
import { AWSSignerV4 } from "./mod.ts";
import {
assertEquals,
assertStringIncludes,
} from "https://deno.land/[email protected]/testing/asserts.ts";
Deno.test("construct from env vars", async () => {
Deno.env.set("AWS_ACCESS_KEY_ID", "examplekey");
Deno.env.set("AWS_SECRET_ACCESS_KEY", "secretkey");
Deno.env.set("AWS_SESSION_TOKEN", "sessiontoken");
Deno.env.set("AWS_REGION", "us-east-1");
const signer = new AWSSignerV4();
const req = await signer.sign(
"dynamodb",
new Request("https://test.dynamodb.us-east-1.amazonaws.com", {
method: "POST",
headers: { "x-hello": "world" },
body: "A dynamodb request!",
}),
);
const now = new Date();
const today = `${now.getFullYear()}${
(now.getMonth() + 1)
.toString()
.padStart(2, "0")
}${now.getDate().toString().padStart(2, "0")}`;
assertEquals(req.method, "POST");
assertEquals(await req.text(), "A dynamodb request!");
assertStringIncludes(req.headers.get("x-amz-date")!, `${today}T`);
assertEquals(req.headers.get("x-amz-security-token"), "sessiontoken");
assertEquals(req.headers.get("x-hello"), "world");
assertEquals(
req.headers.get("host"),
"test.dynamodb.us-east-1.amazonaws.com",
);
assertStringIncludes(
req.headers.get("Authorization")!,
`AWS4-HMAC-SHA256 Credential=examplekey/${today}/us-east-1/dynamodb/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-amz-security-token;x-hello, Signature=`,
);
});
Deno.test("construct manually", async () => {
const signer = new AWSSignerV4("us-east-2", {
awsAccessKeyId: "example_key",
awsSecretKey: "secret_key",
sessionToken: "session_token",
});
const req = await signer.sign(
"dynamodb",
new Request("https://test.dynamodb.us-east-1.amazonaws.com", {
method: "POST",
headers: { "x-hello": "world" },
body: "A dynamodb request!",
}),
);
const now = new Date();
const today = `${now.getFullYear()}${
(now.getMonth() + 1)
.toString()
.padStart(2, "0")
}${now.getDate().toString().padStart(2, "0")}`;
assertEquals(req.method, "POST");
assertEquals(await req.text(), "A dynamodb request!");
assertStringIncludes(req.headers.get("x-amz-date")!, `${today}T`);
assertEquals(req.headers.get("x-amz-security-token"), "session_token");
assertEquals(req.headers.get("x-hello"), "world");
assertEquals(
req.headers.get("host"),
"test.dynamodb.us-east-1.amazonaws.com",
);
assertStringIncludes(
req.headers.get("Authorization")!,
`AWS4-HMAC-SHA256 Credential=example_key/${today}/us-east-2/dynamodb/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-amz-security-token;x-hello, Signature=`,
);
});
Deno.test("example", async () => {
const signer = new AWSSignerV4();
const body = new TextEncoder().encode("Hello World!");
const request = new Request("https://test-bucket.s3.amazonaws.com/test", {
method: "PUT",
headers: { "content-length": body.length.toString() },
body,
});
const _req = await signer.sign("s3", request);
});