fix(core): handle encoded characters when matching routes (#5836)

Co-authored-by: Nate Moore <nate@astro.build>
This commit is contained in:
Nate Moore 2023-01-12 09:44:18 -06:00 committed by GitHub
parent ae8a012a7b
commit 63a6ceb38d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 85 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix route matching when path includes special characters

View file

@ -2,7 +2,7 @@ import type { ManifestData, RouteData } from '../../@types/astro';
/** Find matching route from pathname */
export function matchRoute(pathname: string, manifest: ManifestData): RouteData | undefined {
return manifest.routes.find((route) => route.pattern.test(pathname));
return manifest.routes.find((route) => route.pattern.test(decodeURI(pathname)));
}
/** Find matching static asset from pathname */

View file

@ -0,0 +1,44 @@
import nodejs from '../dist/index.js';
import { loadFixture, createRequestAndResponse } from './test-utils.js';
import { expect } from 'chai';
describe('Encoded Pathname', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/encoded/',
output: 'server',
adapter: nodejs({ mode: 'middleware' }),
});
await fixture.build();
});
it('Can get an Astro file', async () => {
const { handler } = await import('./fixtures/encoded/dist/server/entry.mjs');
let { req, res, text } = createRequestAndResponse({
url: '/什么',
});
handler(req, res);
req.send();
const html = await text();
expect(html).to.include('什么</h1>');
});
it('Can get a Markdown file', async () => {
const { handler } = await import('./fixtures/encoded/dist/server/entry.mjs');
let { req, res, text } = createRequestAndResponse({
url: '/blog/什么',
});
handler(req, res);
req.send();
const html = await text();
expect(html).to.include('什么</h1>');
});
});

View file

@ -0,0 +1,9 @@
{
"name": "@test/nodejs-encoded",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/node": "workspace:*"
}
}

View file

@ -0,0 +1 @@
# 什么

View file

@ -0,0 +1 @@
<h1>什么</h1>

View file

@ -28,7 +28,13 @@ export function createRequestAndResponse(reqOptions) {
let done = toPromise(res);
return { req, res, done };
// Get the response as text
const text = async () => {
let chunks = await done;
return buffersToString(chunks);
};
return { req, res, done, text };
}
export function toPromise(res) {
@ -48,3 +54,12 @@ export function toPromise(res) {
});
});
}
export function buffersToString(buffers) {
let decoder = new TextDecoder();
let str = '';
for (const buffer of buffers) {
str += decoder.decode(buffer);
}
return str;
}

View file

@ -3089,6 +3089,14 @@ importers:
'@astrojs/node': link:../../..
astro: link:../../../../../astro
packages/integrations/node/test/fixtures/encoded:
specifiers:
'@astrojs/node': workspace:*
astro: workspace:*
dependencies:
'@astrojs/node': link:../../..
astro: link:../../../../../astro
packages/integrations/node/test/fixtures/node-middleware:
specifiers:
'@astrojs/node': workspace:*