Skip to content

feat: update how request.url is derived #506

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
24 changes: 24 additions & 0 deletions packages/remix-fastify/__tests__/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@

let response = await app.inject("/foo/bar");

expect(response.body).toBe("URL: /foo/bar");

Check failure on line 113 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / ubuntu-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] handles requests

AssertionError: expected '{"statusCode":500,"error":"Internal S…' to be 'URL: /foo/bar' // Object.is equality Expected: "URL: /foo/bar" Received: "{"statusCode":500,"error":"Internal Server Error","message":"req is not defined"}" ❯ __tests__/server.test.ts:113:31

Check failure on line 113 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / ubuntu-latest | latest

__tests__/server.test.ts > [react-router] fastify createRequestHandler > [react-router] basic requests > [react-router] handles requests

AssertionError: expected '{"statusCode":500,"error":"Internal S…' to be 'URL: /foo/bar' // Object.is equality Expected: "URL: /foo/bar" Received: "{"statusCode":500,"error":"Internal Server Error","message":"req is not defined"}" ❯ __tests__/server.test.ts:113:31

Check failure on line 113 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / macos-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] handles requests

AssertionError: expected '{"statusCode":500,"error":"Internal S…' to be 'URL: /foo/bar' // Object.is equality Expected: "URL: /foo/bar" Received: "{"statusCode":500,"error":"Internal Server Error","message":"req is not defined"}" ❯ __tests__/server.test.ts:113:31

Check failure on line 113 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / macos-latest | latest

__tests__/server.test.ts > [react-router] fastify createRequestHandler > [react-router] basic requests > [react-router] handles requests

AssertionError: expected '{"statusCode":500,"error":"Internal S…' to be 'URL: /foo/bar' // Object.is equality Expected: "URL: /foo/bar" Received: "{"statusCode":500,"error":"Internal Server Error","message":"req is not defined"}" ❯ __tests__/server.test.ts:113:31

Check failure on line 113 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / windows-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] handles requests

AssertionError: expected '{"statusCode":500,"error":"Internal S…' to be 'URL: /foo/bar' // Object.is equality Expected: "URL: /foo/bar" Received: "{"statusCode":500,"error":"Internal Server Error","message":"req is not defined"}" ❯ __tests__/server.test.ts:113:31

Check failure on line 113 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / windows-latest | latest

__tests__/server.test.ts > [react-router] fastify createRequestHandler > [react-router] basic requests > [react-router] handles requests

AssertionError: expected '{"statusCode":500,"error":"Internal S…' to be 'URL: /foo/bar' // Object.is equality Expected: "URL: /foo/bar" Received: "{"statusCode":500,"error":"Internal Server Error","message":"req is not defined"}" ❯ __tests__/server.test.ts:113:31
expect(response.statusCode).toBe(200);
});

Expand All @@ -123,7 +123,7 @@

let response = await app.inject("//");

expect(response.statusCode).toBe(200);

Check failure on line 126 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / ubuntu-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] handles root // URLs

AssertionError: expected 500 to be 200 // Object.is equality - Expected + Received - 200 + 500 ❯ __tests__/server.test.ts:126:37

Check failure on line 126 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / macos-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] handles root // URLs

AssertionError: expected 500 to be 200 // Object.is equality - Expected + Received - 200 + 500 ❯ __tests__/server.test.ts:126:37

Check failure on line 126 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / windows-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] handles root // URLs

AssertionError: expected 500 to be 200 // Object.is equality - Expected + Received - 200 + 500 ❯ __tests__/server.test.ts:126:37
expect(response.body).toBe("URL: //");
});

Expand All @@ -136,7 +136,7 @@

let response = await app.inject("//foo//bar");

expect(response.statusCode).toBe(200);

Check failure on line 139 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / ubuntu-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] handles nested // URLs

AssertionError: expected 500 to be 200 // Object.is equality - Expected + Received - 200 + 500 ❯ __tests__/server.test.ts:139:37

Check failure on line 139 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / macos-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] handles nested // URLs

AssertionError: expected 500 to be 200 // Object.is equality - Expected + Received - 200 + 500 ❯ __tests__/server.test.ts:139:37

Check failure on line 139 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / windows-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] handles nested // URLs

AssertionError: expected 500 to be 200 // Object.is equality - Expected + Received - 200 + 500 ❯ __tests__/server.test.ts:139:37
expect(response.body).toBe("URL: //foo//bar");
});

Expand All @@ -149,7 +149,7 @@

let response = await app.inject("/");

expect(response.statusCode).toBe(200);

Check failure on line 152 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / ubuntu-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] handles null body

AssertionError: expected 500 to be 200 // Object.is equality - Expected + Received - 200 + 500 ❯ __tests__/server.test.ts:152:37

Check failure on line 152 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / macos-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] handles null body

AssertionError: expected 500 to be 200 // Object.is equality - Expected + Received - 200 + 500 ❯ __tests__/server.test.ts:152:37

Check failure on line 152 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / windows-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] handles null body

AssertionError: expected 500 to be 200 // Object.is equality - Expected + Received - 200 + 500 ❯ __tests__/server.test.ts:152:37
});

// https://github.com/node-fetch/node-fetch/blob/4ae35388b078bddda238277142bf091898ce6fda/test/response.js#L142-L148
Expand All @@ -163,7 +163,7 @@
let app = createApp();
let response = await app.inject("/");

expect(response.statusCode).toBe(200);

Check failure on line 166 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / ubuntu-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] handles body as stream

AssertionError: expected 500 to be 200 // Object.is equality - Expected + Received - 200 + 500 ❯ __tests__/server.test.ts:166:37

Check failure on line 166 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / macos-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] handles body as stream

AssertionError: expected 500 to be 200 // Object.is equality - Expected + Received - 200 + 500 ❯ __tests__/server.test.ts:166:37

Check failure on line 166 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / windows-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] handles body as stream

AssertionError: expected 500 to be 200 // Object.is equality - Expected + Received - 200 + 500 ❯ __tests__/server.test.ts:166:37
expect(response.body).toBe("hello world");
});

Expand All @@ -175,7 +175,7 @@
let app = createApp();
let response = await app.inject("/");

expect(response.statusCode).toBe(204);

Check failure on line 178 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / ubuntu-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] handles status codes

AssertionError: expected 500 to be 204 // Object.is equality - Expected + Received - 204 + 500 ❯ __tests__/server.test.ts:178:37

Check failure on line 178 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / macos-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] handles status codes

AssertionError: expected 500 to be 204 // Object.is equality - Expected + Received - 204 + 500 ❯ __tests__/server.test.ts:178:37

Check failure on line 178 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / windows-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] handles status codes

AssertionError: expected 500 to be 204 // Object.is equality - Expected + Received - 204 + 500 ❯ __tests__/server.test.ts:178:37
});

it(`[${name}] sets headers`, async () => {
Expand All @@ -199,7 +199,7 @@
let app = createApp();
let response = await app.inject("/");

expect(response.headers["x-time-of-year"]).toBe("most wonderful");

Check failure on line 202 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / ubuntu-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] sets headers

AssertionError: expected undefined to be 'most wonderful' // Object.is equality - Expected: "most wonderful" + Received: undefined ❯ __tests__/server.test.ts:202:52

Check failure on line 202 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / macos-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] sets headers

AssertionError: expected undefined to be 'most wonderful' // Object.is equality - Expected: "most wonderful" + Received: undefined ❯ __tests__/server.test.ts:202:52

Check failure on line 202 in packages/remix-fastify/__tests__/server.test.ts

View workflow job for this annotation

GitHub Actions / test / windows-latest | latest

__tests__/server.test.ts > [remix] fastify createRequestHandler > [remix] basic requests > [remix] sets headers

AssertionError: expected undefined to be 'most wonderful' // Object.is equality - Expected: "most wonderful" + Received: undefined ❯ __tests__/server.test.ts:202:52
expect(response.headers["set-cookie"]).toEqual([
"first=one; Expires=0; Path=/; HttpOnly; Secure; SameSite=Lax",
"second=two; MaxAge=1209600; Path=/; HttpOnly; Secure; SameSite=Lax",
Expand Down Expand Up @@ -275,6 +275,30 @@
expect(request.headers.get("host")).toBe("localhost:3000");
});
});

it("validates parsed port", async () => {
let fastifyRequest = createRequest({
url: "/foo/bar",
method: "GET",
protocol: "http",
hostname: "localhost",
headers: {
"Cache-Control": "max-age=300, s-maxage=3600",
Host: "localhost:3000",
"x-forwarded-host": ":/spoofed"
},
});
let fastifyResponse = createResponse();

let remixRequest = createRemixRequest(fastifyRequest, fastifyResponse);

expect(remixRequest.method).toBe("GET");
expect(remixRequest.headers.get("cache-control")).toBe(
"max-age=300, s-maxage=3600"
);
expect(remixRequest.headers.get("host")).toBe("localhost:3000");
expect(remixRequest.url).toBe("http://localhost:3000/foo/bar");
});
}

runTests("remix", {
Expand Down
21 changes: 17 additions & 4 deletions packages/remix-fastify/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,23 @@
export function getUrl<Server extends HttpServer>(
request: FastifyRequest<RouteGenericInterface, Server>,
): string {
let origin = `${request.protocol}://${request.host}`;
// Use `request.originalUrl` so Remix and React Router are aware of the full path
let url = `${origin}${request.originalUrl}`;
return url;
// req.hostname doesn't include port information so grab that from
// `X-Forwarded-Host` or `Host`
let [, hostnamePortStr] = req.get("X-Forwarded-Host")?.split(":") ?? [];

Check failure on line 70 in packages/remix-fastify/src/shared.ts

View workflow job for this annotation

GitHub Actions / test / ubuntu-latest | latest

__tests__/server.test.ts > [remix] fastify createRequest > [remix] creates a request with the correct headers

ReferenceError: req is not defined ❯ getUrl src/shared.ts:70:29 ❯ Module.createRequest src/shared.ts:94:13 ❯ createRemixRequest src/servers/remix.ts:49:10 ❯ __tests__/server.test.ts:270:21

Check failure on line 70 in packages/remix-fastify/src/shared.ts

View workflow job for this annotation

GitHub Actions / test / ubuntu-latest | latest

__tests__/server.test.ts > validates parsed port

ReferenceError: req is not defined ❯ getUrl src/shared.ts:70:29 ❯ Module.createRequest src/shared.ts:94:13 ❯ createRemixRequest src/servers/remix.ts:49:10 ❯ __tests__/server.test.ts:280:27

Check failure on line 70 in packages/remix-fastify/src/shared.ts

View workflow job for this annotation

GitHub Actions / test / macos-latest | latest

__tests__/server.test.ts > [remix] fastify createRequest > [remix] creates a request with the correct headers

ReferenceError: req is not defined ❯ getUrl src/shared.ts:70:29 ❯ Module.createRequest src/shared.ts:94:13 ❯ createRemixRequest src/servers/remix.ts:49:10 ❯ __tests__/server.test.ts:270:21

Check failure on line 70 in packages/remix-fastify/src/shared.ts

View workflow job for this annotation

GitHub Actions / test / macos-latest | latest

__tests__/server.test.ts > validates parsed port

ReferenceError: req is not defined ❯ getUrl src/shared.ts:70:29 ❯ Module.createRequest src/shared.ts:94:13 ❯ createRemixRequest src/servers/remix.ts:49:10 ❯ __tests__/server.test.ts:280:27

Check failure on line 70 in packages/remix-fastify/src/shared.ts

View workflow job for this annotation

GitHub Actions / test / windows-latest | latest

__tests__/server.test.ts > [remix] fastify createRequest > [remix] creates a request with the correct headers

ReferenceError: req is not defined ❯ getUrl src/shared.ts:70:29 ❯ Module.createRequest src/shared.ts:94:13 ❯ createRemixRequest src/servers/remix.ts:49:10 ❯ __tests__/server.test.ts:270:21

Check failure on line 70 in packages/remix-fastify/src/shared.ts

View workflow job for this annotation

GitHub Actions / test / windows-latest | latest

__tests__/server.test.ts > validates parsed port

ReferenceError: req is not defined ❯ getUrl src/shared.ts:70:29 ❯ Module.createRequest src/shared.ts:94:13 ❯ createRemixRequest src/servers/remix.ts:49:10 ❯ __tests__/server.test.ts:280:27
let [, hostPortStr] = req.get("host")?.split(":") ?? [];
Copy link
Preview

Copilot AI Mar 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable 'req' is used instead of the function parameter 'request'. Use 'request.get(...)' to maintain consistency with the function signature.

Suggested change
// req.hostname doesn't include port information so grab that from
// `X-Forwarded-Host` or `Host`
let [, hostnamePortStr] = req.get("X-Forwarded-Host")?.split(":") ?? [];
let [, hostPortStr] = req.get("host")?.split(":") ?? [];
// request.hostname doesn't include port information so grab that from
// `X-Forwarded-Host` or `Host`
let [, hostnamePortStr] = request.get("X-Forwarded-Host")?.split(":") ?? [];
let [, hostPortStr] = request.get("host")?.split(":") ?? [];

Copilot uses AI. Check for mistakes.

let hostnamePort = Number.parseInt(hostnamePortStr, 10);
let hostPort = Number.parseInt(hostPortStr, 10);
let port = Number.isSafeInteger(hostnamePort)
? hostnamePort
: Number.isSafeInteger(hostPort)
? hostPort
: "";

// Use req.hostname here as it respects the "trust proxy" setting
let resolvedHost = `${req.hostname}${port ? `:${port}` : ""}`;
// Use `req.originalUrl` so Remix is aware of the full path
let url = new URL(req.originalUrl, `${req.protocol}://${resolvedHost}`);
return url.toString()
}

export function createRequest<Server extends HttpServer>(
Expand Down
Loading