Skip to content

Commit

Permalink
test: http add tests for content-length mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
sidwebworks committed Aug 26, 2022
1 parent b1e44ab commit 50e63f7
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions 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()

0 comments on commit 50e63f7

Please sign in to comment.