-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: verify client method is a string
Prior to this commit, it was possible to pass a truthy non-string value as the HTTP method to the HTTP client, resulting in an exception being thrown. This commit adds validation to the method. PR-URL: #10111 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
1 parent
6967ed4
commit df39784
Showing
2 changed files
with
45 additions
and
1 deletion.
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
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,40 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
const expectedSuccesses = [undefined, null, 'GET', 'post']; | ||
let requestCount = 0; | ||
|
||
const server = http.createServer((req, res) => { | ||
requestCount++; | ||
res.end(); | ||
|
||
if (expectedSuccesses.length === requestCount) { | ||
server.close(); | ||
} | ||
}).listen(0, test); | ||
|
||
function test() { | ||
function fail(input) { | ||
assert.throws(() => { | ||
http.request({ method: input, path: '/' }, common.fail); | ||
}, /^TypeError: Method must be a string$/); | ||
} | ||
|
||
fail(-1); | ||
fail(1); | ||
fail(0); | ||
fail({}); | ||
fail(true); | ||
fail(false); | ||
fail([]); | ||
|
||
function ok(method) { | ||
http.request({ method: method, port: server.address().port }).end(); | ||
} | ||
|
||
expectedSuccesses.forEach((method) => { | ||
ok(method); | ||
}); | ||
} |