-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
89 lines (77 loc) · 2.02 KB
/
test.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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'use strict';
const {createServer} = require('http');
const Fettuccine = require('.');
const test = require('tape');
let count = 0;
createServer(function(req, response) {
if (req.method === 'POST') {
req.once('data', data => {
response.writeHead(200, {'Content-Type': 'application/json'});
response.end(`{"data": ${data.toString()}}`, () => req.destroy());
});
} else if (req.method === 'DELETE') {
response.writeHead(200, {
'Content-Type': 'text/plain',
[req.headers.key]: req.headers['user-agent']
});
response.end('{"status": "deleted"}', () => req.destroy());
} else {
response.writeHead(200, {'Content-Type': 'application/json'});
response.end('[1]', () => req.destroy());
}
if (++count === 3) {
this.close();
}
}).listen(8124);
test('Fettuccine prototype methods', t => {
t.plan(5);
(async () => {
t.equal(
await (await new Fettuccine({}).get('http://localhost:8124')).text(),
'[1]',
'should send a request to the server.'
);
})();
(async () => {
t.deepEqual(
await (await new Fettuccine({
method: 'post',
body: JSON.stringify([1, 2]),
headers: {}
}).fetch('http://localhost:8124')).json(),
{data: [1, 2]},
'should use constructor parameters as its default options.'
);
})();
(async () => {
const {body, headers} = await new Fettuccine({headers: {'User-agent': 'fettucine'}}).delete('/', {
headers: {
key: 'foo',
'user-Agent': 'Fettucine'
},
baseUrl: 'http://localhost:8124'
});
for await (const chunk of body) {
t.ok(
chunk.equals(Buffer.from('{"status": "deleted"}')),
'should contain per-method aliases.'
);
}
t.equal(
headers.get('foo'),
'Fettucine',
'should use the first argument of the method as prior options.'
);
})();
(async () => {
try {
await new Fettuccine({}).delete('https://n/o/_/s/e/r/v/e/r', {method: 'put'});
} catch ({code}) {
t.equal(
code,
'ERR_OPTION_UNCONFIGURABLE',
'should make `method` option unconfigurable if the method is not `request`.'
);
}
})();
});