-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: add type checking for hostname
Add type checking for options.hostname / options.host Maintains the use of `undefined` and `null` for default `localhost`, but other falsy values (like `false`, `0` and `NaN`) are rejected.
- Loading branch information
Showing
2 changed files
with
37 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,27 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
// All of these values should cause http.request() to throw synchronously | ||
// when passed as the value of either options.hostname or options.host | ||
const vals = [{}, [], NaN, Infinity, -Infinity, true, false, 1, 0, new Date()]; | ||
|
||
function errCheck(name) { | ||
return new RegExp(`^TypeError: "options\\.${name}" must either be a ` + | ||
'string, undefined or null$'); | ||
} | ||
|
||
vals.forEach((v) => { | ||
assert.throws(() => http.request({hostname: v}), errCheck('hostname')); | ||
assert.throws(() => http.request({host: v}), errCheck('host')); | ||
}); | ||
|
||
// These values are OK and should not throw synchronously | ||
['', undefined, null].forEach((v) => { | ||
assert.doesNotThrow(() => { | ||
http.request({hostname: v}).on('error', common.noop); | ||
http.request({host: v}).on('error', common.noop); | ||
}); | ||
}); |