MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_http

Method test_http

Lib/test/test_urllib2.py:900–964  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

898 self.assertEqual(req.type == "ftp", ftp)
899
900 def test_http(self):
901
902 h = urllib.request.AbstractHTTPHandler()
903 o = h.parent = MockOpener()
904
905 url = "http://example.com/"
906 for method, data in [("GET", None), ("POST", b"blah")]:
907 req = Request(url, data, {"Foo": "bar"})
908 req.timeout = None
909 req.add_unredirected_header("Spam", "eggs")
910 http = MockHTTPClass()
911 r = h.do_open(http, req)
912
913 # result attributes
914 r.read; r.readline # wrapped MockFile methods
915 r.info; r.geturl # addinfourl methods
916 r.code, r.msg == 200, "OK" # added from MockHTTPClass.getreply()
917 hdrs = r.info()
918 hdrs.get; hdrs.__contains__ # r.info() gives dict from .getreply()
919 self.assertEqual(r.geturl(), url)
920
921 self.assertEqual(http.host, "example.com")
922 self.assertEqual(http.level, 0)
923 self.assertEqual(http.method, method)
924 self.assertEqual(http.selector, "/")
925 self.assertEqual(http.req_headers,
926 [("Connection", "close"),
927 ("Foo", "bar"), ("Spam", "eggs")])
928 self.assertEqual(http.data, data)
929
930 # check OSError converted to URLError
931 http.raise_on_endheaders = True
932 self.assertRaises(urllib.error.URLError, h.do_open, http, req)
933
934 # Check for TypeError on POST data which is str.
935 req = Request("http://example.com/","badpost")
936 self.assertRaises(TypeError, h.do_request_, req)
937
938 # check adding of standard headers
939 o.addheaders = [("Spam", "eggs")]
940 for data in b"", None: # POST, GET
941 req = Request("http://example.com/", data)
942 r = MockResponse(200, "OK", {}, "")
943 newreq = h.do_request_(req)
944 if data is None: # GET
945 self.assertNotIn("Content-length", req.unredirected_hdrs)
946 self.assertNotIn("Content-type", req.unredirected_hdrs)
947 else: # POST
948 self.assertEqual(req.unredirected_hdrs["Content-length"], "0")
949 self.assertEqual(req.unredirected_hdrs["Content-type"],
950 "application/x-www-form-urlencoded")
951 # XXX the details of Host could be better tested
952 self.assertEqual(req.unredirected_hdrs["Host"], "example.com")
953 self.assertEqual(req.unredirected_hdrs["Spam"], "eggs")
954
955 # don't clobber existing headers
956 req.add_unredirected_header("Content-length", "foo")
957 req.add_unredirected_header("Content-type", "bar")

Callers

nothing calls this directly

Calls 12

do_openMethod · 0.95
infoMethod · 0.95
geturlMethod · 0.95
do_request_Method · 0.95
RequestClass · 0.90
MockOpenerClass · 0.85
MockHTTPClassClass · 0.85
MockResponseClass · 0.85
assertNotInMethod · 0.80
assertEqualMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected