diff options
author | mash-graz <mash-graz@users.noreply.github.com> | 2024-03-10 23:46:05 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-10 22:46:05 +0000 |
commit | 16dbbfa64a5d2905580535c52c1db51d1cf5b89f (patch) | |
tree | 5cf86fd3955309a86149322dd2664d15b33c31ea /tests/node_compat | |
parent | 0e644503690f23ced5c13c5d3e59592ce1858018 (diff) |
fix(node:http) Export `validateHeaderName` and `validateHeaderValue` functions (#22616)
Modify `_http_outgoing.ts` to support the extended signature of
`validateHeaderName()` used since node v19.5.0/v18.14.0 by adding the
`label` parameter. (see:
https://nodejs.org/api/http.html#httpvalidateheadernamename-label)
Making both validation functions accessible as public exports of
`node:http`
Fixes: #22614
Diffstat (limited to 'tests/node_compat')
-rw-r--r-- | tests/node_compat/config.jsonc | 1 | ||||
-rw-r--r-- | tests/node_compat/test/parallel/test-http-header-validators.js | 69 |
2 files changed, 70 insertions, 0 deletions
diff --git a/tests/node_compat/config.jsonc b/tests/node_compat/config.jsonc index d84cc4dd2..3fde8be5a 100644 --- a/tests/node_compat/config.jsonc +++ b/tests/node_compat/config.jsonc @@ -365,6 +365,7 @@ // TODO(lev): ClientRequest.socket is not polyfilled so this test keeps // failing //"test-http-client-set-timeout.js", + "test-http-header-validators.js", "test-http-localaddress.js", // TODO(bartlomieju): temporarily disabled while we iterate on the HTTP client // "test-http-outgoing-buffer.js", diff --git a/tests/node_compat/test/parallel/test-http-header-validators.js b/tests/node_compat/test/parallel/test-http-header-validators.js new file mode 100644 index 000000000..2b2f82339 --- /dev/null +++ b/tests/node_compat/test/parallel/test-http-header-validators.js @@ -0,0 +1,69 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file + +// Copyright Joyent and Node contributors. All rights reserved. MIT license. +// Taken from Node 18.12.1 +// This file is automatically generated by `tools/node_compat/setup.ts`. Do not modify this file manually. + +'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`); +} |