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 HTTPServerRequest , HTTPHeaders
22
20
from tornado .queues import Queue
23
21
from tornado .web import HTTPError
24
22
from traitlets import Int , Unicode
@@ -374,14 +372,13 @@ def test_gateway_request_timeout_pad_option(
374
372
375
373
GatewayClient .clear_instance ()
376
374
377
-
378
375
@pytest .mark .parametrize (
379
- "accept_cookies,expire_arg,expire_param,existing_cookies,cookie_exists " ,
376
+ "accept_cookies,expire_arg,expire_param,existing_cookies" ,
380
377
[
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 ),
378
+ (False , None , 0 , "EXISTING=1" ),
379
+ (True , None , 0 , "EXISTING=1" ),
380
+ (True , "expires " , 180 , None ),
381
+ (True , "Max-Age" , - 360 , "EXISTING=1" ),
385
382
],
386
383
)
387
384
def test_gateway_request_with_expiring_cookies (
@@ -390,39 +387,49 @@ def test_gateway_request_with_expiring_cookies(
390
387
expire_arg ,
391
388
expire_param ,
392
389
existing_cookies ,
393
- cookie_exists ,
394
390
):
395
391
argv = [f"--GatewayClient.accept_cookies={ accept_cookies } " ]
396
392
397
393
GatewayClient .clear_instance ()
398
394
_ = jp_configurable_serverapp (argv = argv )
399
395
400
- headers = {}
401
- # Use comma-separated Set-Cookie headers to demonstrate the issue
402
- headers ["Set-Cookie" ] = "SERVERID=016723f5|12345678; Max-Age=172800; Path=/; HttpOnly," \
403
- "username-my-server=2|1:0|10:1756250589|35:username-my-server|144:123abc789|87654321; " \
404
- "Max-Age=172800; Path=/; HttpOnly"
405
- if expire_arg == "Expires" :
406
- expire_param = format_datetime (
407
- datetime .now (tz = timezone .utc ) + timedelta (seconds = expire_param )
408
- )
409
- if expire_arg :
410
- # Replace the first SERVERID cookie's Max-Age with the new expire parameter
411
- headers ["Cookie" ] = headers ["Set-Cookie" ].replace ("Max-Age=172800" , f"{ expire_arg } ={ expire_param } " )
396
+ test_expiration = bool (expire_param < 0 )
397
+ # Create mock headers with Set-Cookie values
398
+ headers = HTTPHeaders ()
399
+ cookie_value = "SERVERID=1234567; Path=/; HttpOnly"
400
+ if expire_arg == "expires" :
401
+ # Convert expire_param to a string in the format of "Expires: <date>" (RFC 7231)
402
+ expire_param = (datetime .now (tz = timezone .utc ) + timedelta (seconds = expire_param ))\
403
+ .strftime ("%a, %d %b %Y %H:%M:%S GMT" )
404
+ cookie_value = f"SERVERID=1234567; Path=/; expires={ expire_param } ; HttpOnly"
405
+ elif expire_arg == "Max-Age" :
406
+ cookie_value = f"SERVERID=1234567; Path=/; Max-Age={ expire_param } ; HttpOnly"
407
+
408
+ # Add a second cookie to test comma-separated cookies
409
+ headers .add ("Set-Cookie" , cookie_value )
410
+ headers .add ("Set-Cookie" , "ADDITIONAL_COOKIE=8901234; Path=/; HttpOnly" )
411
+
412
+ headers_list = headers .get_list ("Set-Cookie" )
413
+ print (headers_list )
412
414
413
415
GatewayClient .instance ().update_cookies (headers )
414
416
415
417
args = {}
416
418
if existing_cookies :
417
419
args ["headers" ] = {"Cookie" : existing_cookies }
420
+
418
421
connection_args = GatewayClient .instance ().load_connection_args (** args )
419
422
420
- if not cookie_exists :
421
- assert "SERVERID" not in (connection_args ["headers" ].get ("Cookie" ) or "" )
423
+ if not accept_cookies or test_expiration :
424
+ # The first condition ensure the response cookie is not accepted,
425
+ # the second condition ensures that the cookie is not accepted if it is expired.
426
+ assert "SERVERID" not in connection_args ["headers" ].get ("Cookie" )
422
427
else :
423
- assert "SERVERID" in connection_args ["headers" ].get ("Cookie" )
428
+ # The cookie is accepted if it is not expired and accept_cookies is True.
429
+ assert "SERVERID" in connection_args ["headers" ].get ("Cookie" , "" )
430
+
424
431
if existing_cookies :
425
- assert "EXISTING" in connection_args ["headers" ].get ("Cookie" )
432
+ assert "EXISTING" in connection_args ["headers" ].get ("Cookie" , "" )
426
433
427
434
GatewayClient .clear_instance ()
428
435
0 commit comments