diff options
author | 2024-02-01 18:46:55 +0100 | |
---|---|---|
committer | 2024-02-01 18:46:55 +0100 | |
commit | 33e482f2d0a3db25cf38f9918d3e0ad1a138be63 (patch) | |
tree | 9f145e13fc97182faf7741514b3985d49c335599 /packages/integrations/netlify/test/functions | |
parent | 63dea241d0d9df370a1a1fe7fddcdce58c86c715 (diff) | |
download | astro-33e482f2d0a3db25cf38f9918d3e0ad1a138be63.tar.gz astro-33e482f2d0a3db25cf38f9918d3e0ad1a138be63.tar.zst astro-33e482f2d0a3db25cf38f9918d3e0ad1a138be63.zip |
fix(netlify): pre-rendered 404 pages aren't shown (#143)
Diffstat (limited to 'packages/integrations/netlify/test/functions')
4 files changed, 70 insertions, 0 deletions
diff --git a/packages/integrations/netlify/test/functions/cookies.test.js b/packages/integrations/netlify/test/functions/cookies.test.js index ea8df7980..56a414d17 100644 --- a/packages/integrations/netlify/test/functions/cookies.test.js +++ b/packages/integrations/netlify/test/functions/cookies.test.js @@ -23,4 +23,24 @@ describe('Cookies', () => { expect(resp.headers.get('location')).to.equal('/'); expect(resp.headers.getSetCookie()).to.eql(['foo=foo; HttpOnly', 'bar=bar; HttpOnly']); }); + + it("renders dynamic 404 page", async () => { + const entryURL = new URL( + './fixtures/cookies/.netlify/functions-internal/ssr/ssr.mjs', + import.meta.url + ); + const { default: handler } = await import(entryURL); + const resp = await handler( + new Request('http://example.com/nonexistant-page', { + headers: { + "x-test": "bar" + } + }), + {} + ); + expect(resp.status).to.equal(404); + const text = await resp.text() + expect(text).to.contain("This is my custom 404 page"); + expect(text).to.contain("x-test: bar"); + }) }); diff --git a/packages/integrations/netlify/test/functions/fixtures/cookies/src/pages/404.astro b/packages/integrations/netlify/test/functions/fixtures/cookies/src/pages/404.astro new file mode 100644 index 000000000..9049fa0fb --- /dev/null +++ b/packages/integrations/netlify/test/functions/fixtures/cookies/src/pages/404.astro @@ -0,0 +1,7 @@ +--- +export const prerender = false +const header = Astro.request.headers.get("x-test") +--- + +<p>This is my custom 404 page</p> +<p>x-test: {header}</p>
\ No newline at end of file diff --git a/packages/integrations/netlify/test/functions/fixtures/redirects/src/pages/404.astro b/packages/integrations/netlify/test/functions/fixtures/redirects/src/pages/404.astro new file mode 100644 index 000000000..b9e3eda13 --- /dev/null +++ b/packages/integrations/netlify/test/functions/fixtures/redirects/src/pages/404.astro @@ -0,0 +1,5 @@ +--- +export const prerender = true +--- + +<p>This is my static 404 page</p>
\ No newline at end of file diff --git a/packages/integrations/netlify/test/functions/redirects.test.js b/packages/integrations/netlify/test/functions/redirects.test.js index 3cfa1cfd0..8c1b1fa2e 100644 --- a/packages/integrations/netlify/test/functions/redirects.test.js +++ b/packages/integrations/netlify/test/functions/redirects.test.js @@ -1,5 +1,6 @@ import { loadFixture } from '@astrojs/test-utils'; import { expect } from 'chai'; +import { createServer } from 'http'; describe('SSR - Redirects', () => { let fixture; @@ -25,4 +26,41 @@ describe('SSR - Redirects', () => { } expect(hasErrored).to.equal(true, 'this file should not exist'); }); + + it("renders static 404 page", async () => { + const entryURL = new URL( + './fixtures/redirects/.netlify/functions-internal/ssr/ssr.mjs', + import.meta.url + ); + const { default: handler } = await import(entryURL); + const resp = await handler( + new Request('http://example.com/nonexistant-page', ), + {} + ); + expect(resp.status).to.equal(404); + const text = await resp.text() + expect(text).to.contain("This is my static 404 page"); + }) + + it('does not pass through 404 request', async () => { + let testServerCalls = 0 + const testServer = createServer((req, res) => { + testServerCalls++ + res.writeHead(200) + res.end() + }) + testServer.listen(5678) + const entryURL = new URL( + './fixtures/redirects/.netlify/functions-internal/ssr/ssr.mjs', + import.meta.url + ); + const { default: handler } = await import(entryURL); + const resp = await handler( + new Request('http://localhost:5678/nonexistant-page'), + {} + ); + expect(resp.status).to.equal(404); + expect(testServerCalls).to.equal(0) + testServer.close() + }); }); |