Skip to content

Commit

Permalink
http2: reuse http tests for http2 compat
Browse files Browse the repository at this point in the history
Simple try refactoring `test/parallel/test-http-request-end.js`
to run with `http` and `http2`.

Refs: nodejs#20868
  • Loading branch information
DaAitch committed May 26, 2018
1 parent c89669d commit dee7447
Showing 1 changed file with 57 additions and 29 deletions.
86 changes: 57 additions & 29 deletions test/parallel/test-http-request-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,68 @@
require('../common');
const assert = require('assert');
const http = require('http');
const http2 = require('http2');

const expected = 'Post Body For Test';

const server = http.Server(function(req, res) {
let result = '';

req.setEncoding('utf8');
req.on('data', function(chunk) {
result += chunk;
const http2RequestCompat = (options, cb) => {
const sess = http2.connect(`http://localhost:${options.port}`);
const stream = sess.request({
':path': options.path,
':method': options.method
});

stream.on('response', headers => {
cb({ // res
statusCode: headers[':status'],
resume: () => stream.resume()
});
});

req.on('end', function() {
assert.strictEqual(expected, result);
server.close();
res.writeHead(200);
res.end('hello world\n');
stream.on('close', () => {
sess.close();
});

});

server.listen(0, function() {
const req = http.request({
port: this.address().port,
path: '/',
method: 'POST'
}, function(res) {
console.log(res.statusCode);
res.resume();
}).on('error', function(e) {
console.log(e.message);
process.exit(1);
return stream;
};

for (const [createServer, request] of [
[http.createServer, http.request],
[http2.createServer, http2RequestCompat]
]) {

const expected = 'Post Body For Test';

const server = createServer(function(req, res) {
let result = '';

req.setEncoding('utf8');
req.on('data', function(chunk) {
result += chunk;
});

req.on('end', function() {
assert.strictEqual(expected, result);
server.close();
res.writeHead(200);
res.end('hello world\n');
});

});

const result = req.end(expected);
server.listen(0, function() {
const req = request({
port: this.address().port,
path: '/',
method: 'POST'
}, function(res) {
console.log(res.statusCode);
res.resume();
}).on('error', function(e) {
console.log(e.message);
process.exit(1);
});

assert.strictEqual(req, result);
});
const result = req.end(expected);

assert.strictEqual(req, result);
});
}

0 comments on commit dee7447

Please sign in to comment.