Skip to content
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

Allow leading slashes #1284

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
27 changes: 26 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ A trailing slash `/` is optional - one will be added automatically.

**Note:** `prefixUrl` will be ignored if the `url` argument is a URL instance.

**Note:** Leading slashes in `input` are disallowed when using this option to enforce consistency and avoid confusion. For example, when the prefix URL is `https://example.com/foo` and the input is `/bar`, there's ambiguity whether the resulting URL would become `https://example.com/foo/bar` or `https://example.com/bar`. The latter is used by browsers.
**Note:** Leading slashes in `input` are disallowed when using this option unless `allowLeadingSlash` is `true`. This is the default behavior to enforce consistency and avoid confusion. For example, when the prefix URL is `https://example.com/foo` and the input is `/bar`, there's ambiguity whether the resulting URL would become `https://example.com/foo/bar` or `https://example.com/bar`. The latter is used by browsers.

**Tip:** Useful when used with [`got.extend()`](#custom-endpoints) to create niche-specific Got instances.

Expand Down Expand Up @@ -203,6 +203,31 @@ const got = require('got');
})();
```

###### allowLeadingSlash

Type: `boolean`

When specified, leading slashes are allowed when a `prefixUrl` is given. Its behavior is the same as if you omitted the leading slash and it is implemented by removing the leading slash from the input.

```js
const got = require('got');

(async () => {
await got('/unicorn', {prefixUrl: 'https://cats.com'});
//=> throws Error: `input` must not start with a slash when using `prefixUrl`

await got('/unicorn', {prefixUrl: 'https://cats.com', allowLeadingSlash: true});
//=> https://cats.com/unicorn

const instance = got.extend({
prefixUrl: 'https://cats.com/foo/'
});

await instance('/bar');
//=> 'https://cats.com/foo/bar'
})();
```

###### headers

Type: `object`\
Expand Down
9 changes: 8 additions & 1 deletion source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export interface Options extends URLOptions, SecureContextOptions {
decompress?: boolean;
timeout?: Delays | number;
prefixUrl?: string | URL;
allowLeadingSlash?: boolean;
body?: string | Buffer | Readable;
form?: {[key: string]: any};
json?: {[key: string]: any};
Expand Down Expand Up @@ -157,6 +158,7 @@ export interface NormalizedOptions extends Options {
url: URL;
timeout: Delays;
prefixUrl: string;
allowLeadingSlash: boolean;
ignoreInvalidCookies: boolean;
decompress: boolean;
searchParams?: URLSearchParams;
Expand All @@ -183,6 +185,7 @@ export interface NormalizedOptions extends Options {
export interface Defaults {
timeout: Delays;
prefixUrl: string;
allowLeadingSlash: boolean;
method: Method;
ignoreInvalidCookies: boolean;
decompress: boolean;
Expand Down Expand Up @@ -688,7 +691,11 @@ export default class Request extends Duplex implements RequestEvents<Request> {

if (is.string(options.url)) {
if (options.url.startsWith('/')) {
throw new Error('`input` must not start with a slash when using `prefixUrl`');
if (options.allowLeadingSlash) {
options.url = options.url.slice(1);
} else {
throw new Error('`input` must not start with a slash when using `prefixUrl`');
}
}

options.url = optionsToUrl(options.prefixUrl + options.url, options as Options & {searchParams?: URLSearchParams});
Expand Down
1 change: 1 addition & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const defaults: InstanceDefaults = {
resolveBodyOnly: false,
maxRedirects: 10,
prefixUrl: '',
allowLeadingSlash: false,
methodRewriting: true,
ignoreInvalidCookies: false,
context: {},
Expand Down
16 changes: 16 additions & 0 deletions test/arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,22 @@ test('throws on leading slashes', async t => {
});
});

test('leading slashes work with `allowLeadingSlash: true`', withServer, async (t, server, got) => {
server.get('/test/foobar', echoUrl);

const instanceA = got.extend({prefixUrl: `${server.url}`, allowLeadingSlash: true});
const {body} = await instanceA('/test/foobar');
t.is(body, '/test/foobar');
});

test('leading slash does not overwrite path with `allowLeadingSlash: true`', withServer, async (t, server, got) => {
server.get('/test/foobar', echoUrl);

const instanceA = got.extend({prefixUrl: `${server.url}/test/`, allowLeadingSlash: true});
const {body} = await instanceA('/foobar');
t.is(body, '/test/foobar');
});

test('throws on invalid `dnsCache` option', async t => {
await t.throwsAsync(got('https://example.com', {
// @ts-ignore Error tests
Expand Down