Skip to content

Add cookie default options #413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/cache/cookieCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,19 @@ export type CookieCacheConfiguration = {
export class CookieCache implements Cache {
private readonly config: CookieCacheConfiguration;

public constructor(config: CookieCacheConfiguration) {
this.config = config;
public constructor(config: CookieCacheConfiguration, defaultSecure = window.location.protocol === 'https:') {
this.config = {
...config,
path: config.path ?? '/',
};

if (defaultSecure && this.config.secure === undefined) {
this.config.secure = true;
}

if (this.config.secure === true && this.config.sameSite === undefined) {
this.config.sameSite = 'none';
}
}

public get(): string | null {
Expand Down
104 changes: 104 additions & 0 deletions test/cache/cookieCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,110 @@ describe('A cookie cache', () => {
expect(cache.get()).toBe('foo');
});

type DefaultOptionsScenario = {
https?: boolean,
options: Partial<CookieCacheConfiguration>,
expectedOptions: {
Secure?: boolean,
SameSite?: 'Strict' | 'Lax' | 'None',
},
};

it.each<DefaultOptionsScenario>([
{
https: true,
options: {},
expectedOptions: {
Secure: true,
SameSite: 'None',
},
},
{
https: true,
options: {
secure: false,
},
expectedOptions: {
},
},
{
https: false,
options: {},
expectedOptions: {
},
},
{
https: false,
options: {
secure: true,
},
expectedOptions: {
Secure: true,
SameSite: 'None',
},
},
{
https: true,
options: {
sameSite: 'lax',
},
expectedOptions: {
Secure: true,
SameSite: 'Lax',
},
},
{
https: false,
options: {
sameSite: 'none',
},
expectedOptions: {
SameSite: 'None',
},
},
{
https: false,
options: {
secure: false,
},
expectedOptions: {
},
},
{
https: undefined,
options: {},
expectedOptions: {
},
},
])('should use default values for missing configuration (https: $https, options: $options)', scenario => {
const {https, options, expectedOptions} = scenario;
const cache = new CookieCache({name: 'cid', ...options}, https);

let jar = '';

jest.spyOn(document, 'cookie', 'set').mockImplementation(value => {
jar = value;
});

cache.put('foo');

expect(jar).not.toBeEmpty();

const cookie: Record<string, string|boolean> = {};

for (const entry of jar.split(';')) {
const [name, value = ''] = entry.split('=');

cookie[decodeURIComponent(name).trim()] = value === '' ? true : decodeURIComponent(value.trim());
}

expect(cookie).toEqual({
cid: 'foo',
Path: '/',
...expectedOptions,
});
});

it('should cache a value using the provided configuration', () => {
const cache = new CookieCache({
name: 'cookie name',
Expand Down
Loading