summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-header-validators.js
blob: fb23bd4885c7668c5dddc9d157613aa2bf0ea177 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
require('../common');
const assert = require('assert');
const { validateHeaderName, validateHeaderValue } = require('http');

// Expected static methods
isFunc(validateHeaderName, 'validateHeaderName');
isFunc(validateHeaderValue, 'validateHeaderValue');

// Expected to be useful as static methods
console.log('validateHeaderName');
// - when used with valid header names - should not throw
[
  'user-agent',
  'USER-AGENT',
  'User-Agent',
  'x-forwarded-for'
].forEach((name) => {
  console.log('does not throw for "%s"', name);
  validateHeaderName(name);
});

// - when used with invalid header names:
[
  'איקס-פורוורד-פור',
  'x-forwarded-fםr',
].forEach((name) => {
  console.log('throws for: "%s"', name.slice(0, 50));
  assert.throws(
    () => validateHeaderName(name),
    { code: 'ERR_INVALID_HTTP_TOKEN' }
  );
});

console.log('validateHeaderValue');
// - when used with valid header values - should not throw
[
  ['x-valid', 1],
  ['x-valid', '1'],
  ['x-valid', 'string'],
].forEach(([name, value]) => {
  console.log('does not throw for "%s"', name);
  validateHeaderValue(name, value);
});

// - when used with invalid header values:
[
  // [header, value, expectedCode]
  ['x-undefined', undefined, 'ERR_HTTP_INVALID_HEADER_VALUE'],
  ['x-bad-char', 'לא תקין', 'ERR_INVALID_CHAR'],
].forEach(([name, value, code]) => {
  console.log('throws %s for: "%s: %s"', code, name, value);
  assert.throws(
    () => validateHeaderValue(name, value),
    { code }
  );
});

// Misc.
function isFunc(v, ttl) {
  assert.ok(v.constructor === Function, `${ttl} is expected to be a function`);
}