add test
This commit is contained in:
parent
89fbeb34b8
commit
ae8bca661a
5 changed files with 29 additions and 5 deletions
6
packages/integrations/node/test/fixtures/locals/src/middleware.ts
vendored
Normal file
6
packages/integrations/node/test/fixtures/locals/src/middleware.ts
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
import { defineMiddleware } from 'astro:middleware';
|
||||
|
||||
export const onRequest = defineMiddleware(({ url, locals }, next) => {
|
||||
if (url.pathname === "/from-astro-middleware") locals.foo = "baz";
|
||||
return next();
|
||||
})
|
|
@ -1,6 +1,6 @@
|
|||
|
||||
export async function post({ locals }) {
|
||||
let out = { ...locals };
|
||||
export async function POST({ locals }) {
|
||||
const out = { ...locals };
|
||||
|
||||
return new Response(JSON.stringify(out), {
|
||||
headers: {
|
||||
|
|
4
packages/integrations/node/test/fixtures/locals/src/pages/from-node-middleware.astro
vendored
Normal file
4
packages/integrations/node/test/fixtures/locals/src/pages/from-node-middleware.astro
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
const { foo } = Astro.locals;
|
||||
---
|
||||
<h1>{foo}</h1>
|
|
@ -15,11 +15,10 @@ describe('API routes', () => {
|
|||
await fixture.build();
|
||||
});
|
||||
|
||||
it('Can render locals in page', async () => {
|
||||
it('Can use locals added by node middleware', async () => {
|
||||
const { handler } = await import('./fixtures/locals/dist/server/entry.mjs');
|
||||
let { req, res, text } = createRequestAndResponse({
|
||||
method: 'POST',
|
||||
url: '/foo',
|
||||
url: '/from-node-middleware',
|
||||
});
|
||||
|
||||
let locals = { foo: 'bar' };
|
||||
|
@ -32,6 +31,21 @@ describe('API routes', () => {
|
|||
expect(html).to.contain('<h1>bar</h1>');
|
||||
});
|
||||
|
||||
it('Can use locals added by astro middleware', async () => {
|
||||
const { handler } = await import('./fixtures/locals/dist/server/entry.mjs');
|
||||
|
||||
const { req, res, text } = createRequestAndResponse({
|
||||
url: '/from-astro-middleware',
|
||||
});
|
||||
|
||||
handler(req, res, () => {});
|
||||
req.send();
|
||||
|
||||
const html = await text();
|
||||
|
||||
expect(html).to.contain('<h1>baz</h1>');
|
||||
});
|
||||
|
||||
it('Can access locals in API', async () => {
|
||||
const { handler } = await import('./fixtures/locals/dist/server/entry.mjs');
|
||||
let { req, res, done } = createRequestAndResponse({
|
||||
|
|
Loading…
Reference in a new issue