/
prefix-url.js
33 lines (29 loc) 路 1.35 KB
/
prefix-url.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import test from 'ava';
import createTestServer from 'create-test-server';
import ky from '..';
test('prefixUrl option', async t => {
const server = await createTestServer();
server.get('/', (request, response) => {
response.end('zebra');
});
server.get('/api/unicorn', (request, response) => {
response.end('rainbow');
});
t.is(await ky(`${server.url}/api/unicorn`, {prefixUrl: false}).text(), 'rainbow');
t.is(await ky(`${server.url}/api/unicorn`, {prefixUrl: ''}).text(), 'rainbow');
t.is(await ky(new URL(`${server.url}/api/unicorn`), {prefixUrl: ''}).text(), 'rainbow');
t.is(await ky('api/unicorn', {prefixUrl: server.url}).text(), 'rainbow');
t.is(await ky('api/unicorn', {prefixUrl: new URL(server.url)}).text(), 'rainbow');
t.is(await ky('unicorn', {prefixUrl: `${server.url}/api`}).text(), 'rainbow');
t.is(await ky('unicorn', {prefixUrl: `${server.url}/api/`}).text(), 'rainbow');
t.is(await ky('unicorn', {prefixUrl: new URL(`${server.url}/api`)}).text(), 'rainbow');
t.is(await ky('', {prefixUrl: server.url}).text(), 'zebra');
t.is(await ky('', {prefixUrl: `${server.url}/`}).text(), 'zebra');
t.is(await ky('', {prefixUrl: new URL(server.url)}).text(), 'zebra');
t.throws(() => {
ky('/unicorn', {prefixUrl: `${server.url}/api`});
}, {
message: '`input` must not begin with a slash when using `prefixUrl`'
});
await server.close();
});