Skip to content

Commit

Permalink
test: clean up http-set-trailers
Browse files Browse the repository at this point in the history
* 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 21, 2019
1 parent 7188b95 commit 6be0398
Showing 1 changed file with 29 additions and 43 deletions.
72 changes: 29 additions & 43 deletions test/parallel/test-http-set-trailers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 6be0398

Please sign in to comment.