Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle encoded characters when matching routes #5836

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.