-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
82 lines (63 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
import http from 'node:http';
import {PassThrough} from 'node:stream';
import test from 'ava';
import pify from 'pify';
import getStream from 'get-stream';
import createTestServer from 'create-test-server';
import cloneResponse from './index.js';
const get = pify(http.get, {errorFirst: false});
let s;
const responseText = 'Hi!';
test.before(async () => {
s = await createTestServer();
s.get('/', (request, response) => {
response.send(responseText);
});
});
test.after('cleanup', async () => {
await s.close();
});
test('cloneResponse is a function', t => {
t.is(typeof cloneResponse, 'function');
});
test('returns a new PassThrough stream', async t => {
const response = await get(s.url);
const clonedResponse = cloneResponse(response);
t.true(clonedResponse instanceof PassThrough);
});
test('throws TypeError if response is not passed in', t => {
t.throws(() => {
cloneResponse();
}, {
message: 'Parameter `response` must be a response stream.',
});
});
test('streaming a response twice should fail', async t => {
const response = await get(s.url);
const firstStream = await getStream(response);
const secondStream = await getStream(response);
t.is(firstStream, responseText);
t.is(secondStream, '');
});
test('streaming multiple cloned responses succeeds', async t => {
const response = await get(s.url);
const clonedResponse = cloneResponse(response);
const firstStream = await getStream(response);
const clonedStream = await getStream(clonedResponse);
t.is(firstStream, responseText);
t.is(clonedStream, responseText);
});
test('custom properties are copied over', async t => {
const response = await get(s.url);
response.foo = 'bar';
const clonedResponse = cloneResponse(response);
t.is(clonedResponse.foo, 'bar');
});
test('function methods are bound to the original response instance', async t => {
const response = await get(s.url);
response.getContext = function () {
return this;
};
const clonedResponse = cloneResponse(response);
t.is(response.getContext(), clonedResponse.getContext());
});