|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const http = require('http'); |
| 6 | + |
| 7 | +const MAX_BODY_LENGTH = 1024 * 1024; |
| 8 | + |
| 9 | +const server = http.createServer(common.mustCall((request, response) => { |
| 10 | + let body = ''; |
| 11 | + |
| 12 | + request.setEncoding('utf8'); |
| 13 | + request.on('data', function onData(chunk) { |
| 14 | + body += chunk; |
| 15 | + if (body.length > MAX_BODY_LENGTH) { |
| 16 | + response.writeHead(413); |
| 17 | + response.end(); |
| 18 | + request.off('data', onData); |
| 19 | + request.destroy(); |
| 20 | + } |
| 21 | + }); |
| 22 | + |
| 23 | + request.on('end', common.mustNotCall()); |
| 24 | + request.on('close', common.mustCall(() => { |
| 25 | + assert.strictEqual(request.complete, false); |
| 26 | + assert.ok(body.length > MAX_BODY_LENGTH); |
| 27 | + })); |
| 28 | +})); |
| 29 | + |
| 30 | +server.listen(0, common.mustCall(() => { |
| 31 | + const body = JSON.stringify({ |
| 32 | + a: 'x'.repeat(512 * 1024), |
| 33 | + b: 'x'.repeat(512 * 1024), |
| 34 | + c: 'x'.repeat(512 * 1024), |
| 35 | + d: 'x'.repeat(512 * 1024), |
| 36 | + }, null, '\t'); |
| 37 | + |
| 38 | + let response; |
| 39 | + const req = http.request({ |
| 40 | + method: 'POST', |
| 41 | + port: server.address().port, |
| 42 | + headers: { |
| 43 | + 'content-type': 'application/json', |
| 44 | + }, |
| 45 | + }, common.mustCall((res) => { |
| 46 | + response = res; |
| 47 | + assert.strictEqual(res.statusCode, 413); |
| 48 | + // Deliberately do not consume the response body. A complete HTTP response |
| 49 | + // should not be followed by a late ClientRequest ECONNRESET. |
| 50 | + })); |
| 51 | + |
| 52 | + req.on('error', common.mustNotCall()); |
| 53 | + req.on('close', common.mustCall(() => { |
| 54 | + assert(response); |
| 55 | + assert.strictEqual(response.complete, true); |
| 56 | + server.close(); |
| 57 | + })); |
| 58 | + |
| 59 | + req.write(body); |
| 60 | + req.end(); |
| 61 | +})); |
0 commit comments