|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +if (!common.hasCrypto) |
| 5 | + common.skip('missing crypto'); |
| 6 | + |
| 7 | +// A request whose stream is reset by the peer before END_STREAM has an |
| 8 | +// incomplete body. Like http1, the compat request must not emit 'end' for it. |
| 9 | + |
| 10 | +const assert = require('assert'); |
| 11 | +const http2 = require('http2'); |
| 12 | +const { finished } = require('stream'); |
| 13 | + |
| 14 | +const codes = [ |
| 15 | + http2.constants.NGHTTP2_NO_ERROR, |
| 16 | + http2.constants.NGHTTP2_CANCEL, |
| 17 | +]; |
| 18 | + |
| 19 | +const server = http2.createServer(common.mustCall((req, res) => { |
| 20 | + // Tell the client the partial body arrived, so it can reset the stream. |
| 21 | + req.once('data', common.mustCall(() => res.writeHead(200).flushHeaders())); |
| 22 | + req.on('end', common.mustNotCall()); |
| 23 | + req.on('aborted', common.mustCall()); |
| 24 | + req.on('error', common.expectsError({ code: 'ERR_HTTP2_STREAM_ABORTED' })); |
| 25 | + req.on('close', common.mustCall(() => { |
| 26 | + assert.strictEqual(req.aborted, true); |
| 27 | + assert.strictEqual(req.readableEnded, false); |
| 28 | + })); |
| 29 | + finished(req, common.mustCall((err) => { |
| 30 | + assert.ok(err); |
| 31 | + })); |
| 32 | +}, codes.length)); |
| 33 | + |
| 34 | +server.listen(0, common.mustCall(() => { |
| 35 | + const client = http2.connect(`http://localhost:${server.address().port}`); |
| 36 | + let pending = codes.length; |
| 37 | + |
| 38 | + for (const code of codes) { |
| 39 | + const req = client.request({ |
| 40 | + ':method': 'POST', |
| 41 | + 'content-length': '1000', |
| 42 | + }); |
| 43 | + req.on('error', () => {}); |
| 44 | + req.on('close', common.mustCall(() => { |
| 45 | + if (--pending === 0) { |
| 46 | + client.close(); |
| 47 | + server.close(); |
| 48 | + } |
| 49 | + })); |
| 50 | + // Send part of the body, then reset the stream without END_STREAM once |
| 51 | + // the server has seen it. |
| 52 | + req.write('{"action":"partial"}'); |
| 53 | + req.on('response', common.mustCall(() => req.close(code))); |
| 54 | + } |
| 55 | +})); |
| 56 | + |
| 57 | +// A complete body followed by a reset still ends the request normally. |
| 58 | +{ |
| 59 | + const server = http2.createServer(common.mustCall((req, res) => { |
| 60 | + let body = ''; |
| 61 | + req.setEncoding('utf8'); |
| 62 | + req.on('data', (chunk) => body += chunk); |
| 63 | + req.on('end', common.mustCall(() => { |
| 64 | + assert.strictEqual(body, 'complete'); |
| 65 | + res.writeHead(200).flushHeaders(); |
| 66 | + })); |
| 67 | + req.on('error', common.mustNotCall()); |
| 68 | + })); |
| 69 | + |
| 70 | + server.listen(0, common.mustCall(() => { |
| 71 | + const client = http2.connect(`http://localhost:${server.address().port}`); |
| 72 | + const req = client.request({ ':method': 'POST' }); |
| 73 | + req.on('error', () => {}); |
| 74 | + req.on('response', common.mustCall(() => { |
| 75 | + req.close(http2.constants.NGHTTP2_CANCEL); |
| 76 | + })); |
| 77 | + req.on('close', common.mustCall(() => { |
| 78 | + client.close(); |
| 79 | + server.close(); |
| 80 | + })); |
| 81 | + req.end('complete'); |
| 82 | + })); |
| 83 | +} |
| 84 | + |
| 85 | +// A stream closed locally by the server before the peer sends END_STREAM also |
| 86 | +// has an incomplete body and must not emit 'end'. |
| 87 | +{ |
| 88 | + const server = http2.createServer(common.mustCall((req, res) => { |
| 89 | + req.once('data', common.mustCall(() => { |
| 90 | + // Tear the stream down locally while the body is still in flight. |
| 91 | + req.stream.destroy(); |
| 92 | + })); |
| 93 | + req.on('end', common.mustNotCall()); |
| 94 | + req.on('aborted', common.mustCall()); |
| 95 | + req.on('error', common.mustNotCall()); |
| 96 | + req.on('close', common.mustCall(() => { |
| 97 | + assert.strictEqual(req.aborted, true); |
| 98 | + assert.strictEqual(req.readableEnded, false); |
| 99 | + })); |
| 100 | + finished(req, common.mustCall((err) => { |
| 101 | + assert.ok(err); |
| 102 | + })); |
| 103 | + })); |
| 104 | + |
| 105 | + server.listen(0, common.mustCall(() => { |
| 106 | + const client = http2.connect(`http://localhost:${server.address().port}`); |
| 107 | + const req = client.request({ |
| 108 | + ':method': 'POST', |
| 109 | + 'content-length': '1000', |
| 110 | + }); |
| 111 | + req.on('error', () => {}); |
| 112 | + req.on('close', common.mustCall(() => { |
| 113 | + client.close(); |
| 114 | + server.close(); |
| 115 | + })); |
| 116 | + req.write('{"action":"partial"}'); |
| 117 | + })); |
| 118 | +} |
| 119 | + |
| 120 | +// An unexpected session teardown mid-body also produces an incomplete request |
| 121 | +// that must not emit 'end'. |
| 122 | +{ |
| 123 | + const server = http2.createServer(common.mustCall((req, res) => { |
| 124 | + req.once('data', common.mustCall(() => { |
| 125 | + req.stream.session.destroy(); |
| 126 | + })); |
| 127 | + req.on('end', common.mustNotCall()); |
| 128 | + req.on('aborted', common.mustCall()); |
| 129 | + req.on('error', common.mustNotCall()); |
| 130 | + req.on('close', common.mustCall(() => { |
| 131 | + assert.strictEqual(req.aborted, true); |
| 132 | + assert.strictEqual(req.readableEnded, false); |
| 133 | + })); |
| 134 | + finished(req, common.mustCall((err) => { |
| 135 | + assert.ok(err); |
| 136 | + })); |
| 137 | + })); |
| 138 | + |
| 139 | + server.listen(0, common.mustCall(() => { |
| 140 | + const client = http2.connect(`http://localhost:${server.address().port}`); |
| 141 | + const req = client.request({ |
| 142 | + ':method': 'POST', |
| 143 | + 'content-length': '1000', |
| 144 | + }); |
| 145 | + req.on('error', () => {}); |
| 146 | + req.on('close', common.mustCall(() => { |
| 147 | + client.close(); |
| 148 | + server.close(); |
| 149 | + })); |
| 150 | + req.write('{"action":"partial"}'); |
| 151 | + })); |
| 152 | +} |
0 commit comments