Skip to content

Commit

Permalink
move cookie code and test
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Jan 8, 2024
1 parent 4a23a2c commit 537c443
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
42 changes: 42 additions & 0 deletions starsky/starsky/clientapp/src/shared/cookie/get-cookie.spec.ts
Original file line number Diff line number Diff line change
@@ -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"
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
}
8 changes: 4 additions & 4 deletions starsky/starsky/clientapp/src/shared/fetch-post.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
});
Expand All @@ -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"
});
Expand All @@ -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"
});
Expand All @@ -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"
});
Expand Down
2 changes: 1 addition & 1 deletion starsky/starsky/clientapp/src/shared/fetch-post.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 537c443

Please sign in to comment.