|
| 1 | +from itertools import cycle |
| 2 | + |
| 3 | +import pytest |
| 4 | +import requests |
| 5 | +from werkzeug import Request |
| 6 | +from werkzeug import Response |
| 7 | + |
| 8 | +from pytest_httpserver import HTTPServer |
| 9 | + |
| 10 | + |
| 11 | +def test_multiple_responses(httpserver: HTTPServer): |
| 12 | + httpserver.expect_request("/retry").respond_multiple( |
| 13 | + [ |
| 14 | + Response("unavailable", status=503), |
| 15 | + Response("OK", headers={"X-Result": "success"}), |
| 16 | + ] |
| 17 | + ) |
| 18 | + first = requests.get(httpserver.url_for("/retry")) |
| 19 | + second = requests.get(httpserver.url_for("/retry")) |
| 20 | + assert (first.status_code, first.text) == (503, "unavailable") |
| 21 | + assert (second.status_code, second.text) == (200, "OK") |
| 22 | + assert second.headers["X-Result"] == "success" |
| 23 | + httpserver.check() |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.parametrize("responses", [[], [Response("OK")]]) |
| 27 | +def test_exhausted_responses(httpserver: HTTPServer, responses): |
| 28 | + httpserver.expect_request("/").respond_multiple(responses) |
| 29 | + for _ in responses: |
| 30 | + assert requests.get(httpserver.url_for("/")).status_code == 200 |
| 31 | + assert requests.get(httpserver.url_for("/")).status_code == 500 |
| 32 | + with pytest.raises(AssertionError, match="No responses left for matching request: GET /"): |
| 33 | + httpserver.check() |
| 34 | + httpserver.check_handler_errors() |
| 35 | + |
| 36 | + |
| 37 | +def test_lazy_generator_and_matching(httpserver: HTTPServer): |
| 38 | + consumed = [] |
| 39 | + |
| 40 | + def responses(): |
| 41 | + for text in ["first", "second"]: |
| 42 | + consumed.append(text) |
| 43 | + yield Response(text) |
| 44 | + |
| 45 | + httpserver.expect_request("/sequence").respond_multiple(responses()) |
| 46 | + httpserver.expect_request("/other").respond_with_data("other") |
| 47 | + assert consumed == [] |
| 48 | + requests.get(httpserver.url_for("/other")) |
| 49 | + assert consumed == [] |
| 50 | + assert requests.get(httpserver.url_for("/sequence")).text == "first" |
| 51 | + assert consumed == ["first"] |
| 52 | + assert requests.get(httpserver.url_for("/sequence")).text == "second" |
| 53 | + httpserver.check() |
| 54 | + |
| 55 | + |
| 56 | +def test_infinite_responses_and_post_hook(httpserver: HTTPServer): |
| 57 | + def hook(_request: Request, response: Response) -> Response: |
| 58 | + response.headers["X-Hook"] = "called" |
| 59 | + return response |
| 60 | + |
| 61 | + httpserver.expect_request("/").with_post_hook(hook).respond_multiple(cycle([Response("a"), Response("b")])) |
| 62 | + results = [requests.get(httpserver.url_for("/")) for _ in range(5)] |
| 63 | + assert [response.text for response in results] == ["a", "b", "a", "b", "a"] |
| 64 | + assert all(response.headers["X-Hook"] == "called" for response in results) |
| 65 | + httpserver.check() |
| 66 | + |
| 67 | + |
| 68 | +def test_invalid_response(httpserver: HTTPServer): |
| 69 | + httpserver.expect_request("/").respond_multiple(iter([None])) # type: ignore[arg-type] |
| 70 | + assert requests.get(httpserver.url_for("/")).status_code == 500 |
| 71 | + with pytest.raises(TypeError, match="respond_multiple expects Werkzeug Response objects"): |
| 72 | + httpserver.check_handler_errors() |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize("method", ["expect_oneshot_request", "expect_ordered_request"]) |
| 76 | +def test_single_use_handlers(httpserver: HTTPServer, method): |
| 77 | + getattr(httpserver, method)("/").respond_multiple([Response("first"), Response("second")]) |
| 78 | + httpserver.expect_request("/").respond_with_data("fallback") |
| 79 | + assert requests.get(httpserver.url_for("/")).text == "first" |
| 80 | + assert requests.get(httpserver.url_for("/")).text == "fallback" |
| 81 | + httpserver.check() |
0 commit comments