Skip to content

Commit

Permalink
test(netlify): run after
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 23, 2023
1 parent 2ece2a3 commit 8a6ed77
Showing 1 changed file with 77 additions and 72 deletions.
149 changes: 77 additions & 72 deletions test/presets/netlify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,77 +13,82 @@ describe("nitro:preset:netlify", async () => {
},
},
});
testNitro(ctx, async () => {
const { handler } = (await import(
resolve(ctx.outDir, "server/server.mjs")
)) as { handler: Handler };
return async ({ url: rawRelativeUrl, headers, method, body }) => {
// creating new URL object to parse query easier
const url = new URL(`https://example.com${rawRelativeUrl}`);
const queryStringParameters = Object.fromEntries(
url.searchParams.entries()
);
const event: Partial<APIGatewayEvent> = {
resource: "/my/path",
path: url.pathname,
headers: headers || {},
httpMethod: method || "GET",
queryStringParameters,
body: body || "",
testNitro(
ctx,
async () => {
const { handler } = (await import(
resolve(ctx.outDir, "server/server.mjs")
)) as { handler: Handler };
return async ({ url: rawRelativeUrl, headers, method, body }) => {
// creating new URL object to parse query easier
const url = new URL(`https://example.com${rawRelativeUrl}`);
const queryStringParameters = Object.fromEntries(
url.searchParams.entries()
);
const event: Partial<APIGatewayEvent> = {
resource: "/my/path",
path: url.pathname,
headers: headers || {},
httpMethod: method || "GET",
queryStringParameters,
body: body || "",
};
const res = await handler(event, {} as any, () => {});
const resHeaders = { ...res.headers, ...res.multiValueHeaders };
return {
data: destr(res.body),
status: res.statusCode,
headers: resHeaders,
};
};
const res = await handler(event, {} as any, () => {});
const resHeaders = { ...res.headers, ...res.multiValueHeaders };
return {
data: destr(res.body),
status: res.statusCode,
headers: resHeaders,
};
};
});
it("should add route rules - redirects", async () => {
const redirects = await fsp.readFile(
resolve(ctx.rootDir, "dist/_redirects"),
"utf8"
);
/* eslint-disable no-tabs */
expect(redirects).toMatchInlineSnapshot(`
"/rules/nested/override /other 302
/rules/redirect/obj https://nitro.unjs.io/ 301
/rules/nested/* /base 302
/rules/redirect /base 302
/rules/_/cached/noncached /.netlify/functions/server 200
/rules/_/noncached/cached /.netlify/builders/server 200
/rules/_/cached/* /.netlify/builders/server 200
/rules/_/noncached/* /.netlify/functions/server 200
/rules/swr-ttl/* /.netlify/builders/server 200
/rules/swr/* /.netlify/builders/server 200
/rules/isr-ttl/* /.netlify/builders/server 200
/rules/isr/* /.netlify/builders/server 200
/rules/dynamic /.netlify/functions/server 200
/* /.netlify/functions/server 200"
`);
/* eslint-enable no-tabs */
});
it("should add route rules - headers", async () => {
const headers = await fsp.readFile(
resolve(ctx.rootDir, "dist/_headers"),
"utf8"
);
/* eslint-disable no-tabs */
expect(headers).toMatchInlineSnapshot(`
"/rules/headers
cache-control: s-maxage=60
/rules/cors
access-control-allow-origin: *
access-control-allow-methods: GET
access-control-allow-headers: *
access-control-max-age: 0
/rules/nested/*
x-test: test
/build/*
cache-control: public, max-age=3600, immutable
"
`);
/* eslint-enable no-tabs */
});
},
() => {
it("should add route rules - redirects", async () => {
const redirects = await fsp.readFile(
resolve(ctx.rootDir, "dist/_redirects"),
"utf8"
);
/* eslint-disable no-tabs */
expect(redirects).toMatchInlineSnapshot(`
"/rules/nested/override /other 302
/rules/redirect/obj https://nitro.unjs.io/ 301
/rules/nested/* /base 302
/rules/redirect /base 302
/rules/_/cached/noncached /.netlify/functions/server 200
/rules/_/noncached/cached /.netlify/builders/server 200
/rules/_/cached/* /.netlify/builders/server 200
/rules/_/noncached/* /.netlify/functions/server 200
/rules/swr-ttl/* /.netlify/builders/server 200
/rules/swr/* /.netlify/builders/server 200
/rules/isr-ttl/* /.netlify/builders/server 200
/rules/isr/* /.netlify/builders/server 200
/rules/dynamic /.netlify/functions/server 200
/* /.netlify/functions/server 200"
`);
/* eslint-enable no-tabs */
});
it("should add route rules - headers", async () => {
const headers = await fsp.readFile(
resolve(ctx.rootDir, "dist/_headers"),
"utf8"
);
/* eslint-disable no-tabs */
expect(headers).toMatchInlineSnapshot(`
"/rules/headers
cache-control: s-maxage=60
/rules/cors
access-control-allow-origin: *
access-control-allow-methods: GET
access-control-allow-headers: *
access-control-max-age: 0
/rules/nested/*
x-test: test
/build/*
cache-control: public, max-age=3600, immutable
"
`);
/* eslint-enable no-tabs */
});
}
);
});

0 comments on commit 8a6ed77

Please sign in to comment.