Skip to content

Commit 8108e80

Browse files
committed
Fix bug from the last bit of forwarding+beforeRequest fixes
1 parent a6ca88f commit 8108e80

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/rules/requests/request-handlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
470470
hostHeader[1] = updateHostHeader;
471471
} // Otherwise: falsey means don't touch it.
472472

473-
reqUrl = new URL(`${protocol}//${hostname}${(port ? `:${port}` : '')}/${path}`).toString();
473+
reqUrl = new URL(`${protocol}//${hostname}${(port ? `:${port}` : '')}${path}`).toString();
474474
}
475475

476476
// Override the request details, if a transform or callback is specified:

test/integration/proxying/proxy-transforms.spec.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ nodeOnly(() => {
106106
it("can update the host header when used with beforeRequest", async () => {
107107
let remoteEndpointMock = await remoteServer.forGet('/get').thenReply(200, "mocked data");
108108
await server.forAnyRequest().thenForwardTo(remoteServer.url, {
109-
beforeRequest: () => {}
109+
beforeRequest: (req) => {
110+
// Forwarding modifications should be applied before beforeRequest:
111+
expect(req.url).to.equal(remoteServer.urlFor('/get'));
112+
expect(req.headers.host).to.equal(`localhost:${remoteServer.port}`);
113+
}
110114
});
111115

112116
await request.get(server.urlFor("/get"));
@@ -118,7 +122,11 @@ nodeOnly(() => {
118122
it("can avoid updating the host header when used with beforeRequest", async () => {
119123
let remoteEndpointMock = await remoteServer.forGet('/get').thenReply(200, "mocked data");
120124
await server.forAnyRequest().thenForwardTo(remoteServer.url, {
121-
beforeRequest: () => {},
125+
beforeRequest: (req) => {
126+
// Forwarding modifications should be applied before beforeRequest:
127+
expect(req.url).to.equal(remoteServer.urlFor('/get')); // <-- New destination
128+
expect(req.headers.host).to.equal(`localhost:${server.port}`); // <-- but old Host
129+
},
122130
forwarding: { updateHostHeader: false }
123131
});
124132

@@ -129,7 +137,7 @@ nodeOnly(() => {
129137
});
130138

131139
it("doesn't override the host header if beforeRequest does instead", async () => {
132-
let remoteEndpointMock = await remoteServer.forGet('/get').thenReply(200, "mocked data");
140+
await remoteServer.forGet('/get').thenReply(200, "mocked data");
133141
await server.forAnyRequest().thenForwardTo(remoteServer.url, {
134142
beforeRequest: () => ({ url: 'http://never.test' })
135143
});

0 commit comments

Comments
 (0)