-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6413200
commit 12d6866
Showing
2 changed files
with
116 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
'use strict'; | ||
|
||
const net = require('net'); | ||
const http = require('http'); | ||
const assert = require('assert'); | ||
const common = require('../common'); | ||
|
||
const server = http.createServer(function (req, res) { | ||
res.writeHead(200, {'Content-Type': 'text/plain'}); | ||
res.write('Hello World!'); | ||
res.end(); | ||
}) | ||
|
||
function assertResponse(headers, body, expectClosed) { | ||
if (expectClosed) { | ||
assert.match(responses[4], /Connection: close\r\n/m); | ||
assert(responses[4].search(/Keep-Alive: timeout=5, max=3\r\n/m) === -1); | ||
assert.match(responses[5], /Hello World!/m); | ||
} else { | ||
assert.match(headers, /Connection: keep-alive\r\n/m); | ||
assert.match(headers, /Keep-Alive: timeout=5, max=3\r\n/m); | ||
assert.match(body, /Hello World!/m); | ||
} | ||
} | ||
|
||
function writeRequest(socket) { | ||
socket.write('GET / HTTP/1.1\r\n'); | ||
socket.write('Connection: keep-alive\r\n'); | ||
socket.write('\r\n\r\n') | ||
} | ||
|
||
server.maxRequestsPerSocket = 3; | ||
server.listen(0, common.mustCall((res) => { | ||
const socket = net.createConnection( | ||
{ port: server.address().port }, | ||
common.mustCall(() => { | ||
writeRequest(socket) | ||
writeRequest(socket) | ||
|
||
const anotherSocket = net.createConnection( | ||
{ port: server.address().port }, | ||
common.mustCall(() => { | ||
writeRequest(anotherSocket) | ||
writeRequest(anotherSocket) | ||
|
||
let anotherBuffer = '' | ||
|
||
anotherSocket.setEncoding('utf8'); | ||
anotherSocket.on('data', common.mustCall((data) => { | ||
anotherBuffer += data; | ||
})); | ||
|
||
anotherSocket.on('end', common.mustCall(() => { | ||
const anoterResponses = anotherBuffer.trim().split('\r\n\r\n'); | ||
|
||
assertResponse(anoterResponses[0], anoterResponses[1], false) | ||
assertResponse(anoterResponses[2], anoterResponses[3], false) | ||
|
||
// Add two additional requests to two previous on the first socket | ||
writeRequest(socket) | ||
writeRequest(socket) | ||
|
||
let buffer = ''; | ||
socket.setEncoding('utf8'); | ||
socket.on('data', common.mustCall((data) => { | ||
buffer += data; | ||
})); | ||
|
||
socket.on('end', common.mustCall(() => { | ||
const responses = buffer.trim().split('\r\n\r\n'); | ||
// We sent more requests than allowed per socket, | ||
// but we get only the allowed number of responses & headers | ||
assert(responses.length === server.maxRequestsPerSocket * 2); | ||
|
||
assertResponse(responses[0], responses[1], false) | ||
assertResponse(responses[2], responses[3], false) | ||
assertResponse(responses[4], responses[5], true) | ||
|
||
server.close(); | ||
})); | ||
})); | ||
})); | ||
}) | ||
); | ||
})); |