@@ -132,6 +132,11 @@ def do_GET(self):
132132 self ._method ('GET' )
133133 elif self .path .startswith ('/headers' ):
134134 self ._headers ()
135+ elif self .path .startswith ('/308-to-headers' ):
136+ self .send_response (308 )
137+ self .send_header ('Location' , '/headers' )
138+ self .send_header ('Content-Length' , '0' )
139+ self .end_headers ()
135140 elif self .path == '/trailing_garbage' :
136141 payload = b'<html><video src="/vid.mp4" /></html>'
137142 self .send_response (200 )
@@ -270,6 +275,7 @@ def do_req(redirect_status, method):
270275 self .assertEqual (do_req (303 , 'PUT' ), ('' , 'GET' ))
271276
272277 # 301 and 302 turn POST only into a GET
278+ # XXX: we should also test if the Content-Type and Content-Length headers are removed
273279 self .assertEqual (do_req (301 , 'POST' ), ('' , 'GET' ))
274280 self .assertEqual (do_req (301 , 'HEAD' ), ('' , 'HEAD' ))
275281 self .assertEqual (do_req (302 , 'POST' ), ('' , 'GET' ))
@@ -313,6 +319,31 @@ def test_cookiejar(self):
313319 data = ydl .urlopen (sanitized_Request (f'http://127.0.0.1:{ self .http_port } /headers' )).read ()
314320 self .assertIn (b'Cookie: test=ytdlp' , data )
315321
322+ def test_passed_cookie_header (self ):
323+ # We should accept a Cookie header being passed as in normal headers and handle it appropriately.
324+ with FakeYDL () as ydl :
325+ # Specified Cookie header should be used
326+ res = ydl .urlopen (
327+ sanitized_Request (f'http://127.0.0.1:{ self .http_port } /headers' ,
328+ headers = {'Cookie' : 'test=test' })).read ().decode ('utf-8' )
329+ self .assertIn ('Cookie: test=test' , res )
330+
331+ # Specified Cookie header should be removed on any redirect
332+ res = ydl .urlopen (
333+ sanitized_Request (f'http://127.0.0.1:{ self .http_port } /308-to-headers' , headers = {'Cookie' : 'test=test' })).read ().decode ('utf-8' )
334+ self .assertNotIn ('Cookie: test=test' , res )
335+
336+ # Specified Cookie header should override global cookiejar for that request
337+ ydl .cookiejar .set_cookie (http .cookiejar .Cookie (
338+ version = 0 , name = 'test' , value = 'ytdlp' , port = None , port_specified = False ,
339+ domain = '127.0.0.1' , domain_specified = True , domain_initial_dot = False , path = '/' ,
340+ path_specified = True , secure = False , expires = None , discard = False , comment = None ,
341+ comment_url = None , rest = {}))
342+
343+ data = ydl .urlopen (sanitized_Request (f'http://127.0.0.1:{ self .http_port } /headers' , headers = {'Cookie' : 'test=test' })).read ()
344+ self .assertNotIn (b'Cookie: test=ytdlp' , data )
345+ self .assertIn (b'Cookie: test=test' , data )
346+
316347 def test_no_compression_compat_header (self ):
317348 with FakeYDL () as ydl :
318349 data = ydl .urlopen (
0 commit comments