forked from fastify/fastify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax-requests-per-socket.test.js
113 lines (87 loc) · 3.46 KB
/
max-requests-per-socket.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict'
const net = require('node:net')
const { test } = require('node:test')
const Fastify = require('..')
test('maxRequestsPerSocket', (t, done) => {
t.plan(8)
const fastify = Fastify({ maxRequestsPerSocket: 2 })
fastify.get('/', (req, reply) => {
reply.send({ hello: 'world' })
})
fastify.listen({ port: 0 }, function (err) {
t.assert.ifError(err)
const port = fastify.server.address().port
const client = net.createConnection({ port }, () => {
client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')
client.once('data', data => {
t.assert.match(data.toString(), /Connection:\s*keep-alive/i)
t.assert.match(data.toString(), /Keep-Alive:\s*timeout=\d+/i)
t.assert.match(data.toString(), /200 OK/i)
client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')
client.once('data', data => {
t.assert.match(data.toString(), /Connection:\s*close/i)
t.assert.match(data.toString(), /200 OK/i)
client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')
client.once('data', data => {
t.assert.match(data.toString(), /Connection:\s*close/i)
t.assert.match(data.toString(), /503 Service Unavailable/i)
client.end()
fastify.close()
done()
})
})
})
})
})
})
test('maxRequestsPerSocket zero should behave same as null', (t, done) => {
t.plan(10)
const fastify = Fastify({ maxRequestsPerSocket: 0 })
fastify.get('/', (req, reply) => {
reply.send({ hello: 'world' })
})
fastify.listen({ port: 0 }, function (err) {
t.assert.ifError(err)
const port = fastify.server.address().port
const client = net.createConnection({ port }, () => {
client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')
client.once('data', data => {
t.assert.match(data.toString(), /Connection:\s*keep-alive/i)
t.assert.match(data.toString(), /Keep-Alive:\s*timeout=\d+/i)
t.assert.match(data.toString(), /200 OK/i)
client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')
client.once('data', data => {
t.assert.match(data.toString(), /Connection:\s*keep-alive/i)
t.assert.match(data.toString(), /Keep-Alive:\s*timeout=\d+/i)
t.assert.match(data.toString(), /200 OK/i)
client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')
client.once('data', data => {
t.assert.match(data.toString(), /Connection:\s*keep-alive/i)
t.assert.match(data.toString(), /Keep-Alive:\s*timeout=\d+/i)
t.assert.match(data.toString(), /200 OK/i)
client.end()
fastify.close()
done()
})
})
})
})
})
})
test('maxRequestsPerSocket should be set', async (t) => {
t.plan(1)
const initialConfig = Fastify({ maxRequestsPerSocket: 5 }).initialConfig
t.assert.deepStrictEqual(initialConfig.maxRequestsPerSocket, 5)
})
test('maxRequestsPerSocket should 0', async (t) => {
t.plan(1)
const initialConfig = Fastify().initialConfig
t.assert.deepStrictEqual(initialConfig.maxRequestsPerSocket, 0)
})
test('requestTimeout passed to server', t => {
t.plan(2)
const httpServer = Fastify({ maxRequestsPerSocket: 5 }).server
t.assert.strictEqual(httpServer.maxRequestsPerSocket, 5)
const httpsServer = Fastify({ maxRequestsPerSocket: 5, https: true }).server
t.assert.strictEqual(httpsServer.maxRequestsPerSocket, 5)
})