-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: #33119 Reviewed-By: Anna Henningsen <[email protected]>
- Loading branch information
1 parent
b061655
commit f33e866
Showing
4 changed files
with
138 additions
and
0 deletions.
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
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,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`); | ||
} |