(self)
| 1226 | self.assertIs(r, newr) |
| 1227 | |
| 1228 | def test_redirect(self): |
| 1229 | from_url = "http://example.com/a.html" |
| 1230 | to_url = "http://example.com/b.html" |
| 1231 | h = urllib.request.HTTPRedirectHandler() |
| 1232 | o = h.parent = MockOpener() |
| 1233 | |
| 1234 | # ordinary redirect behaviour |
| 1235 | for code in 301, 302, 303, 307, 308: |
| 1236 | for data in None, "blah\nblah\n": |
| 1237 | method = getattr(h, "http_error_%s" % code) |
| 1238 | req = Request(from_url, data) |
| 1239 | req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT |
| 1240 | req.add_header("Nonsense", "viking=withhold") |
| 1241 | if data is not None: |
| 1242 | req.add_header("Content-Length", str(len(data))) |
| 1243 | req.add_unredirected_header("Spam", "spam") |
| 1244 | try: |
| 1245 | method(req, MockFile(), code, "Blah", |
| 1246 | MockHeaders({"location": to_url})) |
| 1247 | except urllib.error.HTTPError as err: |
| 1248 | # 307 and 308 in response to POST require user OK |
| 1249 | self.assertIn(code, (307, 308)) |
| 1250 | self.assertIsNotNone(data) |
| 1251 | err.close() |
| 1252 | self.assertEqual(o.req.get_full_url(), to_url) |
| 1253 | try: |
| 1254 | self.assertEqual(o.req.get_method(), "GET") |
| 1255 | except AttributeError: |
| 1256 | self.assertFalse(o.req.data) |
| 1257 | |
| 1258 | # now it's a GET, there should not be headers regarding content |
| 1259 | # (possibly dragged from before being a POST) |
| 1260 | headers = [x.lower() for x in o.req.headers] |
| 1261 | self.assertNotIn("content-length", headers) |
| 1262 | self.assertNotIn("content-type", headers) |
| 1263 | |
| 1264 | self.assertEqual(o.req.headers["Nonsense"], |
| 1265 | "viking=withhold") |
| 1266 | self.assertNotIn("Spam", o.req.headers) |
| 1267 | self.assertNotIn("Spam", o.req.unredirected_hdrs) |
| 1268 | |
| 1269 | # loop detection |
| 1270 | req = Request(from_url) |
| 1271 | req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT |
| 1272 | |
| 1273 | def redirect(h, req, url=to_url): |
| 1274 | h.http_error_302(req, MockFile(), 302, "Blah", |
| 1275 | MockHeaders({"location": url})) |
| 1276 | # Note that the *original* request shares the same record of |
| 1277 | # redirections with the sub-requests caused by the redirections. |
| 1278 | |
| 1279 | # detect infinite loop redirect of a URL to itself |
| 1280 | req = Request(from_url, origin_req_host="example.com") |
| 1281 | count = 0 |
| 1282 | req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT |
| 1283 | try: |
| 1284 | while 1: |
| 1285 | redirect(h, req, "http://example.com/") |
nothing calls this directly
no test coverage detected