-
Notifications
You must be signed in to change notification settings - Fork 29.1k
/
test-http-hostname-typechecking.js
43 lines (38 loc) · 1.33 KB
/
test-http-hostname-typechecking.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'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()];
vals.forEach((v) => {
assert.throws(
() => http.request({ hostname: v }),
common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options.hostname" property must be one of ' +
'type string, undefined, or null. ' +
`Received type ${typeof v}`
})
);
assert.throws(
() => http.request({ host: v }),
common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options.host" property must be one of ' +
'type string, undefined, or null. ' +
`Received type ${typeof v}`
})
);
});
// These values are OK and should not throw synchronously.
// Only testing for 'hostname' validation so ignore connection errors.
const dontCare = () => {};
['', undefined, null].forEach((v) => {
assert.doesNotThrow(() => {
http.request({ hostname: v }).on('error', dontCare).end();
http.request({ host: v }).on('error', dontCare).end();
});
});