-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding test case for integration supplied context, to also show up on…
… 404 return from component
- Loading branch information
1 parent
e3f40ea
commit 83c3bdd
Showing
6 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import assert from 'node:assert/strict'; | ||
import { after, before, describe, it } from 'node:test'; | ||
import * as cheerio from 'cheerio'; | ||
import { loadFixture } from './test-utils.js'; | ||
|
||
describe('Custom 404 locals', () => { | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/custom-404-locals/', | ||
site: 'http://example.com', | ||
}); | ||
}); | ||
|
||
describe('dev', () => { | ||
let devServer; | ||
let $; | ||
|
||
before(async () => { | ||
devServer = await fixture.startDevServer(); | ||
}); | ||
|
||
after(async () => { | ||
await devServer.stop(); | ||
}); | ||
|
||
it('renders /', async () => { | ||
const html = await fixture.fetch('/').then((res) => res.text()); | ||
$ = cheerio.load(html); | ||
|
||
assert.equal($('h1').text(), 'Home'); | ||
}); | ||
|
||
it('renders 404 for /a', async () => { | ||
const res = await fixture.fetch('/a'); | ||
assert.equal(res.status, 404); | ||
|
||
const html = await res.text(); | ||
$ = cheerio.load(html); | ||
|
||
assert.equal($('h1').text(), 'Page not found'); | ||
assert.equal($('p.message').text(), 'This 404 is a dynamic HTML file.'); | ||
assert.equal($('p.runtime').text(), 'locals'); | ||
}); | ||
|
||
it('renders 404 for /404-return', async () => { | ||
const res = await fixture.fetch('/404-return'); | ||
assert.equal(res.status, 404); | ||
|
||
const html = await res.text(); | ||
$ = cheerio.load(html); | ||
|
||
assert.equal($('h1').text(), 'Page not found'); | ||
assert.equal($('p.message').text(), 'This 404 is a dynamic HTML file.'); | ||
assert.equal($('p.runtime').text(), 'locals'); | ||
}); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
packages/astro/test/fixtures/custom-404-locals/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { defineConfig } from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
integrations: [{ | ||
name: 'locals-integration', | ||
hooks: { | ||
'astro:server:setup': ({ server }) => { | ||
server.middlewares.use(async function middleware(req, res, next) { | ||
const clientLocalsSymbol = Symbol.for('astro.locals'); | ||
Reflect.set(req, clientLocalsSymbol, { | ||
runtime: 'locals' | ||
}); | ||
next(); | ||
}); | ||
}, | ||
}, | ||
}] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@test/custom-404-locals", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/astro/test/fixtures/custom-404-locals/src/pages/404-return.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
return new Response(null, { status: 404 }); | ||
--- | ||
|
||
I should never load |
13 changes: 13 additions & 0 deletions
13
packages/astro/test/fixtures/custom-404-locals/src/pages/404.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
const runtime = Astro.locals.runtime | ||
--- | ||
<html lang="en"> | ||
<head> | ||
<title>Not Found - Custom 404</title> | ||
</head> | ||
<body> | ||
<h1>Page not found</h1> | ||
<p class="message">This 404 is a dynamic HTML file.</p> | ||
<p class="runtime">{runtime}</p> | ||
</body> | ||
</html> |
11 changes: 11 additions & 0 deletions
11
packages/astro/test/fixtures/custom-404-locals/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
--- | ||
|
||
<html lang="en"> | ||
<head> | ||
<title>Custom 404</title> | ||
</head> | ||
<body> | ||
<h1>Home</h1> | ||
</body> | ||
</html> |