node/test/parallel/test-http-client-readable.js
Rich Trott 7d18a4d9cf test: refactor test-http-client-readable
* var -> const
* remove function names where V8 inference is as good or better
* add function names where there is no V8 inference
* assert.equal -> strictEqual
* move assertion from exit handler to response end handler

PR-URL: https://github.com/nodejs/node/pull/9344
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2016-11-01 14:19:30 -07:00

55 lines
1.1 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const util = require('util');
const Duplex = require('stream').Duplex;
function FakeAgent() {
http.Agent.call(this);
}
util.inherits(FakeAgent, http.Agent);
FakeAgent.prototype.createConnection = function() {
const s = new Duplex();
var once = false;
s._read = function() {
if (once)
return this.push(null);
once = true;
this.push('HTTP/1.1 200 Ok\r\nTransfer-Encoding: chunked\r\n\r\n');
this.push('b\r\nhello world\r\n');
this.readable = false;
this.push('0\r\n\r\n');
};
// Blackhole
s._write = function(data, enc, cb) {
cb();
};
s.destroy = s.destroySoon = function() {
this.writable = false;
};
return s;
};
var received = '';
const req = http.request({
agent: new FakeAgent()
}, common.mustCall(function requestCallback(res) {
res.on('data', function dataCallback(chunk) {
received += chunk;
});
res.on('end', common.mustCall(function endCallback() {
assert.strictEqual(received, 'hello world');
}));
}));
req.end();