Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zirkelc committed Jan 8, 2025
1 parent 0386b1b commit 4ea8e2c
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 40 deletions.
6 changes: 4 additions & 2 deletions packages/aws-sigv4-sign/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const { signRequest } = require('aws-sigv4-sign');
```

## Usage
This package exports a `signRequest` function that returns a `Request` object with signed headers for AWS Signature V4 (SigV4) authentication.
This package exports a `signRequest` function that returns a [`Request` object](https://developer.mozilla.org/en-US/docs/Web/API/Request) with signed headers for AWS Signature V4 (SigV4) authentication.
The function is overloaded with multiple signatures to make it easy to use.

```ts
Expand Down Expand Up @@ -58,7 +58,7 @@ const signedRequest = await signRequest(url,
);
```

The returned `Request` object contains the signed authorization `headers` with the following keys: `authorization`, `host`, `x-amz-date`, `x-amz-content-sha256`, `x-amz-security-token` (optional).
The returned [`Request` object](https://developer.mozilla.org/en-US/docs/Web/API/Request) contains the signed authorization `headers` with the following keys: `authorization`, `host`, `x-amz-date`, `x-amz-content-sha256`, `x-amz-security-token` (optional).

```ts
const signedRequest = await signRequest(url, options);
Expand Down Expand Up @@ -91,6 +91,8 @@ The following examples show how to sign requests with different HTTP libraries.

### Fetch

The signed [`Request` object](https://developer.mozilla.org/en-US/docs/Web/API/Request) can be passed directly to the `fetch` API.

```ts
import { signRequest } from "aws-sigv4-sign";

Expand Down
13 changes: 5 additions & 8 deletions test/aws/test/api-gateway.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ describe("APIGateway", () => {
const signedFetch = createSignedFetcher({ service: SERVICE, region: REGION });

// Act
const response = await signedFetch(url, {
method: "GET",
});
const response = await signedFetch(url);

// Assert
expect(response.status).toBe(200);
Expand Down Expand Up @@ -68,9 +66,7 @@ describe("APIGateway", () => {
// Arrange

// Act
const response = await fetch(url, {
method: "GET",
});
const response = await fetch(url);

// Assert
expect(response.status).toBe(403);
Expand All @@ -82,14 +78,15 @@ describe("APIGateway", () => {
describe("POST", () => {
describe.each(paths)("Path: %s", async (path) => {
const url = `${apiRootUrl}${path}`;
const method = "POST";

it("should fetch with string", async () => {
// Arrange
const signedFetch = createSignedFetcher({ service: SERVICE, region: REGION });

// Act
const response = await signedFetch(url, {
method: "POST",
method,
body: JSON.stringify({}),
headers: {
"Content-Type": "application/json",
Expand Down Expand Up @@ -136,7 +133,7 @@ describe("APIGateway", () => {

// Act
const response = await fetch(url, {
method: "POST",
method,
body: JSON.stringify({}),
headers: {
"Content-Type": "application/json",
Expand Down
29 changes: 12 additions & 17 deletions test/aws/test/iam.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ describe("IAM", () => {

// Act
const response = await signedFetch(url, {
method: "GET",
headers: {
"x-amz-test-header": "test-value",
"x-api-key": "test-api-key",
Expand Down Expand Up @@ -105,6 +104,10 @@ describe("IAM", () => {

describe("POST", () => {
const url = "https://iam.amazonaws.com/";
const method = "POST";
const headers = {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
};
const body = "Action=GetUser&Version=2010-05-08";

it("should fetch with string", async () => {
Expand All @@ -113,11 +116,9 @@ describe("IAM", () => {

// Act
const response = await signedFetch(url, {
method: "POST",
method,
body,
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
},
headers,
});

// Assert
Expand All @@ -133,11 +134,9 @@ describe("IAM", () => {

// Act
const response = await signedFetch(new URL(url), {
method: "POST",
method,
body,
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
},
headers,
});

// Assert
Expand All @@ -153,11 +152,9 @@ describe("IAM", () => {

// Act
const response = await signedFetch(new Request(url), {
method: "POST",
method,
body,
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
},
headers,
});

// Assert
Expand All @@ -172,11 +169,9 @@ describe("IAM", () => {

// Act
const response = await fetch(url, {
method: "POST",
method,
body,
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
},
headers,
});

// Assert
Expand Down
25 changes: 12 additions & 13 deletions test/aws/test/lambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ describe("Lambda Function URL", () => {
const signedFetch = createSignedFetcher({ service: SERVICE, region: REGION });

// Act
const response = await signedFetch(url, {
method: "GET",
});
const response = await signedFetch(url);

// Assert
expect(response.status).toBe(200);
Expand Down Expand Up @@ -75,18 +73,21 @@ describe("Lambda Function URL", () => {
describe("POST", () => {
describe.each(paths)("Path: %s", (path) => {
const url = `${functionUrl}${path}`;
const method = "POST";
const headers = {
"Content-Type": "application/json",
};
const body = JSON.stringify({});

it("should fetch with string", async () => {
// Arrange
const signedFetch = createSignedFetcher({ service: SERVICE, region: REGION });

// Act
const response = await signedFetch(url, {
method: "POST",
body: JSON.stringify({}),
headers: {
"Content-Type": "application/json",
},
method,
body,
headers,
});

// Assert
Expand Down Expand Up @@ -122,11 +123,9 @@ describe("Lambda Function URL", () => {

// Act
const response = await fetch(url, {
method: "POST",
body: JSON.stringify({}),
headers: {
"Content-Type": "application/json",
},
method,
body,
headers,
});

// Assert
Expand Down
66 changes: 66 additions & 0 deletions test/http/test/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { signRequest } from "aws-sigv4-sign";
import { describe, expect, it } from "vitest";

const SERVICE = "iam";
const REGION = "us-east-1";

describe("fetch", () => {
describe("GET", () => {
const url = "https://iam.amazonaws.com/?Action=GetUser&Version=2010-05-08";

it("should make request with signed headers", async () => {
// Arrange
const signedRequest = await signRequest(url, { service: SERVICE, region: REGION });

// Act
const response = await fetch(signedRequest);

// Assert
expect(response.status).toBe(200);
expect(await response.text()).toContain("<GetUserResult>");
});

it("should fail with unsigned request", async () => {
// Arrange

// Act
const response = await fetch(url);

// Assert
expect(response.status).toBe(403);
expect(response.statusText).toBe("Forbidden");
});
});

describe("POST", () => {
const url = "https://iam.amazonaws.com/";
const method = "POST";
const headers = {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
};
const body = "Action=GetUser&Version=2010-05-08";

it("should make request with signed headers", async () => {
// Arrange
const signedRequest = await signRequest(url, { method, headers, body }, { service: SERVICE, region: REGION });

// Act
const response = await fetch(signedRequest);

// Assert
expect(response.status).toBe(200);
expect(await response.text()).toContain("<GetUserResult>");
});

it("should fail with unsigned request", async () => {
// Arrange

// Act
const response = await fetch(url, { method, headers, body });

// Assert
expect(response.status).toBe(403);
expect(response.statusText).toBe("Forbidden");
});
});
});

0 comments on commit 4ea8e2c

Please sign in to comment.