forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: http add tests for content-length mismatch
- Loading branch information
1 parent
b1e44ab
commit 50e63f7
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |