From da0ef90494ac2a6b709c7b36b70ebe43b5b89421 Mon Sep 17 00:00:00 2001 From: sidwebworks Date: Wed, 24 Aug 2022 22:20:50 +0530 Subject: [PATCH] test: http add tests for content-length mismatch --- .../test-http-content-length-mismatch.js | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 test/parallel/test-http-content-length-mismatch.js diff --git a/test/parallel/test-http-content-length-mismatch.js b/test/parallel/test-http-content-length-mismatch.js new file mode 100644 index 00000000000000..581c1c0c49e1bd --- /dev/null +++ b/test/parallel/test-http-content-length-mismatch.js @@ -0,0 +1,87 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const http = require('http'); + +function shouldThrowOnMismatch() { + const server = http.createServer(common.mustCall((req, res) => { + res.setHeader('Content-Length', 5); + assert.throws(() => { + res.write('hello'); + res.write('a'); + res.statusCode = 200; + }, { + code: 'ERR_HTTP_CONTENT_LENGTH_MISMATCH' + }) + res.end(); + })); + + server.listen(0, () => { + http.get({ + port: server.address().port, + }, common.mustCall((res) => { + console.log(res.statusMessage); + res.resume(); + assert.strictEqual(res.statusCode, 200); + server.close(); + })); + }); +} + +function shouldNotThrow() { + const server = http.createServer(common.mustCall((req, res) => { + assert.doesNotThrow(() => { + res.write('hello'); + res.write('a'); + res.statusCode = 200; + }) + res.end(); + })); + + server.listen(0, () => { + http.get({ + port: server.address().port, + headers: { + 'Content-Length': '6' + } + }, common.mustCall((res) => { + console.log(res.statusMessage); + res.resume(); + assert.strictEqual(res.statusCode, 200); + server.close(); + })); + }); +} + +function shouldOverwriteContentLength() { + const server = http.createServer(common.mustCall((req, res) => { + res.writeHead(200, { + 'Content-Length': '1' + }) + assert.throws(() => { + res.write('hello'); + res.write('a'); + res.statusCode = 200; + }) + res.end(); + })); + + server.listen(0, () => { + http.get({ + port: server.address().port, + headers: { + 'Content-Length': '6' + } + }, common.mustCall((res) => { + console.log(res.statusMessage); + res.resume(); + assert.strictEqual(res.statusCode, 200); + server.close(); + })); + }); +} + +shouldThrowOnMismatch() +shouldNotThrow() +shouldOverwriteContentLength()