Skip to content

Commit 7c50810

Browse files
authoredJun 22, 2023
test(util-stream): switch lambda test from e2e to integration (#4864)
1 parent d10238a commit 7c50810

File tree

7 files changed

+76
-217
lines changed

7 files changed

+76
-217
lines changed
 

‎packages/util-stream/jest.config.e2e.js

-4
This file was deleted.

‎packages/util-stream/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
"build:types:downlevel": "downlevel-dts dist-types dist-types/ts3.4",
1111
"clean": "rimraf ./dist-* && rimraf *.tsbuildinfo",
1212
"test": "jest && karma start karma.conf.js",
13-
"test:integration": "jest -c jest.config.integ.js",
14-
"test:e2e": "jest -c jest.config.e2e.js"
13+
"test:integration": "jest -c jest.config.integ.js"
1514
},
1615
"main": "./dist-cjs/index.js",
1716
"module": "./dist-es/index.js",

‎packages/util-stream/src/util-stream.integ.spec.ts

+75
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import { Lambda } from "@aws-sdk/client-lambda";
2+
import { HttpResponse } from "@aws-sdk/protocol-http";
3+
import { HttpRequest as IHttpRequest } from "@aws-sdk/types";
4+
import { Uint8ArrayBlobAdapter } from "@aws-sdk/util-stream";
5+
import { fromUtf8 } from "@aws-sdk/util-utf8";
6+
import { Readable } from "stream";
27

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

@@ -41,4 +46,74 @@ describe("util-stream", () => {
4146
});
4247
});
4348
});
49+
50+
describe("blob helper integration", () => {
51+
const FunctionName = "echo";
52+
53+
const lambda = new Lambda({
54+
region: "us-west-2",
55+
});
56+
57+
requireRequestsFrom(lambda).toMatch({
58+
method: "POST",
59+
hostname: "lambda.us-west-2.amazonaws.com",
60+
query: {},
61+
headers: {
62+
"content-type": "application/octet-stream",
63+
host: "lambda.us-west-2.amazonaws.com",
64+
},
65+
protocol: "https:",
66+
path: "/2015-03-31/functions/echo/invocations",
67+
});
68+
69+
lambda.config.requestHandler = new (class {
70+
async handle(request: IHttpRequest) {
71+
return {
72+
response: new HttpResponse({
73+
statusCode: 200,
74+
body: typeof request.body === "string" ? fromUtf8(request.body) : Uint8Array.from(request.body),
75+
}),
76+
};
77+
}
78+
})();
79+
80+
it("should allow string as payload blob and allow conversion of output payload blob to string", async () => {
81+
const payload = JSON.stringify({ hello: "world" });
82+
const invoke = await lambda.invoke({ FunctionName, Payload: payload });
83+
expect(JSON.parse(invoke?.Payload?.transformToString() ?? "{}")).toEqual({ hello: "world" });
84+
});
85+
86+
it("should allow Uint8Array as payload blob", async () => {
87+
const payload = Uint8ArrayBlobAdapter.fromString(JSON.stringify({ hello: "world" }));
88+
const invoke = await lambda.invoke({ FunctionName, Payload: payload });
89+
expect(JSON.parse(invoke?.Payload?.transformToString() ?? "{}")).toEqual({ hello: "world" });
90+
});
91+
92+
it("should allow buffer as payload blob", async () => {
93+
// note: Buffer extends Uint8Array
94+
const payload = Buffer.from(Uint8ArrayBlobAdapter.fromString(JSON.stringify({ hello: "world" })));
95+
const invoke = await lambda.invoke({ FunctionName, Payload: payload });
96+
expect(JSON.parse(invoke?.Payload?.transformToString() ?? "{}")).toEqual({ hello: "world" });
97+
});
98+
99+
it("should allow stream as payload blob but not be able to sign it", async () => {
100+
const payload = Readable.from(Buffer.from(Uint8ArrayBlobAdapter.fromString(JSON.stringify({ hello: "world" }))), {
101+
encoding: "utf-8",
102+
});
103+
expect(JSON.parse(await streamToString(payload))).toEqual({ hello: "world" });
104+
await lambda.invoke({ FunctionName, Payload: payload }).catch((e) => {
105+
expect(e.toString()).toContain("InvalidSignatureException");
106+
});
107+
expect.hasAssertions();
108+
});
109+
});
110+
111+
function streamToString(stream: Readable): Promise<string> {
112+
const chunks: any[] = [];
113+
return new Promise((resolve, reject) => {
114+
stream.on("data", (chunk) => chunks.push(Buffer.from(chunk)));
115+
stream.on("error", (err) => reject(err));
116+
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
117+
});
118+
}
44119
});
-228 Bytes
Binary file not shown.

‎packages/util-stream/test/index.mjs

-6
This file was deleted.

‎packages/util-stream/test/setup.ts

-149
This file was deleted.

‎packages/util-stream/test/util-stream-blob.e2e.spec.ts

-56
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.