| 554 | return "'%s'" % str(content) |
| 555 | |
| 556 | def _assert_contains(self, response, text, status_code, msg_prefix, html): |
| 557 | # If the response supports deferred rendering and hasn't been rendered |
| 558 | # yet, then ensure that it does get rendered before proceeding further. |
| 559 | if ( |
| 560 | hasattr(response, "render") |
| 561 | and callable(response.render) |
| 562 | and not response.is_rendered |
| 563 | ): |
| 564 | response.render() |
| 565 | |
| 566 | if msg_prefix: |
| 567 | msg_prefix += ": " |
| 568 | |
| 569 | self.assertEqual( |
| 570 | response.status_code, |
| 571 | status_code, |
| 572 | msg_prefix + "Couldn't retrieve content: Response code was %d" |
| 573 | " (expected %d)" % (response.status_code, status_code), |
| 574 | ) |
| 575 | |
| 576 | if response.streaming: |
| 577 | content = b"".join(response.streaming_content) |
| 578 | # Reset the content so it can be checked again (idempotency). |
| 579 | response.streaming_content = [content] |
| 580 | else: |
| 581 | content = response.content |
| 582 | response_content = content |
| 583 | if not isinstance(text, bytes) or html: |
| 584 | text = str(text) |
| 585 | content = content.decode(response.charset) |
| 586 | if html: |
| 587 | content = assert_and_parse_html( |
| 588 | self, content, None, "Response's content is not valid HTML:" |
| 589 | ) |
| 590 | text = assert_and_parse_html( |
| 591 | self, text, None, "Second argument is not valid HTML:" |
| 592 | ) |
| 593 | real_count = content.count(text) |
| 594 | return real_count, msg_prefix, response_content |
| 595 | |
| 596 | def assertContains( |
| 597 | self, response, text, count=None, status_code=200, msg_prefix="", html=False |