|
1 | 1 | import { describe, expect, it } from 'vitest'; |
2 | | -import { formatCookieHeader, sortCookies } from '../cookies'; |
| 2 | +import { formatCookieHeader, mergeCookies, sortCookies } from '../cookies'; |
3 | 3 |
|
4 | 4 | describe('formatCookieHeader', () => { |
5 | 5 | it('joins name=value pairs with "; "', () => { |
@@ -68,3 +68,42 @@ describe('sortCookies', () => { |
68 | 68 | expect(input[0]?.name).toBe('root'); |
69 | 69 | }); |
70 | 70 | }); |
| 71 | + |
| 72 | +describe('mergeCookies', () => { |
| 73 | + it('adds names only the page knows about', () => { |
| 74 | + // The regression this guards: a privileged query that comes back with one |
| 75 | + // entry must not shrink the header below what the page itself would send. |
| 76 | + const merged = mergeCookies( |
| 77 | + [{ name: 'PASSPORT', value: 'FAKE1', path: '/' }], |
| 78 | + [ |
| 79 | + { name: 'PASSPORT', value: 'STALE' }, |
| 80 | + { name: 'sid', value: 'FAKE2' } |
| 81 | + ] |
| 82 | + ); |
| 83 | + expect(merged).toEqual([ |
| 84 | + { name: 'PASSPORT', value: 'FAKE1', path: '/' }, |
| 85 | + { name: 'sid', value: 'FAKE2' } |
| 86 | + ]); |
| 87 | + }); |
| 88 | + |
| 89 | + it('keeps the privileged value on a name clash', () => { |
| 90 | + // chrome.cookies/GM_cookie carry path and HttpOnly entries; the page copy |
| 91 | + // is the weaker record. |
| 92 | + const merged = mergeCookies( |
| 93 | + [{ name: 'sid', value: 'PRIVILEGED' }], |
| 94 | + [{ name: 'sid', value: 'FROM_PAGE' }] |
| 95 | + ); |
| 96 | + expect(merged).toEqual([{ name: 'sid', value: 'PRIVILEGED' }]); |
| 97 | + }); |
| 98 | + |
| 99 | + it('falls back to the page list when the privileged one is empty', () => { |
| 100 | + expect(mergeCookies([], [{ name: 'sid', value: 'FAKE' }])).toEqual([ |
| 101 | + { name: 'sid', value: 'FAKE' } |
| 102 | + ]); |
| 103 | + }); |
| 104 | + |
| 105 | + it('is a no-op without page cookies', () => { |
| 106 | + const privileged = [{ name: 'sid', value: 'FAKE', path: '/' }]; |
| 107 | + expect(mergeCookies(privileged, [])).toEqual(privileged); |
| 108 | + }); |
| 109 | +}); |
0 commit comments