11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst { validateHeaderName, validateHeaderValue } = require('http');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// Expected static methods
71cb0ef41Sopenharmony_ciisFunc(validateHeaderName, 'validateHeaderName');
81cb0ef41Sopenharmony_ciisFunc(validateHeaderValue, 'validateHeaderValue');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// Expected to be useful as static methods
111cb0ef41Sopenharmony_ciconsole.log('validateHeaderName');
121cb0ef41Sopenharmony_ci// - when used with valid header names - should not throw
131cb0ef41Sopenharmony_ci[
141cb0ef41Sopenharmony_ci  'user-agent',
151cb0ef41Sopenharmony_ci  'USER-AGENT',
161cb0ef41Sopenharmony_ci  'User-Agent',
171cb0ef41Sopenharmony_ci  'x-forwarded-for',
181cb0ef41Sopenharmony_ci].forEach((name) => {
191cb0ef41Sopenharmony_ci  console.log('does not throw for "%s"', name);
201cb0ef41Sopenharmony_ci  validateHeaderName(name);
211cb0ef41Sopenharmony_ci});
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci// - when used with invalid header names:
241cb0ef41Sopenharmony_ci[
251cb0ef41Sopenharmony_ci  'איקס-פורוורד-פור',
261cb0ef41Sopenharmony_ci  'x-forwarded-fםr',
271cb0ef41Sopenharmony_ci].forEach((name) => {
281cb0ef41Sopenharmony_ci  console.log('throws for: "%s"', name.slice(0, 50));
291cb0ef41Sopenharmony_ci  assert.throws(
301cb0ef41Sopenharmony_ci    () => validateHeaderName(name),
311cb0ef41Sopenharmony_ci    { code: 'ERR_INVALID_HTTP_TOKEN' }
321cb0ef41Sopenharmony_ci  );
331cb0ef41Sopenharmony_ci});
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ciconsole.log('validateHeaderValue');
361cb0ef41Sopenharmony_ci// - when used with valid header values - should not throw
371cb0ef41Sopenharmony_ci[
381cb0ef41Sopenharmony_ci  ['x-valid', 1],
391cb0ef41Sopenharmony_ci  ['x-valid', '1'],
401cb0ef41Sopenharmony_ci  ['x-valid', 'string'],
411cb0ef41Sopenharmony_ci].forEach(([name, value]) => {
421cb0ef41Sopenharmony_ci  console.log('does not throw for "%s"', name);
431cb0ef41Sopenharmony_ci  validateHeaderValue(name, value);
441cb0ef41Sopenharmony_ci});
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci// - when used with invalid header values:
471cb0ef41Sopenharmony_ci[
481cb0ef41Sopenharmony_ci  // [header, value, expectedCode]
491cb0ef41Sopenharmony_ci  ['x-undefined', undefined, 'ERR_HTTP_INVALID_HEADER_VALUE'],
501cb0ef41Sopenharmony_ci  ['x-bad-char', 'לא תקין', 'ERR_INVALID_CHAR'],
511cb0ef41Sopenharmony_ci].forEach(([name, value, code]) => {
521cb0ef41Sopenharmony_ci  console.log('throws %s for: "%s: %s"', code, name, value);
531cb0ef41Sopenharmony_ci  assert.throws(
541cb0ef41Sopenharmony_ci    () => validateHeaderValue(name, value),
551cb0ef41Sopenharmony_ci    { code }
561cb0ef41Sopenharmony_ci  );
571cb0ef41Sopenharmony_ci});
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci// Misc.
601cb0ef41Sopenharmony_cifunction isFunc(v, ttl) {
611cb0ef41Sopenharmony_ci  assert.ok(v.constructor === Function, `${ttl} is expected to be a function`);
621cb0ef41Sopenharmony_ci}
63