prefix.ts
This file
Path
test/prefix.ts
88 lines · 4.0 KiB · TypeScript
Last changed by
TweaksSindre Sorhusbefore adoption3ded2aa
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889import test from 'ava';import ky from '../source/index.js';import {createHttpTestServer} from './helpers/create-http-test-server.js';
test('undefined clears an inherited prefix without changing the parent', async t => { const parent = ky.create({ prefix: 'https://example.com/api/', fetch: async request => new Response(request.url), }); const child = parent.extend({prefix: undefined});
t.is(await parent('users').text(), 'https://example.com/api/users'); await t.throwsAsync(child('users'), {instanceOf: TypeError}); t.is(await child('https://example.org/users').text(), 'https://example.org/users'); t.is(await parent('users').text(), 'https://example.com/api/users');});
test('prefix option', async t => { const server = await createHttpTestServer(t); server.get('/', (_request, response) => { response.end('/'); }); server.get('/foo', (_request, response) => { response.end('/foo'); }); server.get('/bar', (_request, response) => { response.end('/bar'); }); server.get('/foo/bar', (_request, response) => { response.end('/foo/bar'); });
t.is( // @ts-expect-error {prefix: boolean} isn't officially supported await ky(`${server.url}/foo/bar`, {prefix: false}).text(), '/foo/bar', ); t.is(await ky(`${server.url}/foo/bar`, {prefix: ''}).text(), '/foo/bar'); t.is(await ky(new URL(`${server.url}/foo/bar`), {prefix: ''}).text(), '/foo/bar'); t.is(await ky('foo/bar', {prefix: server.url}).text(), '/foo/bar'); t.is(await ky('foo/bar', {prefix: new URL(server.url)}).text(), '/foo/bar'); t.is(await ky('/bar', {prefix: `${server.url}/foo/`}).text(), '/foo/bar'); t.is(await ky('/bar', {prefix: `${server.url}/foo`}).text(), '/foo/bar'); t.is(await ky('//bar', {prefix: `${server.url}/foo`}).text(), '/foo/bar'); t.is(await ky('/bar', {prefix: `${server.url}/foo//`}).text(), '/foo/bar'); t.is(await ky('bar', {prefix: `${server.url}/foo/`}).text(), '/foo/bar'); t.is(await ky('bar', {prefix: `${server.url}/foo`}).text(), '/foo/bar'); t.is(await ky('bar', {prefix: new URL(`${server.url}/foo`)}).text(), '/foo/bar'); t.is(await ky('', {prefix: server.url}).text(), '/'); t.is(await ky('', {prefix: `${server.url}/`}).text(), '/'); t.is(await ky('', {prefix: new URL(server.url)}).text(), '/'); t.is( await ky( 'bar', Object.assign( Object.create({prefixUrl: `${server.url}/api`}), {prefix: `${server.url}/foo`}, ), ).text(), '/foo/bar', ); await t.throwsAsync(ky(`${server.url}/unicorn`, {prefixUrl: ''}), { message: 'The `prefixUrl` option has been renamed `prefix` in v2 and enhanced to allow slashes in input. See also the new `baseUrl` option for improved flexibility with standard URL resolution: https://github.com/sindresorhus/ky#baseurl', }, ); await t.throwsAsync(ky(`${server.url}/unicorn`, {prefixUrl: undefined}), { message: 'The `prefixUrl` option has been renamed `prefix` in v2 and enhanced to allow slashes in input. See also the new `baseUrl` option for improved flexibility with standard URL resolution: https://github.com/sindresorhus/ky#baseurl', }, );
await t.throwsAsync(ky('/unicorn', {prefixUrl: `${server.url}/api`}), { message: 'The `prefixUrl` option has been renamed `prefix` in v2 and enhanced to allow slashes in input. See also the new `baseUrl` option for improved flexibility with standard URL resolution: https://github.com/sindresorhus/ky#baseurl', }, ); await t.throwsAsync(ky(new Request(`${server.url}/unicorn`), {prefixUrl: `${server.url}/api`}), { message: 'The `prefixUrl` option has been renamed `prefix` in v2 and enhanced to allow slashes in input. See also the new `baseUrl` option for improved flexibility with standard URL resolution: https://github.com/sindresorhus/ky#baseurl', }, ); await t.throwsAsync(ky(new URL(`${server.url}/unicorn`), {prefixUrl: `${server.url}/api`}), { message: 'The `prefixUrl` option has been renamed `prefix` in v2 and enhanced to allow slashes in input. See also the new `baseUrl` option for improved flexibility with standard URL resolution: https://github.com/sindresorhus/ky#baseurl', }, );});