MCPcopy Index your code
hub / github.com/python/cpython / test_http

Method test_http

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

Source from the content-addressed store, hash-verified

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