From 5d896ff3e1749d8a6b8c8b8083c8661215d80c2a Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 16 Jan 2024 14:12:10 +0100 Subject: [PATCH] test: enable binary response test for all presets (#2078) --- test/tests.ts | 52 ++++++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/test/tests.ts b/test/tests.ts index 8b8bfd228f..c0dd3dbb42 100644 --- a/test/tests.ts +++ b/test/tests.ts @@ -26,6 +26,7 @@ export interface Context { server?: Listener; isDev: boolean; isWorker: boolean; + isLambda: boolean; isIsolated: boolean; supportsEnv: boolean; env: Record; @@ -77,6 +78,7 @@ export async function setupTest( "vercel-edge", "winterjs", ].includes(preset), + isLambda: ["netlify", "aws-lambda"].includes(preset), isIsolated: ["winterjs"].includes(preset), supportsEnv: !["winterjs"].includes(preset), rootDir: fixtureDir, @@ -169,7 +171,10 @@ export function testNitro( ) { let _handler: TestHandler; - async function callHandler(options): Promise { + async function callHandler( + options, + callOpts: { binary?: boolean } = {} + ): Promise { const result = await _handler(options); if (!["Response", "_Response"].includes(result.constructor.name)) { return result as TestHandlerResult; @@ -192,7 +197,9 @@ export function testNitro( } return { - data: destr(await (result as Response).text()), + data: callOpts.binary + ? Buffer.from(await (result as Response).arrayBuffer()) + : destr(await (result as Response).text()), status: result.status, headers, }; @@ -243,26 +250,27 @@ export function testNitro( expect(obj.headers.location).toBe("https://nitro.unjs.io/"); }); - // aws lambda requires buffer responses to be base 64 - const LambdaPresets = new Set(["netlify", "aws-lambda"]); - it.runIf(LambdaPresets.has(ctx.preset))( - "buffer image responses", - async () => { - const { data } = await callHandler({ url: "/icon.png" }); + it("binary response", async () => { + const { data } = await callHandler({ url: "/icon.png" }, { binary: true }); + let buffer: Buffer; + if (ctx.isLambda) { + // TODO: Handle base64 decoding in lambda tests themselves expect(typeof data).toBe("string"); - const buffer = Buffer.from(data, "base64"); - // check if buffer is a png - function isBufferPng(buffer: Buffer) { - return ( - buffer[0] === 0x89 && - buffer[1] === 0x50 && - buffer[2] === 0x4e && - buffer[3] === 0x47 - ); - } - expect(isBufferPng(buffer)).toBe(true); + buffer = Buffer.from(data, "base64"); + } else { + buffer = data; } - ); + // Check if buffer is a png + function isBufferPng(buffer: Buffer) { + return ( + buffer[0] === 0x89 && + buffer[1] === 0x50 && + buffer[2] === 0x4e && + buffer[3] === 0x47 + ); + } + expect(isBufferPng(buffer)).toBe(true); + }); it("render JSX", async () => { const { data } = await callHandler({ url: "/jsx" }); @@ -437,9 +445,7 @@ export function testNitro( const { data } = await callHandler({ url: "/stream", }); - expect(data).toBe( - LambdaPresets.has(ctx.preset) ? btoa("nitroisawesome") : "nitroisawesome" - ); + expect(data).toBe(ctx.isLambda ? btoa("nitroisawesome") : "nitroisawesome"); }); it.skipIf(!ctx.supportsEnv)("config", async () => {