From 6449f8a21d175f43e48b1bbffda35e21999c6406 Mon Sep 17 00:00:00 2001 From: Arkadiusz Sygulski Date: Wed, 26 Jul 2023 17:29:45 +0200 Subject: [PATCH 1/3] Add failing test case --- test/event.test.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/event.test.ts b/test/event.test.ts index a98731dc..b00523d0 100644 --- a/test/event.test.ts +++ b/test/event.test.ts @@ -102,4 +102,19 @@ describe("Event", () => { expect(result.text).toBe("200"); }); + + it("can read path with URL", async () => { + app.use( + "/", + eventHandler((event) => { + expect(event.path).toBe('/?url=https://example.com'); + return "200"; + }) + ); + + const result = await request + .get('/?url=https://example.com'); + + expect(result.text).toBe("200"); + }) }); From 6c6b03b82462d3d13461b76c258a66c0e2e1dccd Mon Sep 17 00:00:00 2001 From: Arkadiusz Sygulski Date: Wed, 26 Jul 2023 17:51:21 +0200 Subject: [PATCH 2/3] Do not remove double slashes from query --- src/event/event.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/event/event.ts b/src/event/event.ts index 58240e5a..483d90a9 100644 --- a/src/event/event.ts +++ b/src/event/event.ts @@ -61,7 +61,14 @@ export class H3Event implements Pick { get path() { if (!this._path) { - this._path = this._originalPath.replace(DOUBLE_SLASH_RE, "/"); + const hasQuery = this._originalPath.includes('?'); + + if (hasQuery) { + const [basePath, query] = this._originalPath.split('?'); + this._path = basePath.replace(DOUBLE_SLASH_RE, "/") + '?' + query; + } else { + this._path = this._originalPath.replace(DOUBLE_SLASH_RE, "/"); + } } return this._path; } From 155b63fe8753df73ff7ce0820a7c7381f5184d8c Mon Sep 17 00:00:00 2001 From: Arkadiusz Sygulski Date: Wed, 26 Jul 2023 17:52:07 +0200 Subject: [PATCH 3/3] Run linter --- src/event/event.ts | 6 +++--- test/event.test.ts | 7 +++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/event/event.ts b/src/event/event.ts index 483d90a9..5fd4dad1 100644 --- a/src/event/event.ts +++ b/src/event/event.ts @@ -61,11 +61,11 @@ export class H3Event implements Pick { get path() { if (!this._path) { - const hasQuery = this._originalPath.includes('?'); + const hasQuery = this._originalPath.includes("?"); if (hasQuery) { - const [basePath, query] = this._originalPath.split('?'); - this._path = basePath.replace(DOUBLE_SLASH_RE, "/") + '?' + query; + const [basePath, query] = this._originalPath.split("?"); + this._path = basePath.replace(DOUBLE_SLASH_RE, "/") + "?" + query; } else { this._path = this._originalPath.replace(DOUBLE_SLASH_RE, "/"); } diff --git a/test/event.test.ts b/test/event.test.ts index b00523d0..f468c8a2 100644 --- a/test/event.test.ts +++ b/test/event.test.ts @@ -107,14 +107,13 @@ describe("Event", () => { app.use( "/", eventHandler((event) => { - expect(event.path).toBe('/?url=https://example.com'); + expect(event.path).toBe("/?url=https://example.com"); return "200"; }) ); - const result = await request - .get('/?url=https://example.com'); + const result = await request.get("/?url=https://example.com"); expect(result.text).toBe("200"); - }) + }); });