forked from hapijs/wreck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
executable file
·42 lines (25 loc) · 946 Bytes
/
index.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
import * as Http from 'http';
import * as Net from 'net';
import * as Code from '@hapi/code';
import * as Lab from '@hapi/lab';
import * as Wreck from '..';
const { expect } = Lab.types;
// Provision server
const server = Http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Some payload');
});
await new Promise((resolve) => server.listen(0, resolve));
const address = server.address() as Net.AddressInfo;
const url = `http://localhost:${address.port}`;
// request()
const res = await Wreck.request('get', url);
const body = await Wreck.read(res);
Code.expect(Buffer.isBuffer(body)).to.equal(true);
Code.expect(body.toString()).to.equal('Some payload');
server.close();
expect.error(Wreck.request());
// read()
const stream = Wreck.toReadableStream('One two three');
const result = Buffer.from('One two three');
Code.expect<Buffer>(await Wreck.read(stream)).to.equal(result);