node/test/parallel/test-http-byteswritten.js
Jackson Tian 58dc229d9a
test: use repeat() instead of new Array().join()
The usage of `new Array(length + 1).join(str)` is strange.
Change to `str.repeat(length)` is more clearly.

PR-URL: https://github.com/nodejs/node/pull/11071
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Italo A. Casas <me@italoacasas.com>
2017-02-01 20:06:04 -08:00

35 lines
941 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const body = 'hello world\n';
const httpServer = http.createServer(common.mustCall(function(req, res) {
httpServer.close();
res.on('finish', common.mustCall(function() {
assert.strictEqual(typeof req.connection.bytesWritten, 'number');
assert(req.connection.bytesWritten > 0);
}));
res.writeHead(200, { 'Content-Type': 'text/plain' });
// Write 1.5mb to cause some requests to buffer
// Also, mix up the encodings a bit.
const chunk = '7'.repeat(1024);
const bchunk = Buffer.from(chunk);
for (let i = 0; i < 1024; i++) {
res.write(chunk);
res.write(bchunk);
res.write(chunk, 'hex');
}
// Get .bytesWritten while buffer is not empty
assert(res.connection.bytesWritten > 0);
res.end(body);
}));
httpServer.listen(0, function() {
http.get({ port: this.address().port });
});