Skip to content
Permalink
Browse files

test: clean up http-set-trailers

* remove shared state of request counting from each listener by using
  callbacks to report test finish. This also fixes slight race condition
  where one of the request could finish before the other was taken into
  account resulting in ECONNREFUSED due to premature server.close()
* slightly move code for better cohesion
* fix error comment in testHttp10 'Trailer ...' -> 'No trailer ...'

PR-URL: #30522
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
  • Loading branch information
lundibundi authored and MylesBorins committed Nov 18, 2019
1 parent 80bfc08 commit 3fb0f7ebd0a62d2d4cd1fb81b1613e500b2f8530
Showing with 29 additions and 43 deletions.
  1. +29 −43 test/parallel/test-http-set-trailers.js
@@ -26,91 +26,77 @@ const http = require('http');
const net = require('net');
const util = require('util');

let outstanding_reqs = 0;

const server = http.createServer(function(req, res) {
res.writeHead(200, [['content-type', 'text/plain']]);
res.addTrailers({ 'x-foo': 'bar' });
res.end('stuff\n');
});
server.listen(0);


// First, we test an HTTP/1.0 request.
server.on('listening', function() {
const c = net.createConnection(this.address().port);
let res_buffer = '';
function testHttp10(port, callback) {
const c = net.createConnection(port);

c.setEncoding('utf8');

c.on('connect', function() {
outstanding_reqs++;
c.on('connect', () => {
c.write('GET / HTTP/1.0\r\n\r\n');
});

c.on('data', function(chunk) {
let res_buffer = '';
c.on('data', (chunk) => {
res_buffer += chunk;
});

c.on('end', function() {
c.end();
assert.ok(
!/x-foo/.test(res_buffer),
`Trailer in HTTP/1.0 response. Response buffer: ${res_buffer}`
`No trailer in HTTP/1.0 response. Response buffer: ${res_buffer}`
);
outstanding_reqs--;
if (outstanding_reqs === 0) {
server.close();
}
callback();
});
});
}

// Now, we test an HTTP/1.1 request.
server.on('listening', function() {
const c = net.createConnection(this.address().port);
let res_buffer = '';
let tid;
function testHttp11(port, callback) {
const c = net.createConnection(port);

c.setEncoding('utf8');

let tid;
c.on('connect', function() {
outstanding_reqs++;
c.write('GET / HTTP/1.1\r\n\r\n');
tid = setTimeout(common.mustNotCall(), 2000, 'Couldn\'t find last chunk.');
});

let res_buffer = '';
c.on('data', function(chunk) {
res_buffer += chunk;
if (/0\r\n/.test(res_buffer)) { // got the end.
outstanding_reqs--;
clearTimeout(tid);
assert.ok(
/0\r\nx-foo: bar\r\n\r\n$/.test(res_buffer),
`No trailer in HTTP/1.1 response. Response buffer: ${res_buffer}`
);
if (outstanding_reqs === 0) {
server.close();
}
callback();
}
});
});
}

// Now, see if the client sees the trailers.
server.on('listening', function() {
http.get({
port: this.address().port,
path: '/hello',
headers: {}
}, function(res) {
function testClientTrailers(port, callback) {
http.get({ port, path: '/hello', headers: {} }, (res) => {
res.on('end', function() {
assert.ok('x-foo' in res.trailers,
`${util.inspect(res.trailers)} misses the 'x-foo' property`);
outstanding_reqs--;
if (outstanding_reqs === 0) {
server.close();
}
callback();
});
res.resume();
});
outstanding_reqs++;
}

const server = http.createServer((req, res) => {
res.writeHead(200, [['content-type', 'text/plain']]);
res.addTrailers({ 'x-foo': 'bar' });
res.end('stuff\n');
});
server.listen(0, () => {
Promise.all([testHttp10, testHttp11, testClientTrailers]
.map(util.promisify)
.map((f) => f(server.address().port)))
.then(() => server.close());
});

0 comments on commit 3fb0f7e

Please sign in to comment.
You can’t perform that action at this time.