1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const http = require('http'); 5const Countdown = require('../common/countdown'); 6 7const MAX_REQUESTS = 13; 8let reqNum = 0; 9 10function test(res, header, code) { 11 assert.throws(() => { 12 res.writeHead(header); 13 }, { 14 code: 'ERR_HTTP_INVALID_STATUS_CODE', 15 name: 'RangeError', 16 message: `Invalid status code: ${code}` 17 }); 18} 19 20const server = http.Server(common.mustCall(function(req, res) { 21 switch (reqNum) { 22 case 0: 23 test(res, -1, '-1'); 24 break; 25 case 1: 26 test(res, Infinity, 'Infinity'); 27 break; 28 case 2: 29 test(res, NaN, 'NaN'); 30 break; 31 case 3: 32 test(res, {}, '{}'); 33 break; 34 case 4: 35 test(res, 99, '99'); 36 break; 37 case 5: 38 test(res, 1000, '1000'); 39 break; 40 case 6: 41 test(res, '1000', '1000'); 42 break; 43 case 7: 44 test(res, null, 'null'); 45 break; 46 case 8: 47 test(res, true, 'true'); 48 break; 49 case 9: 50 test(res, [], '[]'); 51 break; 52 case 10: 53 test(res, 'this is not valid', 'this is not valid'); 54 break; 55 case 11: 56 test(res, '404 this is not valid either', '404 this is not valid either'); 57 break; 58 case 12: 59 assert.throws(() => { res.writeHead(); }, 60 { 61 code: 'ERR_HTTP_INVALID_STATUS_CODE', 62 name: 'RangeError', 63 message: 'Invalid status code: undefined' 64 }); 65 this.close(); 66 break; 67 default: 68 throw new Error('Unexpected request'); 69 } 70 res.statusCode = 200; 71 res.end(); 72}, MAX_REQUESTS)); 73server.listen(); 74 75const countdown = new Countdown(MAX_REQUESTS, () => server.close()); 76 77server.on('listening', function makeRequest() { 78 http.get({ 79 port: this.address().port 80 }, (res) => { 81 assert.strictEqual(res.statusCode, 200); 82 res.on('end', () => { 83 countdown.dec(); 84 reqNum = MAX_REQUESTS - countdown.remaining; 85 if (countdown.remaining > 0) 86 makeRequest.call(this); 87 }); 88 res.resume(); 89 }); 90}); 91