Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(util-stream): switch lambda test from e2e to integration #4864

Merged
merged 1 commit into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions packages/util-stream/jest.config.e2e.js

This file was deleted.

3 changes: 1 addition & 2 deletions packages/util-stream/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"build:types:downlevel": "downlevel-dts dist-types dist-types/ts3.4",
"clean": "rimraf ./dist-* && rimraf *.tsbuildinfo",
"test": "jest && karma start karma.conf.js",
"test:integration": "jest -c jest.config.integ.js",
"test:e2e": "jest -c jest.config.e2e.js"
"test:integration": "jest -c jest.config.integ.js"
},
"main": "./dist-cjs/index.js",
"module": "./dist-es/index.js",
Expand Down
75 changes: 75 additions & 0 deletions packages/util-stream/src/util-stream.integ.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Lambda } from "@aws-sdk/client-lambda";
import { HttpResponse } from "@aws-sdk/protocol-http";
import { HttpRequest as IHttpRequest } from "@aws-sdk/types";
import { Uint8ArrayBlobAdapter } from "@aws-sdk/util-stream";
import { fromUtf8 } from "@aws-sdk/util-utf8";
import { Readable } from "stream";

import { requireRequestsFrom } from "../../../private/aws-util-test/src";

Expand Down Expand Up @@ -41,4 +46,74 @@ describe("util-stream", () => {
});
});
});

describe("blob helper integration", () => {
const FunctionName = "echo";

const lambda = new Lambda({
region: "us-west-2",
});

requireRequestsFrom(lambda).toMatch({
method: "POST",
hostname: "lambda.us-west-2.amazonaws.com",
query: {},
headers: {
"content-type": "application/octet-stream",
host: "lambda.us-west-2.amazonaws.com",
},
protocol: "https:",
path: "/2015-03-31/functions/echo/invocations",
});

lambda.config.requestHandler = new (class {
async handle(request: IHttpRequest) {
return {
response: new HttpResponse({
statusCode: 200,
body: typeof request.body === "string" ? fromUtf8(request.body) : Uint8Array.from(request.body),
}),
};
}
})();

it("should allow string as payload blob and allow conversion of output payload blob to string", async () => {
const payload = JSON.stringify({ hello: "world" });
const invoke = await lambda.invoke({ FunctionName, Payload: payload });
expect(JSON.parse(invoke?.Payload?.transformToString() ?? "{}")).toEqual({ hello: "world" });
});

it("should allow Uint8Array as payload blob", async () => {
const payload = Uint8ArrayBlobAdapter.fromString(JSON.stringify({ hello: "world" }));
const invoke = await lambda.invoke({ FunctionName, Payload: payload });
expect(JSON.parse(invoke?.Payload?.transformToString() ?? "{}")).toEqual({ hello: "world" });
});

it("should allow buffer as payload blob", async () => {
// note: Buffer extends Uint8Array
const payload = Buffer.from(Uint8ArrayBlobAdapter.fromString(JSON.stringify({ hello: "world" })));
const invoke = await lambda.invoke({ FunctionName, Payload: payload });
expect(JSON.parse(invoke?.Payload?.transformToString() ?? "{}")).toEqual({ hello: "world" });
});

it("should allow stream as payload blob but not be able to sign it", async () => {
const payload = Readable.from(Buffer.from(Uint8ArrayBlobAdapter.fromString(JSON.stringify({ hello: "world" }))), {
encoding: "utf-8",
});
expect(JSON.parse(await streamToString(payload))).toEqual({ hello: "world" });
await lambda.invoke({ FunctionName, Payload: payload }).catch((e) => {
expect(e.toString()).toContain("InvalidSignatureException");
});
expect.hasAssertions();
});
});

function streamToString(stream: Readable): Promise<string> {
const chunks: any[] = [];
return new Promise((resolve, reject) => {
stream.on("data", (chunk) => chunks.push(Buffer.from(chunk)));
stream.on("error", (err) => reject(err));
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
});
}
});
Binary file removed packages/util-stream/test/function.zip
Binary file not shown.
6 changes: 0 additions & 6 deletions packages/util-stream/test/index.mjs

This file was deleted.

149 changes: 0 additions & 149 deletions packages/util-stream/test/setup.ts

This file was deleted.

56 changes: 0 additions & 56 deletions packages/util-stream/test/util-stream-blob.e2e.spec.ts

This file was deleted.