diff options
author | 2023-10-16 14:10:41 +0000 | |
---|---|---|
committer | 2023-10-16 14:10:41 +0000 | |
commit | 3d1329414e507345b19e1dee81fe1bbff4eb3ae0 (patch) | |
tree | 69c822b1dc12f177b4919337109e6300a04df91d /packages/integrations/netlify/test/functions | |
parent | 63977f5e438062b7e9302b8feff41b662bfa6231 (diff) | |
download | astro-3d1329414e507345b19e1dee81fe1bbff4eb3ae0.tar.gz astro-3d1329414e507345b19e1dee81fe1bbff4eb3ae0.tar.zst astro-3d1329414e507345b19e1dee81fe1bbff4eb3ae0.zip |
[ci] format
Diffstat (limited to 'packages/integrations/netlify/test/functions')
9 files changed, 318 insertions, 263 deletions
diff --git a/packages/integrations/netlify/test/functions/404.test.js b/packages/integrations/netlify/test/functions/404.test.js index 1782507db..a1f792ce0 100644 --- a/packages/integrations/netlify/test/functions/404.test.js +++ b/packages/integrations/netlify/test/functions/404.test.js @@ -1,19 +1,21 @@ -import { expect } from 'chai'; -import fs from 'fs/promises'; -import { cli } from './test-utils.js'; -import { fileURLToPath } from 'url'; +import { expect } from "chai"; +import fs from "fs/promises"; +import { cli } from "./test-utils.js"; +import { fileURLToPath } from "url"; -const root = new URL('./fixtures/404/', import.meta.url).toString(); +const root = new URL("./fixtures/404/", import.meta.url).toString(); -describe('404 page', () => { +describe("404 page", () => { + before(async () => { + await cli("build", "--root", fileURLToPath(root)); + }); - before(async () => { - await cli('build', '--root', fileURLToPath(root)); - }); - - it('404 route is included in the redirect file', async () => { - const redir = await fs.readFile(new URL('./dist/_redirects', root), 'utf-8'); - const expr = new RegExp('/* /.netlify/functions/entry 404'); - expect(redir).to.match(expr); - }); + it("404 route is included in the redirect file", async () => { + const redir = await fs.readFile( + new URL("./dist/_redirects", root), + "utf-8", + ); + const expr = new RegExp("/* /.netlify/functions/entry 404"); + expect(redir).to.match(expr); + }); }); diff --git a/packages/integrations/netlify/test/functions/base64-response.test.js b/packages/integrations/netlify/test/functions/base64-response.test.js index 520eaf646..07c1bbafe 100644 --- a/packages/integrations/netlify/test/functions/base64-response.test.js +++ b/packages/integrations/netlify/test/functions/base64-response.test.js @@ -1,52 +1,55 @@ -import { expect } from 'chai'; -import { cli } from './test-utils.js'; -import { fileURLToPath } from 'url'; +import { expect } from "chai"; +import { cli } from "./test-utils.js"; +import { fileURLToPath } from "url"; -const root = new URL('./fixtures/base64-response/', import.meta.url).toString(); +const root = new URL("./fixtures/base64-response/", import.meta.url).toString(); -describe('Base64 Responses', () => { +describe("Base64 Responses", () => { + before(async () => { + await cli("build", "--root", fileURLToPath(root)); + }); - before(async () => { - await cli('build', '--root', fileURLToPath(root)); - }); + it("Can return base64 encoded strings", async () => { + const entryURL = new URL( + "./fixtures/base64-response/.netlify/functions-internal/entry.mjs", + import.meta.url, + ); + const { handler } = await import(entryURL); + const resp = await handler({ + httpMethod: "GET", + headers: {}, + rawUrl: "http://example.com/image", + body: "{}", + isBase64Encoded: false, + }); + expect(resp.statusCode, "successful response").to.equal(200); + expect(resp.isBase64Encoded, "includes isBase64Encoded flag").to.be.true; - it('Can return base64 encoded strings', async () => { - const entryURL = new URL( - './fixtures/base64-response/.netlify/functions-internal/entry.mjs', - import.meta.url - ); - const { handler } = await import(entryURL); - const resp = await handler({ - httpMethod: 'GET', - headers: {}, - rawUrl: 'http://example.com/image', - body: '{}', - isBase64Encoded: false, - }); - expect(resp.statusCode, 'successful response').to.equal(200); - expect(resp.isBase64Encoded, 'includes isBase64Encoded flag').to.be.true; + const buffer = Buffer.from(resp.body, "base64"); + expect(buffer.toString(), "decoded base64 string matches").to.equal( + "base64 test string", + ); + }); - const buffer = Buffer.from(resp.body, 'base64'); - expect(buffer.toString(), 'decoded base64 string matches').to.equal('base64 test string'); - }); + it("Can define custom binaryMediaTypes", async () => { + const entryURL = new URL( + "./fixtures/base64-response/.netlify/functions-internal/entry.mjs", + import.meta.url, + ); + const { handler } = await import(entryURL); + const resp = await handler({ + httpMethod: "GET", + headers: {}, + rawUrl: "http://example.com/font", + body: "{}", + isBase64Encoded: false, + }); + expect(resp.statusCode, "successful response").to.equal(200); + expect(resp.isBase64Encoded, "includes isBase64Encoded flag").to.be.true; - it('Can define custom binaryMediaTypes', async () => { - const entryURL = new URL( - './fixtures/base64-response/.netlify/functions-internal/entry.mjs', - import.meta.url - ); - const { handler } = await import(entryURL); - const resp = await handler({ - httpMethod: 'GET', - headers: {}, - rawUrl: 'http://example.com/font', - body: '{}', - isBase64Encoded: false, - }); - expect(resp.statusCode, 'successful response').to.equal(200); - expect(resp.isBase64Encoded, 'includes isBase64Encoded flag').to.be.true; - - const buffer = Buffer.from(resp.body, 'base64'); - expect(buffer.toString(), 'decoded base64 string matches').to.equal('base64 test font'); - }); + const buffer = Buffer.from(resp.body, "base64"); + expect(buffer.toString(), "decoded base64 string matches").to.equal( + "base64 test font", + ); + }); }); diff --git a/packages/integrations/netlify/test/functions/builders.test.js b/packages/integrations/netlify/test/functions/builders.test.js index e927527a1..281c4f3da 100644 --- a/packages/integrations/netlify/test/functions/builders.test.js +++ b/packages/integrations/netlify/test/functions/builders.test.js @@ -1,26 +1,26 @@ -import { expect } from 'chai'; -import { cli } from './test-utils.js'; -import { fileURLToPath } from 'url'; +import { expect } from "chai"; +import { cli } from "./test-utils.js"; +import { fileURLToPath } from "url"; -const root = new URL('./fixtures/builders/', import.meta.url).toString(); +const root = new URL("./fixtures/builders/", import.meta.url).toString(); -describe('Builders', () => { - before(async () => { - await cli('build', '--root', fileURLToPath(root)); - }); +describe("Builders", () => { + before(async () => { + await cli("build", "--root", fileURLToPath(root)); + }); - it('A route can set builders ttl', async () => { - const entryURL = new URL( - './fixtures/builders/.netlify/functions-internal/entry.mjs', - import.meta.url - ); - const { handler } = await import(entryURL); - const resp = await handler({ - httpMethod: 'GET', - headers: {}, - rawUrl: 'http://example.com/', - isBase64Encoded: false, - }); - expect(resp.ttl).to.equal(45); - }); + it("A route can set builders ttl", async () => { + const entryURL = new URL( + "./fixtures/builders/.netlify/functions-internal/entry.mjs", + import.meta.url, + ); + const { handler } = await import(entryURL); + const resp = await handler({ + httpMethod: "GET", + headers: {}, + rawUrl: "http://example.com/", + isBase64Encoded: false, + }); + expect(resp.ttl).to.equal(45); + }); }); diff --git a/packages/integrations/netlify/test/functions/cookies.test.js b/packages/integrations/netlify/test/functions/cookies.test.js index 328294d10..05727207e 100644 --- a/packages/integrations/netlify/test/functions/cookies.test.js +++ b/packages/integrations/netlify/test/functions/cookies.test.js @@ -1,31 +1,31 @@ -import { expect } from 'chai'; -import { cli } from './test-utils.js'; -import { fileURLToPath } from 'url'; +import { expect } from "chai"; +import { cli } from "./test-utils.js"; +import { fileURLToPath } from "url"; -const root = new URL('./fixtures/cookies/', import.meta.url).toString(); +const root = new URL("./fixtures/cookies/", import.meta.url).toString(); -describe('Cookies', () => { - before(async () => { - await cli('build', '--root', fileURLToPath(root)); - }); +describe("Cookies", () => { + before(async () => { + await cli("build", "--root", fileURLToPath(root)); + }); - it('Can set multiple', async () => { - const entryURL = new URL( - './fixtures/cookies/.netlify/functions-internal/entry.mjs', - import.meta.url - ); - const { handler } = await import(entryURL); - const resp = await handler({ - httpMethod: 'POST', - headers: {}, - rawUrl: 'http://example.com/login', - body: '{}', - isBase64Encoded: false, - }); - expect(resp.statusCode).to.equal(301); - expect(resp.headers.location).to.equal('/'); - expect(resp.multiValueHeaders).to.be.deep.equal({ - 'set-cookie': ['foo=foo; HttpOnly', 'bar=bar; HttpOnly'], - }); - }); + it("Can set multiple", async () => { + const entryURL = new URL( + "./fixtures/cookies/.netlify/functions-internal/entry.mjs", + import.meta.url, + ); + const { handler } = await import(entryURL); + const resp = await handler({ + httpMethod: "POST", + headers: {}, + rawUrl: "http://example.com/login", + body: "{}", + isBase64Encoded: false, + }); + expect(resp.statusCode).to.equal(301); + expect(resp.headers.location).to.equal("/"); + expect(resp.multiValueHeaders).to.be.deep.equal({ + "set-cookie": ["foo=foo; HttpOnly", "bar=bar; HttpOnly"], + }); + }); }); diff --git a/packages/integrations/netlify/test/functions/dynamic-route.test.js b/packages/integrations/netlify/test/functions/dynamic-route.test.js index 2e20454e6..69d25a11e 100644 --- a/packages/integrations/netlify/test/functions/dynamic-route.test.js +++ b/packages/integrations/netlify/test/functions/dynamic-route.test.js @@ -1,24 +1,36 @@ -import { expect } from 'chai'; -import { cli } from './test-utils.js'; -import { fileURLToPath } from 'url'; -import fs from 'fs/promises'; +import { expect } from "chai"; +import { cli } from "./test-utils.js"; +import { fileURLToPath } from "url"; +import fs from "fs/promises"; -const root = new URL('./fixtures/dynamic-route/', import.meta.url).toString(); +const root = new URL("./fixtures/dynamic-route/", import.meta.url).toString(); -describe('Dynamic pages', () => { - before(async () => { - await cli('build', '--root', fileURLToPath(root)); - }); +describe("Dynamic pages", () => { + before(async () => { + await cli("build", "--root", fileURLToPath(root)); + }); - it('Dynamic pages are included in the redirects file', async () => { - const redir = await fs.readFile(new URL('./dist/_redirects', root), 'utf-8'); - expect(redir).to.match(/\/products\/:id/); - }); + it("Dynamic pages are included in the redirects file", async () => { + const redir = await fs.readFile( + new URL("./dist/_redirects", root), + "utf-8", + ); + expect(redir).to.match(/\/products\/:id/); + }); - it('Prerendered routes are also included using placeholder syntax', async () => { - const redir = await fs.readFile(new URL('./dist/_redirects', root), 'utf-8'); - expect(redir).to.include('/pets/:cat /pets/:cat/index.html 200'); - expect(redir).to.include('/pets/:dog /pets/:dog/index.html 200'); - expect(redir).to.include('/pets /.netlify/functions/entry 200'); - }); + it("Prerendered routes are also included using placeholder syntax", async () => { + const redir = await fs.readFile( + new URL("./dist/_redirects", root), + "utf-8", + ); + expect(redir).to.include( + "/pets/:cat /pets/:cat/index.html 200", + ); + expect(redir).to.include( + "/pets/:dog /pets/:dog/index.html 200", + ); + expect(redir).to.include( + "/pets /.netlify/functions/entry 200", + ); + }); }); diff --git a/packages/integrations/netlify/test/functions/edge-middleware.test.js b/packages/integrations/netlify/test/functions/edge-middleware.test.js index 7e51b20da..90bcd69c7 100644 --- a/packages/integrations/netlify/test/functions/edge-middleware.test.js +++ b/packages/integrations/netlify/test/functions/edge-middleware.test.js @@ -1,20 +1,32 @@ -import { fileURLToPath } from 'url'; -import { cli } from './test-utils.js'; -import fs from 'fs/promises'; -import { expect } from 'chai'; +import { fileURLToPath } from "url"; +import { cli } from "./test-utils.js"; +import fs from "fs/promises"; +import { expect } from "chai"; -describe('Middleware', () => { - it('with edge handle file, should successfully build the middleware', async () => { - const root = new URL('./fixtures/middleware-with-handler-file/', import.meta.url).toString(); - await cli('build', '--root', fileURLToPath(root)); - const contents = await fs.readFile(new URL('./.netlify/edge-functions/edgeMiddleware.js', root), 'utf-8'); - expect(contents.includes('"Hello world"')).to.be.true; - }); +describe("Middleware", () => { + it("with edge handle file, should successfully build the middleware", async () => { + const root = new URL( + "./fixtures/middleware-with-handler-file/", + import.meta.url, + ).toString(); + await cli("build", "--root", fileURLToPath(root)); + const contents = await fs.readFile( + new URL("./.netlify/edge-functions/edgeMiddleware.js", root), + "utf-8", + ); + expect(contents.includes('"Hello world"')).to.be.true; + }); - it('without edge handle file, should successfully build the middleware', async () => { - const root = new URL('./fixtures/middleware-without-handler-file/', import.meta.url).toString(); - await cli('build', '--root', fileURLToPath(root)); - const contents = await fs.readFile(new URL('./.netlify/edge-functions/edgeMiddleware.js', root), 'utf-8'); - expect(contents.includes('"Hello world"')).to.be.false; - }); + it("without edge handle file, should successfully build the middleware", async () => { + const root = new URL( + "./fixtures/middleware-without-handler-file/", + import.meta.url, + ).toString(); + await cli("build", "--root", fileURLToPath(root)); + const contents = await fs.readFile( + new URL("./.netlify/edge-functions/edgeMiddleware.js", root), + "utf-8", + ); + expect(contents.includes('"Hello world"')).to.be.false; + }); }); diff --git a/packages/integrations/netlify/test/functions/prerender.test.js b/packages/integrations/netlify/test/functions/prerender.test.js index 8acc5a519..61af252cb 100644 --- a/packages/integrations/netlify/test/functions/prerender.test.js +++ b/packages/integrations/netlify/test/functions/prerender.test.js @@ -1,53 +1,73 @@ -import { expect } from 'chai'; -import fs from 'fs/promises'; -import { cli } from './test-utils.js'; -import { fileURLToPath } from 'url'; - -const root = new URL('./fixtures/prerender/', import.meta.url).toString(); - -describe('Mixed Prerendering with SSR', () => { - before(async () => { - process.env.PRERENDER = true; - await cli('build', '--root', fileURLToPath(root)); - }); - - after(() => { - delete process.env.PRERENDER; - }); - - it('Wildcard 404 is sorted last', async () => { - const redir = await fs.readFile(new URL('./dist/_redirects', root), 'utf-8'); - const baseRouteIndex = redir.indexOf('/ /.netlify/functions/entry 200'); - const oneRouteIndex = redir.indexOf('/one /one/index.html 200'); - const fourOhFourWildCardIndex = redir.indexOf('/* /.netlify/functions/entry 404'); - - expect(oneRouteIndex).to.not.be.equal(-1); - expect(fourOhFourWildCardIndex).to.be.greaterThan(baseRouteIndex); - expect(fourOhFourWildCardIndex).to.be.greaterThan(oneRouteIndex); - }); +import { expect } from "chai"; +import fs from "fs/promises"; +import { cli } from "./test-utils.js"; +import { fileURLToPath } from "url"; + +const root = new URL("./fixtures/prerender/", import.meta.url).toString(); + +describe("Mixed Prerendering with SSR", () => { + before(async () => { + process.env.PRERENDER = true; + await cli("build", "--root", fileURLToPath(root)); + }); + + after(() => { + delete process.env.PRERENDER; + }); + + it("Wildcard 404 is sorted last", async () => { + const redir = await fs.readFile( + new URL("./dist/_redirects", root), + "utf-8", + ); + const baseRouteIndex = redir.indexOf( + "/ /.netlify/functions/entry 200", + ); + const oneRouteIndex = redir.indexOf( + "/one /one/index.html 200", + ); + const fourOhFourWildCardIndex = redir.indexOf( + "/* /.netlify/functions/entry 404", + ); + + expect(oneRouteIndex).to.not.be.equal(-1); + expect(fourOhFourWildCardIndex).to.be.greaterThan(baseRouteIndex); + expect(fourOhFourWildCardIndex).to.be.greaterThan(oneRouteIndex); + }); }); -describe('Mixed Hybrid rendering with SSR', () => { - before(async () => { - process.env.PRERENDER = false; - process.env.ASTRO_OUTPUT = 'hybrid'; - await cli('build', '--root', fileURLToPath(root)); - }); - - after(() => { - delete process.env.PRERENDER; - }); - - it('outputs a correct redirect file', async () => { - const redir = await fs.readFile(new URL('./dist/_redirects', root), 'utf-8'); - const baseRouteIndex = redir.indexOf('/one /.netlify/functions/entry 200'); - const rootRouteIndex = redir.indexOf('/ /index.html 200'); - const fourOhFourIndex = redir.indexOf('/404 /404.html 200'); - const imageEndpoint = redir.indexOf('/_image /.netlify/functions/entry 200'); - - expect(rootRouteIndex).to.not.be.equal(-1); - expect(baseRouteIndex).to.not.be.equal(-1); - expect(fourOhFourIndex).to.not.be.equal(-1); - expect(imageEndpoint).to.not.be.equal(-1); - }); +describe("Mixed Hybrid rendering with SSR", () => { + before(async () => { + process.env.PRERENDER = false; + process.env.ASTRO_OUTPUT = "hybrid"; + await cli("build", "--root", fileURLToPath(root)); + }); + + after(() => { + delete process.env.PRERENDER; + }); + + it("outputs a correct redirect file", async () => { + const redir = await fs.readFile( + new URL("./dist/_redirects", root), + "utf-8", + ); + const baseRouteIndex = redir.indexOf( + "/one /.netlify/functions/entry 200", + ); + const rootRouteIndex = redir.indexOf( + "/ /index.html 200", + ); + const fourOhFourIndex = redir.indexOf( + "/404 /404.html 200", + ); + const imageEndpoint = redir.indexOf( + "/_image /.netlify/functions/entry 200", + ); + + expect(rootRouteIndex).to.not.be.equal(-1); + expect(baseRouteIndex).to.not.be.equal(-1); + expect(fourOhFourIndex).to.not.be.equal(-1); + expect(imageEndpoint).to.not.be.equal(-1); + }); }); diff --git a/packages/integrations/netlify/test/functions/redirects.test.js b/packages/integrations/netlify/test/functions/redirects.test.js index 855378ad4..43fc0cd5d 100644 --- a/packages/integrations/netlify/test/functions/redirects.test.js +++ b/packages/integrations/netlify/test/functions/redirects.test.js @@ -1,50 +1,56 @@ -import { expect } from 'chai'; -import fs from 'fs/promises'; -import { cli } from './test-utils.js'; -import { fileURLToPath } from 'url'; +import { expect } from "chai"; +import fs from "fs/promises"; +import { cli } from "./test-utils.js"; +import { fileURLToPath } from "url"; -const root = new URL('../functions/fixtures/redirects/', import.meta.url).toString(); +const root = new URL( + "../functions/fixtures/redirects/", + import.meta.url, +).toString(); -describe('SSG - Redirects', () => { - before(async () => { - await cli('build', '--root', fileURLToPath(root)); - }); +describe("SSG - Redirects", () => { + before(async () => { + await cli("build", "--root", fileURLToPath(root)); + }); - it('Creates a redirects file', async () => { - let redirects = await fs.readFile(new URL('./dist/_redirects', root), 'utf-8'); - let parts = redirects.split(/\s+/); - expect(parts).to.deep.equal([ - '/other', - '/', - '301', - // This uses the dynamic Astro.redirect, so we don't know that it's a redirect - // until runtime. This is correct! - '/nope', - '/.netlify/functions/entry', - '200', - '/', - '/.netlify/functions/entry', - '200', + it("Creates a redirects file", async () => { + let redirects = await fs.readFile( + new URL("./dist/_redirects", root), + "utf-8", + ); + let parts = redirects.split(/\s+/); + expect(parts).to.deep.equal([ + "/other", + "/", + "301", + // This uses the dynamic Astro.redirect, so we don't know that it's a redirect + // until runtime. This is correct! + "/nope", + "/.netlify/functions/entry", + "200", + "/", + "/.netlify/functions/entry", + "200", - // Image endpoint - '/_image', - '/.netlify/functions/entry', - '200', + // Image endpoint + "/_image", + "/.netlify/functions/entry", + "200", - // A real route - '/team/articles/*', - '/.netlify/functions/entry', - '200', - ]); - expect(redirects).to.matchSnapshot(); - }); + // A real route + "/team/articles/*", + "/.netlify/functions/entry", + "200", + ]); + expect(redirects).to.matchSnapshot(); + }); - it('Does not create .html files', async () => { - try { - await fixture.readFile('/other/index.html'); - expect(false).to.equal(true, 'this file should not exist'); - } catch { - expect(true).to.equal(true); - } - }); + it("Does not create .html files", async () => { + try { + await fixture.readFile("/other/index.html"); + expect(false).to.equal(true, "this file should not exist"); + } catch { + expect(true).to.equal(true); + } + }); }); diff --git a/packages/integrations/netlify/test/functions/test-utils.js b/packages/integrations/netlify/test/functions/test-utils.js index c977af42e..46128095e 100644 --- a/packages/integrations/netlify/test/functions/test-utils.js +++ b/packages/integrations/netlify/test/functions/test-utils.js @@ -1,34 +1,34 @@ // @ts-check -import { fileURLToPath } from 'node:url'; +import { fileURLToPath } from "node:url"; -export * from '../test-utils.js'; +export * from "../test-utils.js"; /** * * @returns {import('astro').AstroIntegration} */ export function testIntegration({ setEntryPoints } = {}) { - return { - name: '@astrojs/netlify/test-integration', - hooks: { - 'astro:config:setup': ({ updateConfig }) => { - updateConfig({ - vite: { - resolve: { - alias: { - '@astrojs/netlify/netlify-functions.js': fileURLToPath( - new URL('../../dist/netlify-functions.js', import.meta.url) - ), - }, - }, - }, - }); - }, - 'astro:build:ssr': ({ entryPoints }) => { - if (entryPoints.size) { - setEntryPoints(entryPoints); - } - }, - }, - }; + return { + name: "@astrojs/netlify/test-integration", + hooks: { + "astro:config:setup": ({ updateConfig }) => { + updateConfig({ + vite: { + resolve: { + alias: { + "@astrojs/netlify/netlify-functions.js": fileURLToPath( + new URL("../../dist/netlify-functions.js", import.meta.url), + ), + }, + }, + }, + }); + }, + "astro:build:ssr": ({ entryPoints }) => { + if (entryPoints.size) { + setEntryPoints(entryPoints); + } + }, + }, + }; } |