File size: 5,194 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
import {
workUnitAsyncStorage,
type RequestStore,
} from '../../../app-render/work-unit-async-storage.external'
import { RequestCookies, ResponseCookies } from '../cookies'
import {
ReadonlyRequestCookiesError,
RequestCookiesAdapter,
MutableRequestCookiesAdapter,
createCookiesWithMutableAccessCheck,
} from './request-cookies'
describe('RequestCookiesAdapter', () => {
it('should be able to create a new instance from a RequestCookies', () => {
const headers = new Headers({ cookie: 'foo=bar; bar=foo' })
const cookies = new RequestCookies(headers)
const sealed = RequestCookiesAdapter.seal(cookies)
expect(sealed).toBeInstanceOf(RequestCookies)
expect(sealed.get('foo')).toEqual({ name: 'foo', value: 'bar' })
expect(sealed.get('bar')).toEqual({ name: 'bar', value: 'foo' })
// These methods are not available on the sealed instance
expect(() => (sealed as any).set('foo', 'bar2')).toThrow(
ReadonlyRequestCookiesError
)
expect(() => (sealed as any).delete('foo')).toThrow(
ReadonlyRequestCookiesError
)
expect(() => (sealed as any).clear()).toThrow(ReadonlyRequestCookiesError)
// Ensure nothing was actually changed.
expect(sealed.get('foo')).toEqual({ name: 'foo', value: 'bar' })
expect(sealed.get('bar')).toEqual({ name: 'bar', value: 'foo' })
})
it('should be able to create a new instance from an empty RequestCookies', () => {
const headers = new Headers({})
const cookies = new RequestCookies(headers)
const sealed = RequestCookiesAdapter.seal(cookies)
expect(sealed).toBeInstanceOf(RequestCookies)
expect(sealed.get('foo')).toEqual(undefined)
expect(sealed.get('bar')).toEqual(undefined)
// These methods are not available on the sealed instance
expect(() => (sealed as any).set('foo', 'bar2')).toThrow(
ReadonlyRequestCookiesError
)
expect(() => (sealed as any).delete('foo')).toThrow(
ReadonlyRequestCookiesError
)
expect(() => (sealed as any).clear()).toThrow(ReadonlyRequestCookiesError)
// Ensure nothing was actually changed.
expect(sealed.get('foo')).toEqual(undefined)
expect(sealed.get('bar')).toEqual(undefined)
})
})
describe('MutableRequestCookiesAdapter', () => {
it('supports chained set calls and preserves wrapping', () => {
const headers = new Headers({})
const underlyingCookies = new RequestCookies(headers)
const onUpdateCookies = jest.fn<void, [string[]]>()
const wrappedCookies = MutableRequestCookiesAdapter.wrap(
underlyingCookies,
onUpdateCookies
)
const returned = wrappedCookies.set('foo', '1').set('bar', '2')
expect(returned).toBe(wrappedCookies)
expect(onUpdateCookies).toHaveBeenCalledWith([
expect.stringContaining('foo=1'),
])
expect(onUpdateCookies).toHaveBeenCalledWith([
expect.stringContaining('foo=1'),
expect.stringContaining('bar=2'),
])
})
it('supports chained delete calls and preserves wrapping', () => {
const headers = new Headers({})
const underlyingCookies = new RequestCookies(headers)
underlyingCookies.set('foo', '1').set('bar', '2')
const onUpdateCookies = jest.fn<void, [string[]]>()
const wrappedCookies = MutableRequestCookiesAdapter.wrap(
underlyingCookies,
onUpdateCookies
)
const returned = wrappedCookies.delete('foo').delete('bar')
expect(returned).toBe(wrappedCookies)
expect(onUpdateCookies).toHaveBeenCalledWith([
expect.stringContaining('foo=;'),
])
expect(onUpdateCookies).toHaveBeenCalledWith([
expect.stringContaining('foo=;'),
expect.stringContaining('bar=;'),
])
})
})
describe('wrapWithMutableAccessCheck', () => {
const createMockRequestStore = (phase: RequestStore['phase']) => {
const headers = new Headers({})
const underlyingCookies = new ResponseCookies(headers)
return {
type: 'request',
phase,
mutableCookies: underlyingCookies,
} as RequestStore
}
it('prevents setting cookies in the render phase', () => {
const requestStore = createMockRequestStore('action')
workUnitAsyncStorage.run(requestStore, () => {
const cookies = createCookiesWithMutableAccessCheck(requestStore)
// simulate changing phases
requestStore.phase = 'render'
const EXPECTED_ERROR =
/Cookies can only be modified in a Server Action or Route Handler\./
expect(() => {
cookies.set('foo', '1')
}).toThrow(EXPECTED_ERROR)
expect(cookies.get('foo')).toBe(undefined)
})
})
it('prevents deleting cookies in the render phase', () => {
const requestStore = createMockRequestStore('action')
workUnitAsyncStorage.run(requestStore, () => {
const cookies = createCookiesWithMutableAccessCheck(requestStore)
cookies.set('foo', '1')
// simulate changing phases
requestStore.phase = 'render'
const EXPECTED_ERROR =
/Cookies can only be modified in a Server Action or Route Handler\./
expect(() => {
cookies.delete('foo')
}).toThrow(EXPECTED_ERROR)
expect(cookies.get('foo')?.value).toEqual('1')
})
})
})
|