66import os
77import uuid
88from datetime import datetime , timedelta , timezone
9- from email .utils import format_datetime
10- from http .cookies import SimpleCookie
119from io import BytesIO
1210from queue import Empty
1311from typing import Any , Union
1816from jupyter_core .utils import ensure_async
1917from tornado .concurrent import Future
2018from tornado .httpclient import HTTPRequest , HTTPResponse
21- from tornado .httputil import HTTPServerRequest
19+ from tornado .httputil import HTTPServerRequest , HTTPHeaders
2220from tornado .queues import Queue
2321from tornado .web import HTTPError
2422from traitlets import Int , Unicode
@@ -374,14 +372,13 @@ def test_gateway_request_timeout_pad_option(
374372
375373 GatewayClient .clear_instance ()
376374
377-
378375@pytest .mark .parametrize (
379- "accept_cookies,expire_arg,expire_param,existing_cookies,cookie_exists " ,
376+ "accept_cookies,expire_arg,expire_param,existing_cookies" ,
380377 [
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" ),
385382 ],
386383)
387384def test_gateway_request_with_expiring_cookies (
@@ -390,39 +387,49 @@ def test_gateway_request_with_expiring_cookies(
390387 expire_arg ,
391388 expire_param ,
392389 existing_cookies ,
393- cookie_exists ,
394390):
395391 argv = [f"--GatewayClient.accept_cookies={ accept_cookies } " ]
396392
397393 GatewayClient .clear_instance ()
398394 _ = jp_configurable_serverapp (argv = argv )
399395
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 )
412414
413415 GatewayClient .instance ().update_cookies (headers )
414416
415417 args = {}
416418 if existing_cookies :
417419 args ["headers" ] = {"Cookie" : existing_cookies }
420+
418421 connection_args = GatewayClient .instance ().load_connection_args (** args )
419422
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" )
422427 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+
424431 if existing_cookies :
425- assert "EXISTING" in connection_args ["headers" ].get ("Cookie" )
432+ assert "EXISTING" in connection_args ["headers" ].get ("Cookie" , "" )
426433
427434 GatewayClient .clear_instance ()
428435
0 commit comments