mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
test: minor fix to how cookies are passed around (#55259)
This commit is contained in:
committed by
GitHub
parent
dda9c929b0
commit
fd1bf0dd5a
+16
-1
@@ -1,4 +1,4 @@
|
|||||||
import { getCsrfToken } from './jest.utils';
|
import { getCsrfToken, getCookies } from './jest.utils';
|
||||||
|
|
||||||
const fakeCookies = [
|
const fakeCookies = [
|
||||||
'_csrf=123; Path=/; HttpOnly; SameSite=Strict',
|
'_csrf=123; Path=/; HttpOnly; SameSite=Strict',
|
||||||
@@ -17,3 +17,18 @@ describe('getCsrfToken', () => {
|
|||||||
).toBeUndefined();
|
).toBeUndefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('setCookiesToCookies', () => {
|
||||||
|
test('returns a string of cookies', () => {
|
||||||
|
expect(getCookies(fakeCookies)).toEqual(
|
||||||
|
'_csrf=123; csrf_token=abc-123; sessionId=CV-abc.123'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
test('handles bare cookies', () => {
|
||||||
|
expect(getCookies(['_csrf=123'])).toEqual('_csrf=123');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('throws an error if the cookies are malformed', () => {
|
||||||
|
expect(() => getCookies(['_csrf'])).toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
+8
-1
@@ -34,6 +34,13 @@ export const getCsrfToken = (setCookies: string[]): string | undefined => {
|
|||||||
|
|
||||||
export const ORIGIN = 'https://www.freecodecamp.org';
|
export const ORIGIN = 'https://www.freecodecamp.org';
|
||||||
|
|
||||||
|
export const getCookies = (setCookies: string[]): string => {
|
||||||
|
for (const cookie of setCookies) {
|
||||||
|
expect(cookie).toMatch(/.*=.*/);
|
||||||
|
}
|
||||||
|
return setCookies.map(cookie => cookie.split(';')[0]).join('; ');
|
||||||
|
};
|
||||||
|
|
||||||
export function superRequest(
|
export function superRequest(
|
||||||
resource: string,
|
resource: string,
|
||||||
config: {
|
config: {
|
||||||
@@ -48,7 +55,7 @@ export function superRequest(
|
|||||||
const req = requests[method](resource).set('Origin', ORIGIN);
|
const req = requests[method](resource).set('Origin', ORIGIN);
|
||||||
|
|
||||||
if (setCookies) {
|
if (setCookies) {
|
||||||
void req.set('Cookie', setCookies);
|
void req.set('Cookie', getCookies(setCookies));
|
||||||
}
|
}
|
||||||
|
|
||||||
const csrfToken = (setCookies && getCsrfToken(setCookies)) ?? '';
|
const csrfToken = (setCookies && getCsrfToken(setCookies)) ?? '';
|
||||||
|
|||||||
Reference in New Issue
Block a user