From 7c400be8221217b24da2a63865fa7b4b94d71a1e Mon Sep 17 00:00:00 2001 From: Christian Cook <3473396+CookieCookson@users.noreply.github.com> Date: Sun, 24 Sep 2023 14:36:45 +0100 Subject: [PATCH] Fix issue with false query parameter detection on URLs with no search parameters --- .../getSearchQueryParamsAsObject.ts | 1 + .../getSearchQueryParamsAsObject.unit.test.ts | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/helpers/getSearchQueryParamsAsObject/getSearchQueryParamsAsObject.ts b/src/helpers/getSearchQueryParamsAsObject/getSearchQueryParamsAsObject.ts index a3481c7..4f3d4d1 100644 --- a/src/helpers/getSearchQueryParamsAsObject/getSearchQueryParamsAsObject.ts +++ b/src/helpers/getSearchQueryParamsAsObject/getSearchQueryParamsAsObject.ts @@ -30,6 +30,7 @@ export function getSearchQueryParamsAsObject(str: string): { [key: string]: any; } { const obj: { [key: string]: any } = {}; + if (str.indexOf("?") === -1) return obj; let queryString = str.substring(str.indexOf("?")); queryString = queryString.split("#").shift(); if (!queryString) return obj; diff --git a/src/helpers/getSearchQueryParamsAsObject/getSearchQueryParamsAsObject.unit.test.ts b/src/helpers/getSearchQueryParamsAsObject/getSearchQueryParamsAsObject.unit.test.ts index f56bf34..c6ceb56 100644 --- a/src/helpers/getSearchQueryParamsAsObject/getSearchQueryParamsAsObject.unit.test.ts +++ b/src/helpers/getSearchQueryParamsAsObject/getSearchQueryParamsAsObject.unit.test.ts @@ -1,8 +1,16 @@ import { getSearchQueryParamsAsObject } from "./getSearchQueryParamsAsObject"; import { Actor } from "../../XAPI"; +test("handles url with no query parameters", () => { + return expect( + Object.keys(getSearchQueryParamsAsObject("http://localhost:1234/")).length + ).toEqual(0); +}); + test("handles empty querystring", () => { - return expect(getSearchQueryParamsAsObject("")).toMatchObject({}); + return expect(Object.keys(getSearchQueryParamsAsObject("")).length).toEqual( + 0 + ); }); test("converts querystring into object", () => {