diff --git a/src/encoding.ts b/src/encoding.ts index 0708453..eed5550 100644 --- a/src/encoding.ts +++ b/src/encoding.ts @@ -9,9 +9,9 @@ const SLASH_RE = /\//g; // %2F const EQUAL_RE = /=/g; // %3D const IM_RE = /\?/g; // %3F const PLUS_RE = /\+/g; // %2B +const BACKTICK_RE = /`/g; // %60 const ENC_CARET_RE = /%5e/gi; // ^ -const ENC_BACKTICK_RE = /%60/gi; // ` const ENC_CURLY_OPEN_RE = /%7b/gi; // { const ENC_PIPE_RE = /%7c/gi; // | const ENC_CURLY_CLOSE_RE = /%7d/gi; // } @@ -64,7 +64,7 @@ export function encodeQueryValue(input: QueryValue): string { .replace(ENC_SPACE_RE, "+") .replace(HASH_RE, "%23") .replace(AMPERSAND_RE, "%26") - .replace(ENC_BACKTICK_RE, "`") + .replace(BACKTICK_RE, "%60") .replace(ENC_CARET_RE, "^") .replace(SLASH_RE, "%2F") ); diff --git a/test/encoding.test.ts b/test/encoding.test.ts index c110b23..d561929 100644 --- a/test/encoding.test.ts +++ b/test/encoding.test.ts @@ -8,6 +8,7 @@ import { encodeParam, decode, decodeQueryKey, + decodeQueryValue } from "../src/"; describe("encode", () => { @@ -85,6 +86,7 @@ describe("encodeQueryValue", () => { const tests = [ { input: "hello world", out: "hello+world" }, { input: "hello+world", out: "hello%2Bworld" }, + { input: "a`b", out: "a%60b" }, { input: "key=value", out: "key=value" }, { input: true, out: "true" }, { input: 42, out: "42" }, @@ -112,6 +114,7 @@ describe("encodeQueryKey", () => { { input: "key=value", out: "key%3Dvalue" }, { input: 123, out: "123" }, { input: "=value", out: "%3Dvalue" }, + { input: "a`b", out : "a%60b" } ]; for (const t of tests) { @@ -199,6 +202,7 @@ describe("decodeQueryKey", () => { { input: "%3Dvalue", out: "=value" }, { input: "key+with+space", out: "key with space" }, { input: "key%2bwith%2bplus", out: "key+with+plus" }, + { input: "a%60b", out: "a`b" } ]; for (const t of tests) { @@ -207,3 +211,21 @@ describe("decodeQueryKey", () => { }); } }); + +describe("decodeQueryValue", () => { + const tests = [ + { input: "key", out: "key" }, + { input: "key%3Dvalue", out: "key=value" }, + { input: "123", out: "123" }, + { input: "%3Dvalue", out: "=value" }, + { input: "key+with+space", out: "key with space" }, + { input: "key%2bwith%2bplus", out: "key+with+plus" }, + { input: "a%60b", out: "a`b" } + ]; + + for (const t of tests) { + test(t.input.toString(), () => { + expect(decodeQueryValue(t.input.toString())).toStrictEqual(t.out); + }); + } +});