Skip to content

Commit

Permalink
cookiesUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
vzakharchenko committed Apr 27, 2020
1 parent 486a493 commit e39ab37
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
89 changes: 89 additions & 0 deletions __tests__/src/utils/cookiesUtilsTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
jest.mock('cookie');
jest.mock('../../../src/edge/lambdaEdgeUtils');

const cookie = require('cookie');
const cookiesUtils = require('../../../src/utils/cookiesUtils');

describe('testing cookiesUtils', () => {
beforeEach(() => {
cookie.parse.mockImplementation(() => ({
KEYCLOAK_AWS_undefined: 'testSession',
KEYCLOAK_AWS_SESSION: 'sessionId',
}));
});

afterEach(() => {
});

test('test clearCookies', async () => {
const newResponseHeaders = cookiesUtils.clearCookies({
headers: {
cookie: [{
value: 'testCookie',
}],
},
}, {}, {});
expect(newResponseHeaders).toEqual({
'set-cookie': [
{
key: 'Set-Cookie',
},
],
});
});

test('test getCookies', async () => {
const newResponseHeaders = cookiesUtils.getCookies({
headers: {
cookie: [{
value: 'testCookie',
}],
},
}, {}, {});
expect(newResponseHeaders).toEqual({
'set-cookie': [
{
key: 'Set-Cookie',
},
{
key: 'Set-Cookie',
},
],
});
});

test('test getCookie', async () => {
const cs = cookiesUtils.getCookie({
headers: {
cookie: [{
value: 'testCookie',
}],
},
});
expect(cs).toEqual({
session: 'sessionId',
sessionToken: 'sessionId',
});
});

test('test getCookie null', async () => {
const cs = cookiesUtils.getCookie({
headers: {
},
});
expect(cs).toEqual(null);
});

test('test getCookie null 2', async () => {
cookie.parse.mockImplementation(() => ({
}));
const cs = cookiesUtils.getCookie({
headers: {
cookie: [{
value: 'testCookie',
}],
},
});
expect(cs).toEqual(null);
});
});
2 changes: 1 addition & 1 deletion src/utils/cookiesUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function clearCookies(request, options, responseHeaders = {}) {
return newResponseHeaders;
}

function getCookie(request, cookieName = 'KEYCLOAK_AWS_SESSION') {
function getCookie(request, cookieName = 'SESSION') {
const s = `KEYCLOAK_AWS_${cookieName}`;
const { headers } = request;
if ('cookie' in headers
Expand Down

0 comments on commit e39ab37

Please sign in to comment.