From 537c4434f11f0f37bf8f4b3e723e85c84056e5ca Mon Sep 17 00:00:00 2001 From: Dion Date: Mon, 8 Jan 2024 08:28:28 +0100 Subject: [PATCH] move cookie code and test --- .../src/shared/cookie/get-cookie.spec.ts | 42 +++++++++++++++++++ .../src/{ => shared/cookie}/get-cookie.ts | 2 +- .../clientapp/src/shared/fetch-post.spec.ts | 8 ++-- .../clientapp/src/shared/fetch-post.ts | 2 +- 4 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 starsky/starsky/clientapp/src/shared/cookie/get-cookie.spec.ts rename starsky/starsky/clientapp/src/{ => shared/cookie}/get-cookie.ts (88%) diff --git a/starsky/starsky/clientapp/src/shared/cookie/get-cookie.spec.ts b/starsky/starsky/clientapp/src/shared/cookie/get-cookie.spec.ts new file mode 100644 index 0000000000..0ef3ebd7eb --- /dev/null +++ b/starsky/starsky/clientapp/src/shared/cookie/get-cookie.spec.ts @@ -0,0 +1,42 @@ +import { GetCookie } from "./get-cookie"; + +describe("GetCookie function", () => { + // Test case 1: Cookie with the specified name exists + test("should return the value of the cookie when it exists", () => { + // Mock document.cookie to simulate the presence of the cookie + document.cookie = "yourCookieName=yourCookieValue; path=/"; + + // Call the function and assert the result + expect(GetCookie("yourCookieName")).toBe("yourCookieValue"); + }); + + // Test case 2: Cookie with the specified name does not exist + test('should return "" when the cookie does not exist', () => { + // Mock document.cookie to simulate an empty cookie + document.cookie = ""; + + // Call the function and assert the result + expect(GetCookie("nonExistentCookie")).toBe(""); + }); + + // Test case 3: Cookie with the specified name has an empty value + test("should return an empty string when the cookie value is empty", () => { + // Mock document.cookie to simulate a cookie with an empty value + document.cookie = "emptyCookie=; path=/"; + + // Call the function and assert the result + expect(GetCookie("emptyCookie")).toBe(""); + }); + + // Test case 4: Cookie with the specified name has special characters + test("should return the correct value when the cookie value has special characters", () => { + // Mock document.cookie to simulate a cookie with special characters + document.cookie = + "specialCookie=%24%25%5E%26%2A%28%29%3D%2B%7B%7D%5B%5D%3B%3A%40%23%21%7C%3C%3E%2C%2F%3F; path=/"; + + // Call the function and assert the result + expect(GetCookie("specialCookie")).toBe( + "%24%25%5E%26%2A%28%29%3D%2B%7B%7D%5B%5D%3B%3A%40%23%21%7C%3C%3E%2C%2F%3F" + ); + }); +}); diff --git a/starsky/starsky/clientapp/src/get-cookie.ts b/starsky/starsky/clientapp/src/shared/cookie/get-cookie.ts similarity index 88% rename from starsky/starsky/clientapp/src/get-cookie.ts rename to starsky/starsky/clientapp/src/shared/cookie/get-cookie.ts index e11015caf0..2cfe72771e 100644 --- a/starsky/starsky/clientapp/src/get-cookie.ts +++ b/starsky/starsky/clientapp/src/shared/cookie/get-cookie.ts @@ -2,5 +2,5 @@ export function GetCookie(name: string): string { const regex = new RegExp("(^| )" + name + "=([^;]+)"); const match = regex.exec(document.cookie); if (match) return match[2]; - return "X-XSRF-TOKEN"; + return ""; } diff --git a/starsky/starsky/clientapp/src/shared/fetch-post.spec.ts b/starsky/starsky/clientapp/src/shared/fetch-post.spec.ts index f2ec4c2a11..60fb6dea95 100644 --- a/starsky/starsky/clientapp/src/shared/fetch-post.spec.ts +++ b/starsky/starsky/clientapp/src/shared/fetch-post.spec.ts @@ -15,7 +15,7 @@ describe("fetch-post", () => { headers: { Accept: "application/json", "Content-Type": "application/x-www-form-urlencoded", - "X-XSRF-TOKEN": "X-XSRF-TOKEN" + "X-XSRF-TOKEN": "" }, method: "post" }); @@ -36,7 +36,7 @@ describe("fetch-post", () => { headers: { Accept: "application/json", "Content-Type": "application/x-www-form-urlencoded", - "X-XSRF-TOKEN": "X-XSRF-TOKEN" + "X-XSRF-TOKEN": "" }, method: "post" }); @@ -62,7 +62,7 @@ describe("fetch-post", () => { headers: { Accept: "application/json", "Content-Type": "application/x-www-form-urlencoded", - "X-XSRF-TOKEN": "X-XSRF-TOKEN" + "X-XSRF-TOKEN": "" }, method: "post" }); @@ -81,7 +81,7 @@ describe("fetch-post", () => { headers: { Accept: "application/json", "Content-Type": "application/x-www-form-urlencoded", - "X-XSRF-TOKEN": "X-XSRF-TOKEN" + "X-XSRF-TOKEN": "" }, method: "post" }); diff --git a/starsky/starsky/clientapp/src/shared/fetch-post.ts b/starsky/starsky/clientapp/src/shared/fetch-post.ts index e1fd82c0fc..aa90edd515 100644 --- a/starsky/starsky/clientapp/src/shared/fetch-post.ts +++ b/starsky/starsky/clientapp/src/shared/fetch-post.ts @@ -1,5 +1,5 @@ import { IConnectionDefault } from "../interfaces/IConnectionDefault"; -import { GetCookie } from "../get-cookie.ts"; +import { GetCookie } from "./cookie/get-cookie.ts"; const FetchPost = async ( url: string, body: string | FormData,