|
1 | 1 | 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"; |
2 | 7 |
|
3 | 8 | import { requireRequestsFrom } from "../../../private/aws-util-test/src";
|
4 | 9 |
|
@@ -41,4 +46,74 @@ describe("util-stream", () => {
|
41 | 46 | });
|
42 | 47 | });
|
43 | 48 | });
|
| 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 | + } |
44 | 119 | });
|
0 commit comments