Skip to content

Fix query matching against list query values #450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import { HttpRequest } from "@aws-sdk/protocol-http";
import { HttpBindingMux, UriSpec } from ".";

describe("simple matching", () => {
const router = new HttpBindingMux<"Test", "A" | "LessSpecificA" | "Greedy" | "MiddleGreedy" | "Delete">([
const router = new HttpBindingMux<
"Test",
"A" | "LessSpecificA" | "Greedy" | "MiddleGreedy" | "Delete" | "QueryKeyOnly"
>([
new UriSpec("GET", [{ type: "path_literal", value: "a" }, { type: "path" }, { type: "path" }], [], {
service: "Test",
operation: "A",
Expand Down Expand Up @@ -46,6 +49,10 @@ describe("simple matching", () => {
],
{ service: "Test", operation: "Delete" }
),
new UriSpec("GET", [{ type: "path_literal", value: "query_key_only" }], [{ type: "query_literal", key: "foo" }], {
service: "Test",
operation: "QueryKeyOnly",
}),
]);

const matches: { [idx: string]: HttpRequest[] } = {
Expand All @@ -68,10 +75,20 @@ describe("simple matching", () => {
new HttpRequest({ method: "GET", path: "/mg/a/b/c/d/z", query: { abc: "def" } }),
],
"Test#Delete": [
new HttpRequest({ method: "DELETE", path: "/", query: { foo: "bar", baz: "quux" } }),
new HttpRequest({ method: "DELETE", path: "/", query: { foo: ["bar"], baz: "quux" } }),
new HttpRequest({ method: "DELETE", path: "/", query: { foo: ["bar", "corge"], baz: "quux" } }),
new HttpRequest({ method: "DELETE", path: "/", query: { foo: "bar", baz: "quux" } }),
new HttpRequest({ method: "DELETE", path: "/", query: { foo: "bar", baz: null } }),
new HttpRequest({ method: "DELETE", path: "", query: { foo: "bar", baz: ["quux", "grault"] } }),
],
"Test#QueryKeyOnly": [
new HttpRequest({ method: "GET", path: "/query_key_only", query: { foo: "bar" } }),
new HttpRequest({ method: "GET", path: "/query_key_only", query: { foo: null } }),
new HttpRequest({ method: "GET", path: "/query_key_only", query: { foo: "" } }),
// this is actually what /query_key_only?foo will look like behind APIGateway
new HttpRequest({ method: "GET", path: "/query_key_only", query: { foo: [""] } }),
],
};

const misses = [
Expand All @@ -87,7 +104,6 @@ describe("simple matching", () => {
new HttpRequest({ method: "GET", path: "/mg/q" }),
new HttpRequest({ method: "GET", path: "/mg/z" }),
new HttpRequest({ method: "GET", path: "/mg/a/b/z/c" }),
new HttpRequest({ method: "DELETE", path: "/", query: { foo: ["bar", "corge"], baz: "quux" } }),
new HttpRequest({ method: "DELETE", path: "/", query: { foo: "bar" } }),
new HttpRequest({ method: "DELETE", path: "/", query: { baz: "quux" } }),
new HttpRequest({ method: "DELETE", path: "/" }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ export class UriSpec<S extends string, O extends string> {
return false;
}
if (querySegment.type === "query_literal") {
if (querySegment.value && querySegment.value !== req.query[querySegment.key]) {
const input_query_value = req.query[querySegment.key];
if (Array.isArray(input_query_value)) {
if (querySegment.value && !input_query_value.includes(querySegment.value)) {
return false;
}
} else if (querySegment.value && querySegment.value !== input_query_value) {
return false;
}
}
Expand Down