@@ -913,40 +913,50 @@ def test_http(self):
913913 self .assertEqual (req .unredirected_hdrs ["Spam" ], "foo" )
914914
915915 def test_http_body_file (self ):
916- # A regular file - Content Length is calculated unless already set.
916+ # A regular file - chunked encoding is used unless Content Length is
917+ # already set.
917918
918919 h = urllib .request .AbstractHTTPHandler ()
919920 o = h .parent = MockOpener ()
920921
921922 file_obj = tempfile .NamedTemporaryFile (mode = 'w+b' , delete = False )
922923 file_path = file_obj .name
923- file_obj .write (b"Something\n Something\n Something\n " )
924924 file_obj .close ()
925+ self .addCleanup (os .unlink , file_path )
925926
926- for headers in {}, {"Content-Length" : 30 }:
927- with open (file_path , "rb" ) as f :
928- req = Request ("http://example.com/" , f , headers )
929- newreq = h .do_request_ (req )
930- self .assertEqual (int (newreq .get_header ('Content-length' )), 30 )
927+ with open (file_path , "rb" ) as f :
928+ req = Request ("http://example.com/" , f , {})
929+ newreq = h .do_request_ (req )
930+ te = newreq .get_header ('Transfer-encoding' )
931+ self .assertEqual (te , "chunked" )
932+ self .assertFalse (newreq .has_header ('Content-length' ))
931933
932- os .unlink (file_path )
934+ with open (file_path , "rb" ) as f :
935+ req = Request ("http://example.com/" , f , {"Content-Length" : 30 })
936+ newreq = h .do_request_ (req )
937+ self .assertEqual (int (newreq .get_header ('Content-length' )), 30 )
938+ self .assertFalse (newreq .has_header ("Transfer-encoding" ))
933939
934940 def test_http_body_fileobj (self ):
935- # A file object - Content Length is calculated unless already set.
941+ # A file object - chunked encoding is used
942+ # unless Content Length is already set.
936943 # (Note that there are some subtle differences to a regular
937944 # file, that is why we are testing both cases.)
938945
939946 h = urllib .request .AbstractHTTPHandler ()
940947 o = h .parent = MockOpener ()
941-
942948 file_obj = io .BytesIO ()
943- file_obj .write (b"Something\n Something\n Something\n " )
944949
945- for headers in {}, {"Content-Length" : 30 }:
946- file_obj .seek (0 )
947- req = Request ("http://example.com/" , file_obj , headers )
948- newreq = h .do_request_ (req )
949- self .assertEqual (int (newreq .get_header ('Content-length' )), 30 )
950+ req = Request ("http://example.com/" , file_obj , {})
951+ newreq = h .do_request_ (req )
952+ self .assertEqual (newreq .get_header ('Transfer-encoding' ), 'chunked' )
953+ self .assertFalse (newreq .has_header ('Content-length' ))
954+
955+ headers = {"Content-Length" : 30 }
956+ req = Request ("http://example.com/" , file_obj , headers )
957+ newreq = h .do_request_ (req )
958+ self .assertEqual (int (newreq .get_header ('Content-length' )), 30 )
959+ self .assertFalse (newreq .has_header ("Transfer-encoding" ))
950960
951961 file_obj .close ()
952962
@@ -959,9 +969,7 @@ def test_http_body_pipe(self):
959969 h = urllib .request .AbstractHTTPHandler ()
960970 o = h .parent = MockOpener ()
961971
962- cmd = [sys .executable , "-c" ,
963- r"import sys; "
964- r"sys.stdout.buffer.write(b'Something\nSomething\nSomething\n')" ]
972+ cmd = [sys .executable , "-c" , r"pass" ]
965973 for headers in {}, {"Content-Length" : 30 }:
966974 with subprocess .Popen (cmd , stdout = subprocess .PIPE ) as proc :
967975 req = Request ("http://example.com/" , proc .stdout , headers )
@@ -983,8 +991,6 @@ def test_http_body_iterable(self):
983991
984992 def iterable_body ():
985993 yield b"one"
986- yield b"two"
987- yield b"three"
988994
989995 for headers in {}, {"Content-Length" : 11 }:
990996 req = Request ("http://example.com/" , iterable_body (), headers )
@@ -996,6 +1002,14 @@ def iterable_body():
9961002 else :
9971003 self .assertEqual (int (newreq .get_header ('Content-length' )), 11 )
9981004
1005+ def test_http_body_empty_seq (self ):
1006+ # Zero-length iterable body should be treated like any other iterable
1007+ h = urllib .request .AbstractHTTPHandler ()
1008+ h .parent = MockOpener ()
1009+ req = h .do_request_ (Request ("http://example.com/" , ()))
1010+ self .assertEqual (req .get_header ("Transfer-encoding" ), "chunked" )
1011+ self .assertFalse (req .has_header ("Content-length" ))
1012+
9991013 def test_http_body_array (self ):
10001014 # array.array Iterable - Content Length is calculated
10011015
0 commit comments