2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-12-30 18:38:06 -05:00
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const http = require('http');
|
|
|
|
const net = require('net');
|
2010-09-09 16:29:35 +10:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
let outstanding_reqs = 0;
|
2010-09-09 16:29:35 +10:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const server = http.createServer(function(req, res) {
|
2010-12-06 01:33:52 +03:00
|
|
|
res.writeHead(200, [['content-type', 'text/plain']]);
|
|
|
|
res.addTrailers({'x-foo': 'bar'});
|
|
|
|
res.end('stuff' + '\n');
|
2010-09-09 16:29:35 +10:00
|
|
|
});
|
2016-05-29 03:06:56 -04:00
|
|
|
server.listen(0);
|
2010-09-09 16:29:35 +10:00
|
|
|
|
|
|
|
|
|
|
|
// first, we test an HTTP/1.0 request.
|
2011-10-15 01:08:36 +02:00
|
|
|
server.on('listening', function() {
|
2017-01-08 13:19:00 +00:00
|
|
|
const c = net.createConnection(this.address().port);
|
|
|
|
let res_buffer = '';
|
2010-09-09 16:29:35 +10:00
|
|
|
|
2010-12-06 01:33:52 +03:00
|
|
|
c.setEncoding('utf8');
|
2010-09-09 16:29:35 +10:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
c.on('connect', function() {
|
2010-09-09 16:29:35 +10:00
|
|
|
outstanding_reqs++;
|
2010-12-06 01:33:52 +03:00
|
|
|
c.write('GET / HTTP/1.0\r\n\r\n');
|
2010-09-09 16:29:35 +10:00
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
c.on('data', function(chunk) {
|
2010-12-06 01:33:52 +03:00
|
|
|
//console.log(chunk);
|
2010-09-09 16:29:35 +10:00
|
|
|
res_buffer += chunk;
|
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
c.on('end', function() {
|
2010-09-09 16:29:35 +10:00
|
|
|
c.end();
|
2016-02-03 12:27:40 -08:00
|
|
|
assert.ok(!/x-foo/.test(res_buffer), 'Trailer in HTTP/1.0 response.');
|
2010-09-09 16:29:35 +10:00
|
|
|
outstanding_reqs--;
|
2016-08-17 16:14:43 -07:00
|
|
|
if (outstanding_reqs === 0) {
|
2010-12-06 01:33:52 +03:00
|
|
|
server.close();
|
|
|
|
process.exit();
|
2010-09-09 16:29:35 +10:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// now, we test an HTTP/1.1 request.
|
2011-10-15 01:08:36 +02:00
|
|
|
server.on('listening', function() {
|
2017-01-08 13:19:00 +00:00
|
|
|
const c = net.createConnection(this.address().port);
|
|
|
|
let res_buffer = '';
|
|
|
|
let tid;
|
2010-09-09 16:29:35 +10:00
|
|
|
|
2010-12-06 01:33:52 +03:00
|
|
|
c.setEncoding('utf8');
|
2010-09-09 16:29:35 +10:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
c.on('connect', function() {
|
2010-09-09 16:29:35 +10:00
|
|
|
outstanding_reqs++;
|
2010-12-06 01:33:52 +03:00
|
|
|
c.write('GET / HTTP/1.1\r\n\r\n');
|
2015-10-20 11:40:54 -07:00
|
|
|
tid = setTimeout(common.fail, 2000, 'Couldn\'t find last chunk.');
|
2010-09-09 16:29:35 +10:00
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
c.on('data', function(chunk) {
|
2010-12-06 01:33:52 +03:00
|
|
|
//console.log(chunk);
|
2010-09-09 16:29:35 +10:00
|
|
|
res_buffer += chunk;
|
|
|
|
if (/0\r\n/.test(res_buffer)) { // got the end.
|
2010-12-06 01:33:52 +03:00
|
|
|
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.'
|
|
|
|
);
|
2016-08-17 16:14:43 -07:00
|
|
|
if (outstanding_reqs === 0) {
|
2010-12-06 01:33:52 +03:00
|
|
|
server.close();
|
|
|
|
process.exit();
|
|
|
|
}
|
2010-09-09 16:29:35 +10:00
|
|
|
}
|
|
|
|
});
|
2010-09-29 19:38:48 +10:00
|
|
|
});
|
2010-09-09 16:29:35 +10:00
|
|
|
|
2010-09-29 19:38:48 +10:00
|
|
|
// now, see if the client sees the trailers.
|
2011-10-15 01:08:36 +02:00
|
|
|
server.on('listening', function() {
|
2016-05-29 03:06:56 -04:00
|
|
|
http.get({
|
|
|
|
port: this.address().port,
|
|
|
|
path: '/hello',
|
|
|
|
headers: {}
|
|
|
|
}, function(res) {
|
2011-10-15 01:08:36 +02:00
|
|
|
res.on('end', function() {
|
2010-12-06 01:33:52 +03:00
|
|
|
//console.log(res.trailers);
|
|
|
|
assert.ok('x-foo' in res.trailers, 'Client doesn\'t see trailers.');
|
2010-09-29 19:38:48 +10:00
|
|
|
outstanding_reqs--;
|
2016-08-17 16:14:43 -07:00
|
|
|
if (outstanding_reqs === 0) {
|
2010-09-29 19:38:48 +10:00
|
|
|
server.close();
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
});
|
2012-12-13 07:47:33 -08:00
|
|
|
res.resume();
|
2010-09-29 19:38:48 +10:00
|
|
|
});
|
2011-08-14 15:02:14 +09:00
|
|
|
outstanding_reqs++;
|
2010-09-09 16:29:35 +10:00
|
|
|
});
|