Assert that a response redirected to a specific URL and that the redirect URL can be loaded. Won't work for external links since it uses the test client to do a request (use fetch_redirect_response=False to check such links without fetching them).
(
self,
response,
expected_url,
status_code=302,
target_status_code=200,
msg_prefix="",
fetch_redirect_response=True,
)
| 406 | return modify_settings(**kwargs) |
| 407 | |
| 408 | def assertRedirects( |
| 409 | self, |
| 410 | response, |
| 411 | expected_url, |
| 412 | status_code=302, |
| 413 | target_status_code=200, |
| 414 | msg_prefix="", |
| 415 | fetch_redirect_response=True, |
| 416 | ): |
| 417 | """ |
| 418 | Assert that a response redirected to a specific URL and that the |
| 419 | redirect URL can be loaded. |
| 420 | |
| 421 | Won't work for external links since it uses the test client to do a |
| 422 | request (use fetch_redirect_response=False to check such links without |
| 423 | fetching them). |
| 424 | """ |
| 425 | if msg_prefix: |
| 426 | msg_prefix += ": " |
| 427 | |
| 428 | if hasattr(response, "redirect_chain"): |
| 429 | # The request was a followed redirect |
| 430 | self.assertTrue( |
| 431 | response.redirect_chain, |
| 432 | msg_prefix |
| 433 | + ( |
| 434 | "Response didn't redirect as expected: Response code was %d " |
| 435 | "(expected %d)" |
| 436 | ) |
| 437 | % (response.status_code, status_code), |
| 438 | ) |
| 439 | |
| 440 | self.assertEqual( |
| 441 | response.redirect_chain[0][1], |
| 442 | status_code, |
| 443 | msg_prefix |
| 444 | + ( |
| 445 | "Initial response didn't redirect as expected: Response code was " |
| 446 | "%d (expected %d)" |
| 447 | ) |
| 448 | % (response.redirect_chain[0][1], status_code), |
| 449 | ) |
| 450 | |
| 451 | url, status_code = response.redirect_chain[-1] |
| 452 | |
| 453 | self.assertEqual( |
| 454 | response.status_code, |
| 455 | target_status_code, |
| 456 | msg_prefix |
| 457 | + ( |
| 458 | "Response didn't redirect as expected: Final Response code was %d " |
| 459 | "(expected %d)" |
| 460 | ) |
| 461 | % (response.status_code, target_status_code), |
| 462 | ) |
| 463 | |
| 464 | else: |
| 465 | # Not a followed redirect |
no test coverage detected