forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-http-remove-connection-header-persists-connection.js
75 lines (62 loc) · 2.06 KB
/
test-http-remove-connection-header-persists-connection.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
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
const http = require('http');
const server = http.createServer(function(request, response) {
// When the connection header is removed, for HTTP/1.1 the connection should still persist.
// For HTTP/1.0, the connection should be closed after the response automatically.
response.removeHeader('connection');
if (request.httpVersion === '1.0') {
const socket = request.socket;
response.on('finish', common.mustCall(function() {
assert.ok(socket.writableEnded);
}));
}
response.end('beep boop\n');
});
const agent = new http.Agent({ keepAlive: true });
function makeHttp11Request(cb) {
http.get({
port: server.address().port,
agent
}, function(res) {
const socket = res.socket;
assert.strictEqual(res.statusCode, 200);
assert.strictEqual(res.headers.connection, undefined);
res.setEncoding('ascii');
let response = '';
res.on('data', function(chunk) {
response += chunk;
});
res.on('end', function() {
assert.strictEqual(response, 'beep boop\n');
// Wait till next tick to ensure that the socket is returned to the agent before
// we continue to the next request
process.nextTick(function() {
cb(socket);
});
});
});
}
function makeHttp10Request(cb) {
// We have to manually make HTTP/1.0 requests since Node does not allow sending them:
const socket = net.connect({ port: server.address().port }, function() {
socket.write('GET / HTTP/1.0\r\n' +
'Host: localhost:' + server.address().port + '\r\n' +
'\r\n');
socket.resume(); // Ignore the response itself
socket.on('close', cb);
});
}
server.listen(0, function() {
makeHttp11Request(function(firstSocket) {
makeHttp11Request(function(secondSocket) {
// Both HTTP/1.1 requests should have used the same socket:
assert.strictEqual(firstSocket, secondSocket);
makeHttp10Request(function() {
server.close();
});
});
});
});