Skip to content

Commit

Permalink
fix(core): handle encoded characters when matching routes (#5836)
Browse files Browse the repository at this point in the history
Co-authored-by: Nate Moore <nate@astro.build>
  • Loading branch information
natemoo-re and natemoo-re committed Jan 12, 2023
1 parent ae8a012 commit 63a6ceb
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-kangaroos-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix route matching when path includes special characters
2 changes: 1 addition & 1 deletion packages/astro/src/core/routing/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
44 changes: 44 additions & 0 deletions packages/integrations/node/test/encoded.test.js
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions packages/integrations/node/test/fixtures/encoded/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/nodejs-encoded",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/node": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 什么
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>什么</h1>
17 changes: 16 additions & 1 deletion packages/integrations/node/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
8 changes: 8 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 63a6ceb

Please sign in to comment.