|
23 | 23 | require('../common'); |
24 | 24 | const assert = require('assert'); |
25 | 25 | const http = require('http'); |
| 26 | +const Countdown = require('../common/countdown'); |
26 | 27 |
|
27 | 28 | // Simple test of Node's HTTP ServerResponse.statusCode |
28 | 29 | // ServerResponse.prototype.statusCode |
29 | 30 |
|
30 | | -let testsComplete = 0; |
31 | 31 | const tests = [200, 202, 300, 404, 451, 500]; |
32 | | -let testIdx = 0; |
| 32 | +let test; |
| 33 | +const countdown = new Countdown(tests.length, () => s.close()); |
33 | 34 |
|
34 | 35 | const s = http.createServer(function(req, res) { |
35 | | - const t = tests[testIdx]; |
36 | | - res.writeHead(t, { 'Content-Type': 'text/plain' }); |
| 36 | + res.writeHead(test, { 'Content-Type': 'text/plain' }); |
37 | 37 | console.log(`--\nserver: statusCode after writeHead: ${res.statusCode}`); |
38 | | - assert.strictEqual(res.statusCode, t); |
| 38 | + assert.strictEqual(res.statusCode, test); |
39 | 39 | res.end('hello world\n'); |
40 | 40 | }); |
41 | 41 |
|
42 | 42 | s.listen(0, nextTest); |
43 | 43 |
|
44 | 44 |
|
45 | 45 | function nextTest() { |
46 | | - if (testIdx + 1 === tests.length) { |
47 | | - return s.close(); |
48 | | - } |
49 | | - const test = tests[testIdx]; |
| 46 | + test = tests.shift(); |
50 | 47 |
|
51 | 48 | http.get({ port: s.address().port }, function(response) { |
52 | 49 | console.log(`client: expected status: ${test}`); |
53 | 50 | console.log(`client: statusCode: ${response.statusCode}`); |
54 | 51 | assert.strictEqual(response.statusCode, test); |
55 | 52 | response.on('end', function() { |
56 | | - testsComplete++; |
57 | | - testIdx += 1; |
58 | | - nextTest(); |
| 53 | + if (countdown.dec()) |
| 54 | + nextTest(); |
59 | 55 | }); |
60 | 56 | response.resume(); |
61 | 57 | }); |
62 | 58 | } |
63 | | - |
64 | | - |
65 | | -process.on('exit', function() { |
66 | | - assert.strictEqual(5, testsComplete); |
67 | | -}); |
0 commit comments