Skip to content

Commit

Permalink
Merge pull request #6 from joshuajaco/improve-query-and-header-matching
Browse files Browse the repository at this point in the history
Improve query and header matching
  • Loading branch information
joshuajaco committed Jul 29, 2023
2 parents 005a5bd + 6878516 commit ca905c8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
12 changes: 7 additions & 5 deletions src/matchRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type MatcherObj = {
method?: string;
path?: string | RegExp;
query?: express.Request["query"];
headers?: Record<string, string>;
headers?: Record<string, string | undefined>;
body?: string | object;
};

Expand Down Expand Up @@ -42,13 +42,15 @@ function matchPath(matcher: MatcherObj, req: express.Request) {

function matchQuery(matcher: MatcherObj, req: express.Request) {
if (!matcher.query) return true;
return deepEqual(matcher.query, req.query, { strict: true });
return Object.entries(matcher.query).every(([k, v]) =>
deepEqual(req.query[k], v, { strict: true }),
);
}

function matchHeaders(matcher: MatcherObj, req: express.Request) {
return (
!matcher.headers ||
Object.entries(matcher.headers).every(([k, v]) => req.headers[k] === v)
if (!matcher.headers) return true;
return Object.entries(matcher.headers).every(
([k, v]) => req.headers[k.toLowerCase()] === v,
);
}

Expand Down
21 changes: 15 additions & 6 deletions tests/matchRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ describe("matchRequest", () => {
});

it("matches with query", () => {
const matcher = { query: { foo: "bar", array: ["a", "b", "c"] } };
const matcher = {
query: { foo: "bar", array: ["a", "b", "c"], baz: undefined },
};

const matches = [
{ query: { foo: "bar", array: ["a", "b", "c"] } },
Expand All @@ -62,6 +64,7 @@ describe("matchRequest", () => {
];

const fails = [
{ query: { foo: "bar", array: ["a", "b", "c"], baz: "1" } },
{ path: "/foo" },
{ query: { array: ["a", "b", "c"] } },
{ query: { foo: "bar" } },
Expand All @@ -74,16 +77,22 @@ describe("matchRequest", () => {
});

it("matches with headers", () => {
const matcher = { headers: { foo: "bar" } };
const matcher = {
headers: { "Content-Type": "application/json", bar: undefined },
};

const matches = [
{ headers: { foo: "bar" } },
{ headers: { foo: "bar" }, path: "/foo" },
{ headers: { foo: "bar" }, query: { foo: "bar" } },
{ headers: { "Content-Type": "application/json" } },
{ headers: { "content-type": "application/json" }, path: "/foo" },
{
headers: { "CONTENT-TYPE": "application/json", foo: "bar" },
query: { foo: "bar" },
},
];

const fails = [
{ headers: { foo: "baz" } },
{ headers: { "Content-Type": "application/json", bar: "1" } },
{ headers: { "Content-Type": "application/pdf" } },
{ headers: { foo: "baz" }, path: "/foo" },
{ path: "/foo" },
];
Expand Down

0 comments on commit ca905c8

Please sign in to comment.