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