1'use strict'; 2 3const common = require('../common'); 4if (!common.hasCrypto) 5 common.skip('missing crypto'); 6const assert = require('assert'); 7const h2 = require('http2'); 8 9// Http2ServerResponse should have a statusCode property 10 11const server = h2.createServer(); 12server.listen(0, common.mustCall(function() { 13 const port = server.address().port; 14 server.once('request', common.mustCall(function(request, response) { 15 const expectedDefaultStatusCode = 200; 16 const realStatusCodes = { 17 continue: 100, 18 ok: 200, 19 multipleChoices: 300, 20 badRequest: 400, 21 internalServerError: 500 22 }; 23 const fakeStatusCodes = { 24 tooLow: 99, 25 tooHigh: 600 26 }; 27 28 assert.strictEqual(response.statusCode, expectedDefaultStatusCode); 29 30 // Setting the response.statusCode should not throw. 31 response.statusCode = realStatusCodes.ok; 32 response.statusCode = realStatusCodes.multipleChoices; 33 response.statusCode = realStatusCodes.badRequest; 34 response.statusCode = realStatusCodes.internalServerError; 35 36 assert.throws(() => { 37 response.statusCode = realStatusCodes.continue; 38 }, { 39 code: 'ERR_HTTP2_INFO_STATUS_NOT_ALLOWED', 40 name: 'RangeError' 41 }); 42 assert.throws(() => { 43 response.statusCode = fakeStatusCodes.tooLow; 44 }, { 45 code: 'ERR_HTTP2_STATUS_INVALID', 46 name: 'RangeError' 47 }); 48 assert.throws(() => { 49 response.statusCode = fakeStatusCodes.tooHigh; 50 }, { 51 code: 'ERR_HTTP2_STATUS_INVALID', 52 name: 'RangeError' 53 }); 54 55 response.on('finish', common.mustCall(function() { 56 server.close(); 57 })); 58 response.end(); 59 })); 60 61 const url = `http://localhost:${port}`; 62 const client = h2.connect(url, common.mustCall(function() { 63 const headers = { 64 ':path': '/', 65 ':method': 'GET', 66 ':scheme': 'http', 67 ':authority': `localhost:${port}` 68 }; 69 const request = client.request(headers); 70 request.on('end', common.mustCall(function() { 71 client.close(); 72 })); 73 request.end(); 74 request.resume(); 75 })); 76})); 77