Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Apr 11, 2023
1 parent f8cf4ff commit f844c7c
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 12 deletions.
4 changes: 4 additions & 0 deletions packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export { deserializeManifest } from './common.js';
import { callMiddleware } from '../middleware/index.js';
import { AstroError, AstroErrorData } from '../errors/index.js';

const clientLocalsSymbol = Symbol.for('astro.locals');

export const pagesVirtualModuleId = '@astrojs-pages-virtual-entry';
export const resolvedPagesVirtualModuleId = '\0' + pagesVirtualModuleId;
const responseSentSymbol = Symbol.for('astro.responseSent');
Expand Down Expand Up @@ -133,6 +135,8 @@ export class App {
}
}

Reflect.set(request, clientLocalsSymbol, {});

// Use the 404 status code for 404.astro components
if (routeData.route === '/404') {
defaultStatus = 404;
Expand Down
2 changes: 0 additions & 2 deletions packages/astro/src/core/app/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { deserializeManifest } from './common.js';
import { App, type MatchOptions } from './index.js';

const clientAddressSymbol = Symbol.for('astro.clientAddress');
const clientLocalsSymbol = Symbol.for('astro.locals');

function createRequestFromNodeRequest(req: NodeIncomingMessage, body?: Uint8Array): Request {
const protocol =
Expand All @@ -27,7 +26,6 @@ function createRequestFromNodeRequest(req: NodeIncomingMessage, body?: Uint8Arra
if (req.socket?.remoteAddress) {
Reflect.set(request, clientAddressSymbol, req.socket.remoteAddress);
}
Reflect.set(request, clientLocalsSymbol, {});
return request;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/render/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export async function renderPage(

let locals = {};
if (apiContext) {
if (typeof apiContext.locals !== 'undefined' && !isValueSerializable(apiContext.locals)) {
if (!isValueSerializable(apiContext.locals)) {
throw new AstroError({
...AstroErrorData.LocalsNotSerializable,
message: AstroErrorData.LocalsNotSerializable.message(ctx.pathname, apiContext.locals),
Expand Down
7 changes: 0 additions & 7 deletions packages/astro/test/fixtures/dev-middeware/src/middleware.js

This file was deleted.

20 changes: 20 additions & 0 deletions packages/astro/test/fixtures/dev-middleware/src/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { sequence } from 'astro/middleware';

/** @type import("astro").AstroMiddlewareHandler */
const first = async (context, resolve) => {
if (context.request.url.endsWith('/lorem')) {
context.locals.name = 'ipsum';
} else {
context.locals.name = 'bar';
}
return await resolve(context);
};

/** @type import("astro").AstroMiddlewareHandler */
const second = async (context, resolve) => {
if (context.request.url.endsWith('/second')) {
context.locals.name = 'second';
}
};

export const onRequest = sequence(first, second);
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ const data = Astro.locals;
</head>
<body>

<p>{data?.foo}</p>
<p>{data?.name}</p>
</body>
</html>
13 changes: 13 additions & 0 deletions packages/astro/test/fixtures/dev-middleware/src/pages/lorem.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
const data = Astro.locals;
---

<html>
<head>
<title>Testing</title>
</head>
<body>

<p>{data?.name}</p>
</body>
</html>
13 changes: 13 additions & 0 deletions packages/astro/test/fixtures/dev-middleware/src/pages/second.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
const data = Astro.locals;
---

<html>
<head>
<title>Testing</title>
</head>
<body>

<p>{data?.name}</p>
</body>
</html>
18 changes: 17 additions & 1 deletion packages/astro/test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
import * as cheerio from 'cheerio';

describe.skip('Middleware API', () => {
describe('Middleware API', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

Expand All @@ -28,5 +28,21 @@ describe.skip('Middleware API', () => {
const $ = cheerio.load(html);
expect($('p').html()).to.equal('bar');
});

it('should change locals data based on URL', async () => {
let html = await fixture.fetch('/').then((res) => res.text());
let $ = cheerio.load(html);
expect($('p').html()).to.equal('bar');

html = await fixture.fetch('/lorem').then((res) => res.text());
$ = cheerio.load(html);
expect($('p').html()).to.equal('ipsum');
});

it('should call a second middleware', async () => {
let html = await fixture.fetch('/second').then((res) => res.text());
let $ = cheerio.load(html);
expect($('p').html()).to.equal('second');
});
});
});
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f844c7c

Please sign in to comment.