base-url.ts
This file
Path
test/base-url.ts
160 lines · 5.3 KiB · TypeScript
Last changed by
TweaksSindre Sorhusbefore adoption3ded2aa
History of this fileTake it
Add `baseUrl` option, rename `prefixUrl` to `prefix`, and allow leading slashes in input (#606)Seth Holladaybefore adoption1f2ad7f
lines 1–4why, one step back1import test from 'ava';2import ky from '../source/index.js';3import {createHttpTestServer} from './helpers/create-http-test-server.js';4Support readonly inputs and explicit undefined in TypeScript optionsSindre Sorhusbefore adoption51b3177
lines 5–12why, one step back5test('undefined clears an inherited baseUrl without changing the parent', async t => {6 const parent = ky.create({7 baseUrl: 'https://example.com/api/',8 fetch: async request => new Response(request.url),9 });10 const child = parent.extend({baseUrl: undefined});1112 t.is(await parent('users').text(), 'https://example.com/api/users');TweaksSindre Sorhusbefore adoption3ded2aa
line 13why, one step back13 await t.throwsAsync(child('users'), {instanceOf: TypeError});Support readonly inputs and explicit undefined in TypeScript optionsSindre Sorhusbefore adoption51b3177
lines 14–17why, one step back14 t.is(await child('https://example.org/users').text(), 'https://example.org/users');15 t.is(await parent('users').text(), 'https://example.com/api/users');16});17Add `baseUrl` option, rename `prefixUrl` to `prefix`, and allow leading slashes in input (#606)Seth Holladaybefore adoption1f2ad7f
lines 18–19why, one step back18test('baseUrl option', async t => {19 const server = await createHttpTestServer();Minor tweaksSindre Sorhusbefore adoption4b37c96
line 20why, one step back20 const serverHost = new URL(server.url).host;Add `baseUrl` option, rename `prefixUrl` to `prefix`, and allow leading slashes in input (#606)Seth Holladaybefore adoption1f2ad7f
lines 21–32why, one step back21 server.get('/', (_request, response) => {22 response.end('/');23 });24 server.get('/foo', (_request, response) => {25 response.end('/foo');26 });27 server.get('/bar', (_request, response) => {28 response.end('/bar');29 });30 server.get('/foo/bar', (_request, response) => {31 response.end('/foo/bar');32 });Minor tweaksSindre Sorhusbefore adoption4b37c96
lines 33–35why, one step back33 server.get('/api/v1/users', (_request, response) => {34 response.end('/api/v1/users');35 });Add `baseUrl` option, rename `prefixUrl` to `prefix`, and allow leading slashes in input (#606)Seth Holladaybefore adoption1f2ad7f
lines 36–41why, one step back3637 t.is(38 // @ts-expect-error {baseUrl: boolean} isn't officially supported39 await ky(`${server.url}/foo/bar`, {baseUrl: false}).text(),40 '/foo/bar',41 );Minor tweaksSindre Sorhusbefore adoption4b37c96
line 42why, one step back42 t.is(await ky(`${server.url}/foo/bar`, {baseUrl: '/api'}).text(), '/foo/bar');Add `baseUrl` option, rename `prefixUrl` to `prefix`, and allow leading slashes in input (#606)Seth Holladaybefore adoption1f2ad7f
lines 43–44why, one step back43 t.is(await ky(`${server.url}/foo/bar`, {baseUrl: ''}).text(), '/foo/bar');44 t.is(await ky(new URL(`${server.url}/foo/bar`), {baseUrl: ''}).text(), '/foo/bar');Minor tweaksSindre Sorhusbefore adoption4b37c96
lines 45–46why, one step back45 t.is(await ky(new URL(`${server.url}/foo/bar`), {baseUrl: `${server.url}/api/`}).text(), '/foo/bar');46 t.is(await ky(new Request(`${server.url}/foo/bar`), {baseUrl: `${server.url}/api/`}).text(), '/foo/bar');Add `baseUrl` option, rename `prefixUrl` to `prefix`, and allow leading slashes in input (#606)Seth Holladaybefore adoption1f2ad7f
line 47why, one step back47 t.is(await ky('foo/bar', {baseUrl: server.url}).text(), '/foo/bar');Minor tweaksSindre Sorhusbefore adoption4b37c96
lines 48–49why, one step back48 t.is(await ky(`//${serverHost}/foo/bar`, {baseUrl: `${server.url}/api/`}).text(), '/foo/bar');49 t.is(await ky('/users', {prefix: 'v1', baseUrl: `${server.url}/api/`}).text(), '/api/v1/users');Add `baseUrl` option, rename `prefixUrl` to `prefix`, and allow leading slashes in input (#606)Seth Holladaybefore adoption1f2ad7f
lines 50–61why, one step back50 t.is(await ky('foo/bar', {baseUrl: new URL(server.url)}).text(), '/foo/bar');51 t.is(await ky('/bar', {baseUrl: `${server.url}/foo/`}).text(), '/bar');52 t.is(await ky('/bar', {baseUrl: `${server.url}/foo`}).text(), '/bar');53 t.is(await ky('bar', {baseUrl: `${server.url}/foo/`}).text(), '/foo/bar');54 t.is(await ky('bar', {baseUrl: `${server.url}/foo`}).text(), '/bar');55 t.is(await ky('bar', {baseUrl: new URL(`${server.url}/foo`)}).text(), '/bar');56 t.is(await ky('', {baseUrl: server.url}).text(), '/');57 t.is(await ky('', {baseUrl: `${server.url}/`}).text(), '/');58 t.is(await ky('', {baseUrl: new URL(server.url)}).text(), '/');5960 await server.close();61});Various tweaksSindre Sorhusbefore adoption0dd9522
line 62why, one step back62Reject malformed HTTP URLs with `baseUrl`Sindre Sorhusbefore adoptionbe60db5
lines 63–101why, one step back63test('baseUrl rejects slashless HTTP URLs', async t => {64 let fetchCallCount = 0;65 const fetch: typeof globalThis.fetch = async () => {66 fetchCallCount++;67 return new Response('attacker');68 };6970 const inputs = [71 'http:example.com/collect',72 'https:example.com/collect',73 'HTTP:example.com/collect',74 'HTTPS:example.com/collect',75 ' http:example.com/collect',76 ' https:example.com/collect',77 '\0http:example.com/collect',78 '\0https:example.com/collect',79 '\thttp:example.com/collect',80 '\thttps:example.com/collect',81 '\nhttp:example.com/collect',82 '\nhttps:example.com/collect',83 '\rhttp:example.com/collect',84 '\rhttps:example.com/collect',85 'h\tttp:example.com/collect',86 'h\tttps:example.com/collect',87 'http:\nexample.com/collect',88 'https:\nexample.com/collect',89 'http:\rexample.com/collect',90 'https:\rexample.com/collect',91 String.raw`http:\example.com/collect`,92 String.raw`https:\example.com/collect`,93 String.raw`http:\\example.com/collect`,94 String.raw`https:\\example.com/collect`,95 String.raw`http:/\example.com/collect`,96 String.raw`https:/\example.com/collect`,97 String.raw`http:\/example.com/collect`,98 String.raw`https:\/example.com/collect`,99 ];100101 for (const input of inputs) {TweaksSindre Sorhusbefore adoption3ded2aa
lines 102–107why, one step back102 // eslint-disable-next-line no-await-in-loop103 await t.throwsAsync(ky(input, {104 baseUrl: 'https://trusted.test/api/',105 fetch,106 headers: {107 authorization: 'Bearer secret',Reject malformed HTTP URLs with `baseUrl`Sindre Sorhusbefore adoptionbe60db5
line 108why, one step back108 },TweaksSindre Sorhusbefore adoption3ded2aa
lines 109–112why, one step back109 }), {110 instanceOf: TypeError,111 message: '`input` url protocol must be followed by `//` when using `baseUrl`',112 });Reject malformed HTTP URLs with `baseUrl`Sindre Sorhusbefore adoptionbe60db5
lines 113–137why, one step back113 }114115 let absoluteUrl: string | undefined;116 const absoluteFetch: typeof globalThis.fetch = async input => {117 absoluteUrl = new Request(input).url;118 return new Response('ok');119 };120121 await t.notThrowsAsync(ky('http://example.test/collect', {122 baseUrl: 'https://trusted.test/api/',123 fetch: absoluteFetch,124 }).text());125 t.is(absoluteUrl, 'http://example.test/collect');126127 await t.notThrowsAsync(ky('https://example.test/collect', {128 baseUrl: 'https://trusted.test/api/',129 fetch: absoluteFetch,130 }).text());131 t.is(absoluteUrl, 'https://example.test/collect');132133 await Promise.resolve();134135 t.is(fetchCallCount, 0);136});137Various tweaksSindre Sorhusbefore adoption0dd9522
lines 138–160why, one step back138test('baseUrl is exposed on normalized hook options', async t => {139 const server = await createHttpTestServer();140 let receivedBaseUrl: URL | string | undefined;141142 server.get('/api/status', (_request, response) => {143 response.end('ok');144 });145146 await ky('status', {147 baseUrl: `${server.url}/api/`,148 hooks: {149 beforeRequest: [150 ({options}) => {151 receivedBaseUrl = options.baseUrl;152 },153 ],154 },155 }).text();156157 t.is(receivedBaseUrl, `${server.url}/api/`);158159 await server.close();160});