Skip to content

Commit

Permalink
test: refactor test-http-information-processing
Browse files Browse the repository at this point in the history
Replace magic numbers with constant.
Use for loop for repeated code.
Reduce console logging.
Remove unneeded countdown module use.

PR-URL: #32547
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
Trott authored and MylesBorins committed Mar 31, 2020
1 parent 04237ec commit 8bcbb8d
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions test/parallel/test-http-information-processing.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@
require('../common');
const assert = require('assert');
const http = require('http');
const Countdown = require('../common/countdown');

const test_res_body = 'other stuff!\n';
const countdown = new Countdown(3, () => server.close());
const testResBody = 'other stuff!\n';
const kMessageCount = 2;

const server = http.createServer((req, res) => {
console.error('Server sending informational message #1...');
res.writeProcessing();
console.error('Server sending informational message #2...');
res.writeProcessing();
for (let i = 0; i < kMessageCount; i++) {
console.error(`Server sending informational message #${i}...`);
res.writeProcessing();
}
console.error('Server sending full response...');
res.writeHead(200, {
'Content-Type': 'text/plain',
'ABCD': '1'
});
res.end(test_res_body);
res.end(testResBody);
});

server.listen(0, function() {
Expand All @@ -29,24 +28,22 @@ server.listen(0, function() {
console.error('Client sending request...');

let body = '';
let infoCount = 0;

req.on('information', function(res) {
console.error('Client got 102 Processing...');
countdown.dec();
});
req.on('information', () => { infoCount++; });

req.on('response', function(res) {
// Check that all 102 Processing received before full response received.
assert.strictEqual(countdown.remaining, 1);
assert.strictEqual(infoCount, kMessageCount);
assert.strictEqual(res.statusCode, 200,
`Final status code was ${res.statusCode}, not 200.`);
res.setEncoding('utf8');
res.on('data', function(chunk) { body += chunk; });
res.on('end', function() {
console.error('Got full response.');
assert.strictEqual(body, test_res_body);
assert.strictEqual(body, testResBody);
assert.ok('abcd' in res.headers);
countdown.dec();
server.close();
});
});
});

0 comments on commit 8bcbb8d

Please sign in to comment.