/* * Copyright (C) Sapphirecode - All Rights Reserved * This file is part of Auth-Server-Helper which is released under MIT. * See file 'LICENSE' for full license details. * Created by Timo Hocker , January 2022 */ import { build_cookie, CookieSettings, extract_cookie } from '../../lib/cookie'; interface CreateCookie { settings: CookieSettings value: string result: string } const create_cookie_pairs: CreateCookie[] = [ { settings: { name: 'foo' }, value: 'bar', result: 'foo=bar; Secure; HttpOnly; SameSite=Strict' }, { settings: { name: 'foäöüo' }, value: 'baäöür', result: 'foäöüo=baäöür; Secure; HttpOnly; SameSite=Strict' }, { settings: { name: 'foo', secure: true, http_only: false, same_site: null }, value: 'bar', result: 'foo=bar; Secure' }, { settings: { name: 'foo', secure: false, http_only: true, same_site: null }, value: 'bar', result: 'foo=bar; HttpOnly' }, { settings: { name: 'foo', secure: false, http_only: false, same_site: 'Lax' }, value: 'bar', result: 'foo=bar; SameSite=Lax' }, { settings: { name: 'foo', secure: false, http_only: false, same_site: null, expires: 'Tomorrow' }, value: 'bar', result: 'foo=bar; Expires=Tomorrow' }, { settings: { name: 'foo', secure: false, http_only: false, same_site: null, max_age: 600 }, value: 'bar', result: 'foo=bar; Max-Age=600' }, { settings: { name: 'foo', secure: false, http_only: false, same_site: null, domain: 'example.com' }, value: 'bar', result: 'foo=bar; Domain=example.com' }, { settings: { name: 'foo', secure: false, http_only: false, same_site: null, path: '/test' }, value: 'bar', result: 'foo=bar; Path=/test' } ]; const parse_cookie_pairs = [ { header: 'foo=bar; Secure; HttpOnly; SameSite=Strict', name: 'foo', value: 'bar' }, { header: '134=567;foäöüo=baäöür;tesT=123', name: 'foäöüo', value: 'baäöür' } ]; describe ('cookie', () => { it ('should create a cookie', () => { for (const pair of create_cookie_pairs) { expect (build_cookie (pair.settings, pair.value)) .toEqual (pair.result); } }); it ('should parse a cookie', () => { for (const pair of parse_cookie_pairs) { expect (extract_cookie (pair.name, pair.header)) .toEqual (pair.value); } }); });