Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: clean up http-set-trailers #30522

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
});