-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.js
84 lines (73 loc) · 2.18 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
import {Buffer} from 'node:buffer';
import test from 'ava';
import lowercaseKeys from 'lowercase-keys';
import getStream from 'get-stream';
import Response from './index.js';
const statusCode = 200;
const headers = {Foo: 'Bar'};
const bodyText = 'Hi.';
const body = Buffer.from(bodyText);
const url = 'https://example.com';
const options = {statusCode, headers, body, url};
test('Response is a function', t => {
t.is(typeof Response, 'function');
});
test('Response cannot be invoked without \'new\'', t => {
t.throws(() => {
// eslint-disable-next-line new-cap
Response({statusCode, headers, body, url});
});
t.notThrows(() => {
// eslint-disable-next-line no-new
new Response({statusCode, headers, body, url});
});
});
test('new Response() throws on invalid statusCode', t => {
t.throws(() => {
// eslint-disable-next-line no-new
new Response({headers, body, url});
}, {
message: 'Argument `statusCode` should be a number',
});
});
test('new Response() throws on invalid headers', t => {
t.throws(() => {
// eslint-disable-next-line no-new
new Response({statusCode, body, url});
}, {
message: 'Argument `headers` should be an object',
});
});
test('new Response() throws on invalid body', t => {
t.throws(() => {
// eslint-disable-next-line no-new
new Response({statusCode, headers, url});
}, {
message: 'Argument `body` should be a buffer',
});
});
test('new Response() throws on invalid url', t => {
t.throws(() => {
// eslint-disable-next-line no-new
new Response({statusCode, headers, body});
}, {
message: 'Argument `url` should be a string',
});
});
test('response has expected properties', t => {
const response = new Response(options);
t.is(response.statusCode, statusCode);
t.deepEqual(response.headers, lowercaseKeys(headers));
t.is(response.body, body);
t.is(response.url, url);
});
test('response headers have lowercase keys', t => {
const response = new Response(options);
t.not(JSON.stringify(headers), response.headers);
t.deepEqual(response.headers, lowercaseKeys(headers));
});
test('response streams body', async t => {
const response = new Response(options);
const responseStream = await getStream(response);
t.is(responseStream, bodyText);
});