summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-header-validators.js
diff options
context:
space:
mode:
authorosher <osher.filter@gmail.com>2020-04-28 12:30:20 +0300
committerAnna Henningsen <anna@addaleax.net>2020-05-09 07:55:55 +0200
commit96faea137e7d591cab5fa15f4c5fd7cb28da35ee (patch)
tree318fb17a32ce39ee6e1a1cb34f2562ebf589dc46 /test/parallel/test-http-header-validators.js
parentd8a380e13665ef06ffbfa220cb3a7aaaaa17c9fd (diff)
downloadnode-new-96faea137e7d591cab5fa15f4c5fd7cb28da35ee.tar.gz
http: expose http.validate-header-name/value
The use-case is for any framework that provides user mw a response replacement, that collects the desired response state, and applies them only on conclusion. As such a framework, I'd want to validate the header names and values as soon as the user-code provides them. This - to eliminate errors on response-send time, and provide developer stack trace that contains the line that submits the offending values. PR-URL: https://github.com/nodejs/node/pull/33119 Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'test/parallel/test-http-header-validators.js')
-rw-r--r--test/parallel/test-http-header-validators.js62
1 files changed, 62 insertions, 0 deletions
diff --git a/test/parallel/test-http-header-validators.js b/test/parallel/test-http-header-validators.js
new file mode 100644
index 0000000000..fb23bd4885
--- /dev/null
+++ b/test/parallel/test-http-header-validators.js
@@ -0,0 +1,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`);
+}