Skip to content

Commit

Permalink
test: enable binary response test for all presets (#2078)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Jan 16, 2024
1 parent b4cf68a commit 5d896ff
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface Context {
server?: Listener;
isDev: boolean;
isWorker: boolean;
isLambda: boolean;
isIsolated: boolean;
supportsEnv: boolean;
env: Record<string, string>;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -169,7 +171,10 @@ export function testNitro(
) {
let _handler: TestHandler;

async function callHandler(options): Promise<TestHandlerResult> {
async function callHandler(
options,
callOpts: { binary?: boolean } = {}
): Promise<TestHandlerResult> {
const result = await _handler(options);
if (!["Response", "_Response"].includes(result.constructor.name)) {
return result as TestHandlerResult;
Expand All @@ -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,
};
Expand Down Expand Up @@ -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" });
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit 5d896ff

Please sign in to comment.