Skip to content

Commit 9725fbd

Browse files
committed
FIx preserving prefixUrl in hooks
Fixes #2124
1 parent 449833a commit 9725fbd

File tree

3 files changed

+78
-9
lines changed

3 files changed

+78
-9
lines changed

source/core/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,6 @@ export default class Request extends Duplex implements RequestEvents<Request> {
906906
}
907907

908908
this.redirectUrls.push(redirectUrl);
909-
updatedOptions.prefixUrl = '';
910909
updatedOptions.url = redirectUrl;
911910

912911
for (const hook of updatedOptions.hooks.beforeRedirect) {
@@ -1290,9 +1289,6 @@ export default class Request extends Duplex implements RequestEvents<Request> {
12901289
}
12911290
}
12921291

1293-
// Reset `prefixUrl`
1294-
options.prefixUrl = '';
1295-
12961292
let request: ReturnType<Options['getRequestFunction']> | undefined;
12971293

12981294
for (const hook of options.hooks.beforeRequest) {

source/core/options.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,10 +1075,6 @@ const cloneInternals = (internals: typeof defaultInternals) => {
10751075
pagination: {...internals.pagination},
10761076
};
10771077

1078-
if (result.url !== undefined) {
1079-
result.prefixUrl = '';
1080-
}
1081-
10821078
return result;
10831079
};
10841080

@@ -1648,7 +1644,12 @@ export default class Options {
16481644
throw new Error('`url` must not start with a slash');
16491645
}
16501646

1651-
const urlString = `${this.prefixUrl as string}${value.toString()}`;
1647+
// Detect if URL is already absolute (has a protocol/scheme)
1648+
const valueString = value.toString();
1649+
const isAbsolute = is.urlInstance(value) || /^[a-z][a-z\d+.-]*:/i.test(valueString);
1650+
1651+
// Only concatenate prefixUrl if the URL is relative
1652+
const urlString = isAbsolute ? valueString : `${this.prefixUrl as string}${valueString}`;
16521653
const url = new URL(urlString);
16531654
this._internals.url = url;
16541655

test/hooks.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,78 @@ test('beforeRetry allows stream body if different from original', withServer, as
592592
t.is(body, 'test');
593593
});
594594

595+
test('prefixUrl is preserved in beforeRequest hook', withServer, async (t, server, got) => {
596+
server.get('/endpoint', (_request, response) => {
597+
response.end('success');
598+
});
599+
600+
let capturedPrefixUrl: string | URL | undefined;
601+
602+
await got('endpoint', {
603+
prefixUrl: server.url,
604+
hooks: {
605+
beforeRequest: [
606+
options => {
607+
capturedPrefixUrl = options.prefixUrl;
608+
},
609+
],
610+
},
611+
});
612+
613+
const normalizedServerUrl = new URL(server.url).toString();
614+
t.is(capturedPrefixUrl, normalizedServerUrl);
615+
});
616+
617+
test('prefixUrl is preserved in beforeRetry hook', withServer, async (t, server, got) => {
618+
server.get('/retry', (_request, response) => {
619+
response.statusCode = 500;
620+
response.end();
621+
});
622+
623+
let capturedPrefixUrl: string | URL | undefined;
624+
625+
await t.throwsAsync(got('retry', {
626+
prefixUrl: server.url,
627+
retry: {
628+
limit: 1,
629+
},
630+
hooks: {
631+
beforeRetry: [
632+
({options}) => {
633+
capturedPrefixUrl = options.prefixUrl;
634+
},
635+
],
636+
},
637+
}));
638+
639+
const normalizedServerUrl = new URL(server.url).toString();
640+
t.is(capturedPrefixUrl, normalizedServerUrl);
641+
});
642+
643+
test('setting absolute URL in hook does not concatenate with prefixUrl', withServer, async (t, server, got) => {
644+
server.get('/original', (_request, response) => {
645+
response.end('original');
646+
});
647+
648+
server.get('/changed', (_request, response) => {
649+
response.end('changed');
650+
});
651+
652+
const {body} = await got('original', {
653+
prefixUrl: server.url,
654+
hooks: {
655+
beforeRequest: [
656+
options => {
657+
// Set absolute URL - should not concatenate with prefixUrl
658+
options.url = new URL(`${server.url}/changed`);
659+
},
660+
],
661+
},
662+
});
663+
664+
t.is(body, 'changed');
665+
});
666+
595667
test('afterResponse is called with response', withServer, async (t, server, got) => {
596668
server.get('/', echoHeaders);
597669

0 commit comments

Comments
 (0)