From 63a6ceb38d88331451dca64d0034c7c58e3d26f1 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Thu, 12 Jan 2023 09:44:18 -0600 Subject: [PATCH] fix(core): handle encoded characters when matching routes (#5836) Co-authored-by: Nate Moore --- .changeset/swift-kangaroos-decide.md | 5 +++ packages/astro/src/core/routing/match.ts | 2 +- .../integrations/node/test/encoded.test.js | 44 +++++++++++++++++++ .../node/test/fixtures/encoded/package.json | 9 ++++ .../pages/blog/\344\273\200\344\271\210.md" | 1 + .../src/pages/\344\273\200\344\271\210.astro" | 1 + packages/integrations/node/test/test-utils.js | 17 ++++++- pnpm-lock.yaml | 8 ++++ 8 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 .changeset/swift-kangaroos-decide.md create mode 100644 packages/integrations/node/test/encoded.test.js create mode 100644 packages/integrations/node/test/fixtures/encoded/package.json create mode 100644 "packages/integrations/node/test/fixtures/encoded/src/pages/blog/\344\273\200\344\271\210.md" create mode 100644 "packages/integrations/node/test/fixtures/encoded/src/pages/\344\273\200\344\271\210.astro" diff --git a/.changeset/swift-kangaroos-decide.md b/.changeset/swift-kangaroos-decide.md new file mode 100644 index 000000000000..5569365c7217 --- /dev/null +++ b/.changeset/swift-kangaroos-decide.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix route matching when path includes special characters diff --git a/packages/astro/src/core/routing/match.ts b/packages/astro/src/core/routing/match.ts index cb742a5aa6b5..b7694bec2760 100644 --- a/packages/astro/src/core/routing/match.ts +++ b/packages/astro/src/core/routing/match.ts @@ -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 */ diff --git a/packages/integrations/node/test/encoded.test.js b/packages/integrations/node/test/encoded.test.js new file mode 100644 index 000000000000..162ebd6cc138 --- /dev/null +++ b/packages/integrations/node/test/encoded.test.js @@ -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('什么'); + }); + + 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('什么'); + }); +}); diff --git a/packages/integrations/node/test/fixtures/encoded/package.json b/packages/integrations/node/test/fixtures/encoded/package.json new file mode 100644 index 000000000000..350077973049 --- /dev/null +++ b/packages/integrations/node/test/fixtures/encoded/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/nodejs-encoded", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*", + "@astrojs/node": "workspace:*" + } +} diff --git "a/packages/integrations/node/test/fixtures/encoded/src/pages/blog/\344\273\200\344\271\210.md" "b/packages/integrations/node/test/fixtures/encoded/src/pages/blog/\344\273\200\344\271\210.md" new file mode 100644 index 000000000000..2820cf17ee2d --- /dev/null +++ "b/packages/integrations/node/test/fixtures/encoded/src/pages/blog/\344\273\200\344\271\210.md" @@ -0,0 +1 @@ +# 什么 diff --git "a/packages/integrations/node/test/fixtures/encoded/src/pages/\344\273\200\344\271\210.astro" "b/packages/integrations/node/test/fixtures/encoded/src/pages/\344\273\200\344\271\210.astro" new file mode 100644 index 000000000000..c8473f594ec0 --- /dev/null +++ "b/packages/integrations/node/test/fixtures/encoded/src/pages/\344\273\200\344\271\210.astro" @@ -0,0 +1 @@ +

什么

diff --git a/packages/integrations/node/test/test-utils.js b/packages/integrations/node/test/test-utils.js index d3d7c17bef4b..13e32a5e89c2 100644 --- a/packages/integrations/node/test/test-utils.js +++ b/packages/integrations/node/test/test-utils.js @@ -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; +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1f05719e1e33..a423398faa50 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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:*