fix(core): handle encoded characters when matching routes (#5836)
Co-authored-by: Nate Moore <nate@astro.build>
This commit is contained in:
parent
ae8a012a7b
commit
63a6ceb38d
8 changed files with 85 additions and 2 deletions
5
.changeset/swift-kangaroos-decide.md
Normal file
5
.changeset/swift-kangaroos-decide.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fix route matching when path includes special characters
|
|
@ -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 */
|
||||
|
|
44
packages/integrations/node/test/encoded.test.js
Normal file
44
packages/integrations/node/test/encoded.test.js
Normal 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>');
|
||||
});
|
||||
});
|
9
packages/integrations/node/test/fixtures/encoded/package.json
vendored
Normal file
9
packages/integrations/node/test/fixtures/encoded/package.json
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"name": "@test/nodejs-encoded",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"astro": "workspace:*",
|
||||
"@astrojs/node": "workspace:*"
|
||||
}
|
||||
}
|
1
packages/integrations/node/test/fixtures/encoded/src/pages/blog/什么.md
vendored
Normal file
1
packages/integrations/node/test/fixtures/encoded/src/pages/blog/什么.md
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
# 什么
|
1
packages/integrations/node/test/fixtures/encoded/src/pages/什么.astro
vendored
Normal file
1
packages/integrations/node/test/fixtures/encoded/src/pages/什么.astro
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
<h1>什么</h1>
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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:*
|
||||
|
|
Loading…
Reference in a new issue