fix(params): match @header names without case (GitHub #37)
Node's IncomingHttpHeaders and the fetch adapter's headersToRecord
both store request header names lower-cased, but resolveOne looked a
@header/@headers key up exactly as the decorator wrote it. Any name
with a capital letter, such as @header('Content-Type'), therefore
reached the handler as undefined.
resolveOne now lower-cases the key for the headers source before the
lookup. A picked bag (@headers([...])) keeps the names as written, so a
handler reads back the key it asked for; @headers() with no argument
still returns the whole lower-cased bag.
test/params.test.ts covers the single and picked forms on resolveArgs;
test/header-case.test.ts sends a request over app.fetch and the Node
server. All three fail without the fix.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
4 files changed
+57
-2
No files matched your search
+5
-2
@@ -147,14 +147,17 @@ function resolveOne(spec: ArgSpec, context: Context): unknown {
|
||||
const bag = spec.source === 'cookies' ? cookiesOf(context) : context[spec.source];
|
||||
if (typeof bag !== 'object' || bag === null) return bag; // can't pick from a non-object; return as-is
|
||||
const values = bag as Record<string, unknown>;
|
||||
// Every adapter stores header names lower-cased, so `@header('Content-Type')` looks up
|
||||
// `content-type` (GitHub #37). A picked bag keeps the names as the handler wrote them.
|
||||
const at = spec.source === 'headers' ? (key: string) => values[key.toLowerCase()] : (key: string) => values[key];
|
||||
|
||||
if (spec.keys) {
|
||||
const picked: Record<string, unknown> = {};
|
||||
for (const key of spec.keys) picked[key] = values[key];
|
||||
for (const key of spec.keys) picked[key] = at(key);
|
||||
return picked;
|
||||
}
|
||||
|
||||
if (spec.key !== undefined) return values[spec.key];
|
||||
if (spec.key !== undefined) return at(spec.key);
|
||||
return values;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user