|
1 | 1 | """Tests for the usage of the mock Flask application.""" |
2 | 2 |
|
| 3 | +import email.utils |
3 | 4 | import io |
4 | 5 | import json |
| 6 | +import time |
5 | 7 | import uuid |
6 | 8 | from collections.abc import Iterator |
7 | 9 | from http import HTTPStatus |
@@ -605,3 +607,63 @@ def test_random( |
605 | 607 | assert lowest_rating >= minimum_rating |
606 | 608 | assert highest_rating <= maximum_rating |
607 | 609 | assert lowest_rating != highest_rating |
| 610 | + |
| 611 | + |
| 612 | +class TestResponseDelay: |
| 613 | + """Tests for the response delay feature. |
| 614 | +
|
| 615 | + These tests run through the ``responses`` library, which intercepts |
| 616 | + requests in-process. Because of this, the client ``timeout`` parameter |
| 617 | + is not enforced — the delay blocks but never raises |
| 618 | + ``requests.exceptions.Timeout``. When running the Flask app as a real |
| 619 | + server (e.g. in Docker), the delay causes a genuinely slow HTTP |
| 620 | + response and the ``requests`` client will raise ``Timeout`` on its own. |
| 621 | + """ |
| 622 | + |
| 623 | + DELAY_SECONDS = 0.5 |
| 624 | + |
| 625 | + @staticmethod |
| 626 | + def _make_request() -> None: |
| 627 | + """Make a request to the VWS API.""" |
| 628 | + requests.get( |
| 629 | + url="https://vws.vuforia.com/summary", |
| 630 | + headers={ |
| 631 | + "Date": email.utils.formatdate( |
| 632 | + timeval=None, |
| 633 | + localtime=False, |
| 634 | + usegmt=True, |
| 635 | + ), |
| 636 | + "Authorization": "bad_auth_token", |
| 637 | + }, |
| 638 | + data=b"", |
| 639 | + timeout=30, |
| 640 | + ) |
| 641 | + |
| 642 | + def test_default_no_delay(self) -> None: |
| 643 | + """By default, there is no response delay.""" |
| 644 | + database = VuforiaDatabase() |
| 645 | + databases_url = _EXAMPLE_URL_FOR_TARGET_MANAGER + "/databases" |
| 646 | + requests.post(url=databases_url, json=database.to_dict(), timeout=30) |
| 647 | + |
| 648 | + start = time.monotonic() |
| 649 | + self._make_request() |
| 650 | + elapsed = time.monotonic() - start |
| 651 | + assert elapsed < self.DELAY_SECONDS |
| 652 | + |
| 653 | + def test_delay_is_applied( |
| 654 | + self, |
| 655 | + monkeypatch: pytest.MonkeyPatch, |
| 656 | + ) -> None: |
| 657 | + """When response_delay_seconds is set, the response is delayed.""" |
| 658 | + monkeypatch.setenv( |
| 659 | + name="RESPONSE_DELAY_SECONDS", |
| 660 | + value=f"{self.DELAY_SECONDS}", |
| 661 | + ) |
| 662 | + database = VuforiaDatabase() |
| 663 | + databases_url = _EXAMPLE_URL_FOR_TARGET_MANAGER + "/databases" |
| 664 | + requests.post(url=databases_url, json=database.to_dict(), timeout=30) |
| 665 | + |
| 666 | + start = time.monotonic() |
| 667 | + self._make_request() |
| 668 | + elapsed = time.monotonic() - start |
| 669 | + assert elapsed >= self.DELAY_SECONDS |
0 commit comments