/
unix-socket.ts
71 lines (56 loc) 路 1.94 KB
/
unix-socket.ts
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import process from 'process';
import {format} from 'util';
import test from 'ava';
import {Handler} from 'express';
import got from '../source/index.js';
import {withSocketServer} from './helpers/with-server.js';
const okHandler: Handler = (_request, response) => {
response.end('ok');
};
const redirectHandler: Handler = (_request, response) => {
response.writeHead(302, {
location: 'foo',
});
response.end();
};
if (process.platform !== 'win32') {
test('works', withSocketServer, async (t, server) => {
server.on('/', okHandler);
const url = format('http://unix:%s:%s', server.socketPath, '/');
t.is((await got(url)).body, 'ok');
});
test('protocol-less works', withSocketServer, async (t, server) => {
server.on('/', okHandler);
const url = format('unix:%s:%s', server.socketPath, '/');
t.is((await got(url)).body, 'ok');
});
test('address with : works', withSocketServer, async (t, server) => {
server.on('/foo:bar', okHandler);
const url = format('unix:%s:%s', server.socketPath, '/foo:bar');
t.is((await got(url)).body, 'ok');
});
test('throws on invalid URL', async t => {
try {
await got('unix:', {retry: {limit: 0}});
} catch (error: any) {
t.regex(error.code, /ENOTFOUND|EAI_AGAIN/);
}
});
test('works when extending instances', withSocketServer, async (t, server) => {
server.on('/', okHandler);
const url = format('unix:%s:%s', server.socketPath, '/');
const instance = got.extend({prefixUrl: url});
t.is((await instance('')).body, 'ok');
});
test('passes search params', withSocketServer, async (t, server) => {
server.on('/?a=1', okHandler);
const url = format('http://unix:%s:%s', server.socketPath, '/?a=1');
t.is((await got(url)).body, 'ok');
});
test('redirects work', withSocketServer, async (t, server) => {
server.on('/', redirectHandler);
server.on('/foo', okHandler);
const url = format('http://unix:%s:%s', server.socketPath, '/');
t.is((await got(url)).body, 'ok');
});
}