6
6
import os
7
7
import uuid
8
8
from datetime import datetime , timedelta , timezone
9
- from email .utils import format_datetime
10
- from http .cookies import SimpleCookie
11
9
from io import BytesIO
12
10
from queue import Empty
13
11
from typing import Any , Union
18
16
from jupyter_core .utils import ensure_async
19
17
from tornado .concurrent import Future
20
18
from tornado .httpclient import HTTPRequest , HTTPResponse
21
- from tornado .httputil import HTTPServerRequest
19
+ from tornado .httputil import HTTPHeaders , HTTPServerRequest
22
20
from tornado .queues import Queue
23
21
from tornado .web import HTTPError
24
22
from traitlets import Int , Unicode
@@ -376,12 +374,12 @@ def test_gateway_request_timeout_pad_option(
376
374
377
375
378
376
@pytest .mark .parametrize (
379
- "accept_cookies,expire_arg,expire_param,existing_cookies,cookie_exists " ,
377
+ "accept_cookies,expire_arg,expire_param,existing_cookies" ,
380
378
[
381
- (False , None , None , "EXISTING=1" , False ),
382
- (True , None , None , "EXISTING=1" , True ),
383
- (True , "Expires " , 180 , None , True ),
384
- (True , "Max-Age" , " -360" , "EXISTING=1" , False ),
379
+ (False , None , 0 , "EXISTING=1" ),
380
+ (True , None , 0 , "EXISTING=1" ),
381
+ (True , "expires " , 180 , None ),
382
+ (True , "Max-Age" , - 360 , "EXISTING=1" ),
385
383
],
386
384
)
387
385
def test_gateway_request_with_expiring_cookies (
@@ -390,35 +388,50 @@ def test_gateway_request_with_expiring_cookies(
390
388
expire_arg ,
391
389
expire_param ,
392
390
existing_cookies ,
393
- cookie_exists ,
394
391
):
395
392
argv = [f"--GatewayClient.accept_cookies={ accept_cookies } " ]
396
393
397
394
GatewayClient .clear_instance ()
398
395
_ = jp_configurable_serverapp (argv = argv )
399
396
400
- cookie : SimpleCookie = SimpleCookie ()
401
- cookie .load ("SERVERID=1234567; Path=/" )
402
- if expire_arg == "Expires" :
403
- expire_param = format_datetime (
404
- datetime .now (tz = timezone .utc ) + timedelta (seconds = expire_param )
397
+ test_expiration = bool (expire_param < 0 )
398
+ # Create mock headers with Set-Cookie values
399
+ headers = HTTPHeaders ()
400
+ cookie_value = "SERVERID=1234567; Path=/; HttpOnly"
401
+ if expire_arg == "expires" :
402
+ # Convert expire_param to a string in the format of "Expires: <date>" (RFC 7231)
403
+ expire_param = (datetime .now (tz = timezone .utc ) + timedelta (seconds = expire_param )).strftime (
404
+ "%a, %d %b %Y %H:%M:%S GMT"
405
405
)
406
- if expire_arg :
407
- cookie ["SERVERID" ][expire_arg ] = expire_param
406
+ cookie_value = f"SERVERID=1234567; Path=/; expires={ expire_param } ; HttpOnly"
407
+ elif expire_arg == "Max-Age" :
408
+ cookie_value = f"SERVERID=1234567; Path=/; Max-Age={ expire_param } ; HttpOnly"
408
409
409
- GatewayClient .instance ().update_cookies (cookie )
410
+ # Add a second cookie to test comma-separated cookies
411
+ headers .add ("Set-Cookie" , cookie_value )
412
+ headers .add ("Set-Cookie" , "ADDITIONAL_COOKIE=8901234; Path=/; HttpOnly" )
413
+
414
+ headers_list = headers .get_list ("Set-Cookie" )
415
+ print (headers_list )
416
+
417
+ GatewayClient .instance ().update_cookies (headers )
410
418
411
419
args = {}
412
420
if existing_cookies :
413
421
args ["headers" ] = {"Cookie" : existing_cookies }
422
+
414
423
connection_args = GatewayClient .instance ().load_connection_args (** args )
415
424
416
- if not cookie_exists :
417
- assert "SERVERID" not in (connection_args ["headers" ].get ("Cookie" ) or "" )
425
+ if not accept_cookies or test_expiration :
426
+ # The first condition ensure the response cookie is not accepted,
427
+ # the second condition ensures that the cookie is not accepted if it is expired.
428
+ assert "SERVERID" not in connection_args ["headers" ].get ("Cookie" )
418
429
else :
419
- assert "SERVERID" in connection_args ["headers" ].get ("Cookie" )
430
+ # The cookie is accepted if it is not expired and accept_cookies is True.
431
+ assert "SERVERID" in connection_args ["headers" ].get ("Cookie" , "" )
432
+
420
433
if existing_cookies :
421
- assert "EXISTING" in connection_args ["headers" ].get ("Cookie" )
434
+ assert "EXISTING" in connection_args ["headers" ].get ("Cookie" , "" )
422
435
423
436
GatewayClient .clear_instance ()
424
437
0 commit comments