@@ -197,73 +197,78 @@ describe("cookies", () => {
197
197
} ) ;
198
198
} ) ;
199
199
200
- describe ( "encode " , ( ) => {
201
- it ( "encodes the cookie using default encoding when no encode function is provided" , async ( ) => {
202
- let rawCookieValue = "cookie " ;
200
+ describe ( "custom encoding/decoding " , ( ) => {
201
+ it ( "uses default base64 encoding when no functions are provided" , async ( ) => {
202
+ let rawCookieValue = "hello world " ;
203
203
let cookie = createCookie ( "my-cookie" ) ;
204
204
let setCookie = await cookie . serialize ( rawCookieValue ) ;
205
- expect ( setCookie ) . not . toContain ( "my-cookie=cookie" ) ;
205
+ expect ( setCookie ) . toContain ( "my-cookie=ImhlbGxvIHdvcmxkIg%3D%3D;" ) ;
206
+ let parsed = await cookie . parse ( getCookieFromSetCookie ( setCookie ) ) ;
207
+ expect ( parsed ) . toBe ( rawCookieValue ) ;
206
208
} ) ;
207
209
208
- it ( "encodes the cookie using the provided encode function at initialization" , async ( ) => {
209
- let rawCookieValue = "cookie" ;
210
- let encodeFn = ( str : string ) => {
211
- // Check that the value is not encoded before calling encodeFn
212
- expect ( str ) . toBe ( rawCookieValue ) ;
213
- return str ;
214
- } ;
215
- let cookie = createCookie ( "my-cookie" , { encode : encodeFn } ) ;
210
+ it ( "uses custom implementations when provided at initialization" , async ( ) => {
211
+ let rawCookieValue = "hello world" ;
212
+ let cookie = createCookie ( "my-cookie" , {
213
+ encode ( str : string ) {
214
+ expect ( str ) . toBe ( rawCookieValue ) ; // not encoded yet
215
+ return encodeURIComponent ( str . toUpperCase ( ) ) ;
216
+ } ,
217
+ decode ( str : string ) {
218
+ expect ( str ) . toBe ( "HELLO%20WORLD" ) ;
219
+ return decodeURIComponent ( str . toLowerCase ( ) ) ;
220
+ } ,
221
+ } ) ;
216
222
let setCookie = await cookie . serialize ( rawCookieValue ) ;
217
- expect ( setCookie ) . toContain ( "my-cookie=cookie" ) ;
223
+ expect ( setCookie ) . toContain ( "my-cookie=HELLO%20WORLD;" ) ;
224
+ let parsed = await cookie . parse ( getCookieFromSetCookie ( setCookie ) ) ;
225
+ expect ( parsed ) . toBe ( rawCookieValue ) ;
218
226
} ) ;
219
227
220
- it ( "encodes the cookie using the provided encode function when specified during serialization" , async ( ) => {
221
- let rawCookieValue = "cookie" ;
222
- let encodeFn = ( str : string ) => {
223
- // Check that the value is not encoded before calling encodeFn
224
- expect ( str ) . toBe ( rawCookieValue ) ;
225
- return str ;
226
- } ;
228
+ it ( "uses custom implementations when provided at usage time" , async ( ) => {
229
+ let rawCookieValue = "hello world" ;
227
230
let cookie = createCookie ( "my-cookie" ) ;
228
231
let setCookie = await cookie . serialize ( rawCookieValue , {
229
- encode : encodeFn ,
232
+ encode ( str : string ) {
233
+ expect ( str ) . toBe ( rawCookieValue ) ; // not encoded yet
234
+ return encodeURIComponent ( str . toUpperCase ( ) ) ;
235
+ } ,
230
236
} ) ;
231
- expect ( setCookie ) . toContain ( "my-cookie=cookie" ) ;
232
- } ) ;
233
- } ) ;
234
-
235
- describe ( "decode" , ( ) => {
236
- it ( "decodes cookie using default decode function" , async ( ) => {
237
- let rawCookieValue = "cookie" ;
238
- let cookie = createCookie ( "my-cookie" ) ;
239
- let setCookie = await cookie . serialize ( rawCookieValue ) ;
240
- let value = await cookie . parse ( getCookieFromSetCookie ( setCookie ) ) ;
241
- expect ( value ) . toBe ( rawCookieValue ) ;
237
+ expect ( setCookie ) . toContain ( "my-cookie=HELLO%20WORLD;" ) ;
238
+ let parsed = await cookie . parse ( getCookieFromSetCookie ( setCookie ) , {
239
+ decode ( str : string ) {
240
+ expect ( str ) . toBe ( "HELLO%20WORLD" ) ;
241
+ return decodeURIComponent ( str . toLowerCase ( ) ) ;
242
+ } ,
243
+ } ) ;
244
+ expect ( parsed ) . toBe ( rawCookieValue ) ;
242
245
} ) ;
243
246
244
- it ( "decodes cookie using provided encode and decode functions during initialization" , async ( ) => {
245
- let rawCookieValue = "cookie" ;
246
- let encodeFn = ( str : string ) => str ;
247
- let decodeFn = ( str : string ) => str ;
247
+ it ( "uses custom implementations when using signed cookies" , async ( ) => {
248
+ let rawCookieValue = "hello world" ;
248
249
let cookie = createCookie ( "my-cookie" , {
249
- encode : encodeFn ,
250
- decode : decodeFn ,
250
+ secrets : [ "s3cr3t" ] ,
251
+ encode ( str : string ) {
252
+ expect ( str ) . toBe ( rawCookieValue ) ; // not encoded yet
253
+ return encodeURIComponent ( str . toUpperCase ( ) ) ;
254
+ } ,
255
+ decode ( str : string ) {
256
+ expect ( str ) . toBe ( "HELLO%20WORLD" ) ;
257
+ return decodeURIComponent ( str . toLowerCase ( ) ) ;
258
+ } ,
251
259
} ) ;
252
260
let setCookie = await cookie . serialize ( rawCookieValue ) ;
253
- let value = await cookie . parse ( getCookieFromSetCookie ( setCookie ) ) ;
254
- expect ( value ) . toBe ( rawCookieValue ) ;
255
- } ) ;
261
+ expect ( setCookie ) . toContain (
262
+ "my-cookie=HELLO%20WORLD.4bKWgOIqYxcP3KMCHWBmoKEQth3NPQ9yrTRurGMgS40;" ,
263
+ ) ;
264
+ let parsed = await cookie . parse ( getCookieFromSetCookie ( setCookie ) ) ;
265
+ expect ( parsed ) . toBe ( rawCookieValue ) ;
256
266
257
- it ( "decodes cookie using provided decode function during parsing" , async ( ) => {
258
- let rawCookieValue = "cookie" ;
259
- let encodeFn = ( str : string ) => str ;
260
- let decodeFn = ( str : string ) => str ;
261
- let cookie = createCookie ( "my-cookie" , { encode : encodeFn } ) ;
262
- let setCookie = await cookie . serialize ( rawCookieValue ) ;
263
- let value = await cookie . parse ( getCookieFromSetCookie ( setCookie ) , {
264
- decode : decodeFn ,
265
- } ) ;
266
- expect ( value ) . toBe ( rawCookieValue ) ;
267
+ // Fails if the cookie value is tampered with
268
+ parsed = await cookie . parse (
269
+ "my-cookie=HELLO%20MARS.4bKWgOIqYxcP3KMCHWBmoKEQth3NPQ9yrTRurGMgS40" ,
270
+ ) ;
271
+ expect ( parsed ) . toBe ( null ) ;
267
272
} ) ;
268
273
} ) ;
269
274
} ) ;
0 commit comments