Skip to content

Commit

Permalink
fix(headers): case sensitivity fix
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed Mar 7, 2020
1 parent a41f766 commit c119eae
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
8 changes: 5 additions & 3 deletions packages/app/src/Transport/HeadersAbstract.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import {isBoolean, isFunction, assert} from "@typesafeunit/util";
import {assert, curry, isBoolean, isFunction} from "@typesafeunit/util";
import {compare} from "@typesafeunit/util/dist/string";
import {HeaderAssertValue, IHeaders} from "../interfaces";
import {KeyValueReadonlyMap} from "./KeyValueReadonlyMap";

export abstract class HeadersAbstract extends KeyValueReadonlyMap implements IHeaders {
public assert(header: string, expected: HeaderAssertValue): void {
const clientValue = this.get(header);
const clientValue = this.get(header.toLowerCase());
const cmp = curry(compare, clientValue);
if (Array.isArray(expected)) {
assert(
expected.some((value) => value === clientValue),
expected.some(cmp),
`Wrong header "${header}" value, allowed: ${expected.join(", ")}`,
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/Transport/Request/JSONTransform.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {IRequest} from "../../interfaces";

export const JSONTransform = async <T extends object = object>(request: IRequest): Promise<T> => {
request.headers.assert("Content-Type", ["application/json"]);
request.headers.assert("content-type", ["application/json"]);
const buffer = await request.getBuffer();
return JSON.parse(buffer.toString("utf-8"));
};
5 changes: 5 additions & 0 deletions packages/util/src/function.ts
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
export const noop = () => void 0;
export function curry<A extends any[], T, S>(fn: (arg1: T, ...args: A) => S, value: T) {
return (...args: A) => {
return fn(value, ...args);
};
}
3 changes: 3 additions & 0 deletions packages/util/src/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function compare(str1: string, str2: string) {
return str1.toLowerCase() === str2.toLowerCase();
}

0 comments on commit c119eae

Please sign in to comment.