44import os
55import io
66import socket
7+ import array
78
89import urllib .request
910from urllib .request import Request , OpenerDirector
@@ -765,7 +766,7 @@ def test_http(self):
765766 o = h .parent = MockOpener ()
766767
767768 url = "http://example.com/"
768- for method , data in [("GET" , None ), ("POST" , "blah" )]:
769+ for method , data in [("GET" , None ), ("POST" , b "blah" )]:
769770 req = Request (url , data , {"Foo" : "bar" })
770771 req .timeout = None
771772 req .add_unredirected_header ("Spam" , "eggs" )
@@ -795,7 +796,7 @@ def test_http(self):
795796
796797 # check adding of standard headers
797798 o .addheaders = [("Spam" , "eggs" )]
798- for data in "" , None : # POST, GET
799+ for data in b "" , None : # POST, GET
799800 req = Request ("http://example.com/" , data )
800801 r = MockResponse (200 , "OK" , {}, "" )
801802 newreq = h .do_request_ (req )
@@ -821,14 +822,58 @@ def test_http(self):
821822 self .assertEqual (req .unredirected_hdrs ["Host" ], "baz" )
822823 self .assertEqual (req .unredirected_hdrs ["Spam" ], "foo" )
823824
825+ # Check iterable body support
826+ def iterable_body ():
827+ yield b"one"
828+ yield b"two"
829+ yield b"three"
830+
831+ for headers in {}, {"Content-Length" : 11 }:
832+ req = Request ("http://example.com/" , iterable_body (), headers )
833+ if not headers :
834+ # Having an iterable body without a Content-Length should
835+ # raise an exception
836+ self .assertRaises (ValueError , h .do_request_ , req )
837+ else :
838+ newreq = h .do_request_ (req )
839+
840+ # A file object
841+
842+ """
843+ file_obj = io.StringIO()
844+ file_obj.write("Something\n Something\n Something\n ")
845+
846+ for headers in {}, {"Content-Length": 30}:
847+ req = Request("http://example.com/", file_obj, headers)
848+ if not headers:
849+ # Having an iterable body without a Content-Length should
850+ # raise an exception
851+ self.assertRaises(ValueError, h.do_request_, req)
852+ else:
853+ newreq = h.do_request_(req)
854+ self.assertEqual(int(newreq.get_header('Content-length')),30)
855+
856+ file_obj.close()
857+
858+ # array.array Iterable - Content Length is calculated
859+
860+ iterable_array = array.array("I",[1,2,3,4])
861+
862+ for headers in {}, {"Content-Length": 16}:
863+ req = Request("http://example.com/", iterable_array, headers)
864+ newreq = h.do_request_(req)
865+ self.assertEqual(int(newreq.get_header('Content-length')),16)
866+ """
867+
868+
824869 def test_http_doubleslash (self ):
825870 # Checks the presence of any unnecessary double slash in url does not
826871 # break anything. Previously, a double slash directly after the host
827872 # could could cause incorrect parsing.
828873 h = urllib .request .AbstractHTTPHandler ()
829874 o = h .parent = MockOpener ()
830875
831- data = ""
876+ data = b ""
832877 ds_urls = [
833878 "http://example.com/foo/bar/baz.html" ,
834879 "http://example.com//foo/bar/baz.html" ,
0 commit comments