|
| 1 | +// Flags: --expose-http2 |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | +if (!common.hasCrypto) |
| 6 | + common.skip('missing crypto'); |
| 7 | +const http2 = require('http2'); |
| 8 | +const path = require('path'); |
| 9 | +const fs = require('fs'); |
| 10 | + |
| 11 | +const optionsWithTypeError = { |
| 12 | + offset: 'number', |
| 13 | + length: 'number', |
| 14 | + statCheck: 'function', |
| 15 | + getTrailers: 'function' |
| 16 | +}; |
| 17 | + |
| 18 | +const types = { |
| 19 | + boolean: true, |
| 20 | + function: () => {}, |
| 21 | + number: 1, |
| 22 | + object: {}, |
| 23 | + array: [], |
| 24 | + null: null, |
| 25 | + symbol: Symbol('test') |
| 26 | +}; |
| 27 | + |
| 28 | +const fname = path.resolve(common.fixturesDir, 'elipses.txt'); |
| 29 | +const fd = fs.openSync(fname, 'r'); |
| 30 | + |
| 31 | +const server = http2.createServer(); |
| 32 | + |
| 33 | +server.on('stream', common.mustCall((stream) => { |
| 34 | + // should throw if fd isn't a number |
| 35 | + Object.keys(types).forEach((type) => { |
| 36 | + if (type === 'number') { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + common.expectsError( |
| 41 | + () => stream.respondWithFD(types[type], { |
| 42 | + [http2.constants.HTTP2_HEADER_CONTENT_TYPE]: 'text/plain' |
| 43 | + }), |
| 44 | + { |
| 45 | + type: TypeError, |
| 46 | + code: 'ERR_INVALID_ARG_TYPE', |
| 47 | + message: 'The "fd" argument must be of type number' |
| 48 | + } |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + // Check for all possible TypeError triggers on options |
| 53 | + Object.keys(optionsWithTypeError).forEach((option) => { |
| 54 | + Object.keys(types).forEach((type) => { |
| 55 | + if (type === optionsWithTypeError[option]) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + common.expectsError( |
| 60 | + () => stream.respondWithFD(fd, { |
| 61 | + [http2.constants.HTTP2_HEADER_CONTENT_TYPE]: 'text/plain' |
| 62 | + }, { |
| 63 | + [option]: types[type] |
| 64 | + }), |
| 65 | + { |
| 66 | + type: TypeError, |
| 67 | + code: 'ERR_INVALID_OPT_VALUE', |
| 68 | + message: `The value "${String(types[type])}" is invalid ` + |
| 69 | + `for option "${option}"` |
| 70 | + } |
| 71 | + ); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + // Should throw if :status 204, 205 or 304 |
| 76 | + [204, 205, 304].forEach((status) => common.expectsError( |
| 77 | + () => stream.respondWithFD(fd, { |
| 78 | + [http2.constants.HTTP2_HEADER_CONTENT_TYPE]: 'text/plain', |
| 79 | + ':status': status, |
| 80 | + }), |
| 81 | + { |
| 82 | + code: 'ERR_HTTP2_PAYLOAD_FORBIDDEN', |
| 83 | + message: `Responses with ${status} status must not have a payload` |
| 84 | + } |
| 85 | + )); |
| 86 | + |
| 87 | + // Should throw if headers already sent |
| 88 | + stream.respond({ |
| 89 | + ':status': 200, |
| 90 | + }); |
| 91 | + common.expectsError( |
| 92 | + () => stream.respondWithFD(fd, { |
| 93 | + [http2.constants.HTTP2_HEADER_CONTENT_TYPE]: 'text/plain' |
| 94 | + }), |
| 95 | + { |
| 96 | + code: 'ERR_HTTP2_HEADERS_SENT', |
| 97 | + message: 'Response has already been initiated.' |
| 98 | + } |
| 99 | + ); |
| 100 | + |
| 101 | + // Should throw if stream already destroyed |
| 102 | + stream.destroy(); |
| 103 | + common.expectsError( |
| 104 | + () => stream.respondWithFD(fd, { |
| 105 | + [http2.constants.HTTP2_HEADER_CONTENT_TYPE]: 'text/plain' |
| 106 | + }), |
| 107 | + { |
| 108 | + code: 'ERR_HTTP2_INVALID_STREAM', |
| 109 | + message: 'The stream has been destroyed' |
| 110 | + } |
| 111 | + ); |
| 112 | +})); |
| 113 | + |
| 114 | +server.listen(0, common.mustCall(() => { |
| 115 | + const client = http2.connect(`http://localhost:${server.address().port}`); |
| 116 | + const req = client.request(); |
| 117 | + |
| 118 | + req.on('streamClosed', common.mustCall(() => { |
| 119 | + client.destroy(); |
| 120 | + server.close(); |
| 121 | + })); |
| 122 | + req.end(); |
| 123 | +})); |
0 commit comments