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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
'use strict'
const { test } = require('tap')
const { Client, errors } = require('..')
const { createServer } = require('http')
test('max response size', (t) => {
t.plan(4)
t.test('default max default size should allow all responses', (t) => {
t.plan(3)
const server = createServer()
t.teardown(server.close.bind(server))
server.on('request', (req, res) => {
res.end('hello')
})
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`, { maxResponseSize: -1 })
t.teardown(client.close.bind(client))
client.request({ path: '/', method: 'GET' }, (err, { statusCode, body }) => {
t.error(err)
t.equal(statusCode, 200)
const bufs = []
body.on('data', (buf) => {
bufs.push(buf)
})
body.on('end', () => {
t.equal('hello', Buffer.concat(bufs).toString('utf8'))
})
})
})
})
t.test('max response size set to zero should allow only empty responses', (t) => {
t.plan(3)
const server = createServer()
t.teardown(server.close.bind(server))
server.on('request', (req, res) => {
res.end()
})
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`, { maxResponseSize: 0 })
t.teardown(client.close.bind(client))
client.request({ path: '/', method: 'GET' }, (err, { statusCode, body }) => {
t.error(err)
t.equal(statusCode, 200)
const bufs = []
body.on('data', (buf) => {
bufs.push(buf)
})
body.on('end', () => {
t.equal('', Buffer.concat(bufs).toString('utf8'))
})
})
})
})
t.test('should throw an error if the response is too big', (t) => {
t.plan(3)
const server = createServer()
t.teardown(server.close.bind(server))
server.on('request', (req, res) => {
res.end('hello')
})
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`, {
maxResponseSize: 1
})
t.teardown(client.close.bind(client))
client.request({ path: '/', method: 'GET' }, (err, { body }) => {
t.error(err)
body.on('error', (err) => {
t.ok(err)
t.type(err, errors.ResponseExceededMaxSizeError)
})
})
})
})
t.test('invalid max response size should throw an error', (t) => {
t.plan(2)
t.throws(() => {
// eslint-disable-next-line no-new
new Client('http://localhost:3000', { maxResponseSize: 'hello' })
}, 'maxResponseSize must be a number')
t.throws(() => {
// eslint-disable-next-line no-new
new Client('http://localhost:3000', { maxResponseSize: -2 })
}, 'maxResponseSize must be greater than or equal to -1')
})
})
|