|
| 1 | +"""A successful 204 must not be reported to the caller as a failure (#103). |
| 2 | +
|
| 3 | +Both clients parse EVERY 2xx with `response.json()`. The Rails webhooks |
| 4 | +controller answers `destroy` with `head :no_content` -- a 204 with an empty |
| 5 | +body -- so `client.webhooks.delete(...)` raises JSONDecodeError. The deletion |
| 6 | +SUCCEEDED and the caller was told it failed, which is the worst shape of error |
| 7 | +for a mutation: the obvious recovery is to retry a delete that already worked. |
| 8 | +
|
| 9 | +Narrow by design: |
| 10 | + - an empty body on a 2xx is success, and `delete` keeps returning None; |
| 11 | + - a MALFORMED NON-EMPTY 200 stays an error -- it is a real parse failure and |
| 12 | + must not be laundered into an empty success; |
| 13 | + - typed errors for 401/403/429 are untouched. |
| 14 | +""" |
| 15 | + |
| 16 | +import httpx |
| 17 | +import pytest |
| 18 | +import respx |
| 19 | + |
| 20 | +from oilpriceapi import AsyncOilPriceAPI, OilPriceAPI |
| 21 | +from oilpriceapi.exceptions import ( |
| 22 | + AuthenticationError, |
| 23 | + OilPriceAPIError, |
| 24 | +) |
| 25 | + |
| 26 | +BASE = "https://api.oilpriceapi.com" |
| 27 | +WEBHOOK = "/v1/webhooks/fixture-id" |
| 28 | + |
| 29 | + |
| 30 | +def _sync(): |
| 31 | + return OilPriceAPI(api_key="k", base_url=BASE, max_retries=1) |
| 32 | + |
| 33 | + |
| 34 | +def _async(): |
| 35 | + return AsyncOilPriceAPI(api_key="k", base_url=BASE, max_retries=1) |
| 36 | + |
| 37 | + |
| 38 | +# --- 204 with no body is success ------------------------------------------- |
| 39 | + |
| 40 | +@respx.mock |
| 41 | +def test_sync_delete_accepts_204_no_content(): |
| 42 | + respx.delete(f"{BASE}{WEBHOOK}").mock(return_value=httpx.Response(204)) |
| 43 | + assert _sync().webhooks.delete("fixture-id") is None |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.asyncio |
| 47 | +@respx.mock |
| 48 | +async def test_async_delete_accepts_204_no_content(): |
| 49 | + respx.delete(f"{BASE}{WEBHOOK}").mock(return_value=httpx.Response(204)) |
| 50 | + assert await _async().webhooks.delete("fixture-id") is None |
| 51 | + |
| 52 | + |
| 53 | +@respx.mock |
| 54 | +def test_sync_request_returns_empty_dict_for_204(): |
| 55 | + respx.delete(f"{BASE}{WEBHOOK}").mock(return_value=httpx.Response(204)) |
| 56 | + assert _sync().request("DELETE", WEBHOOK) == {} |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.asyncio |
| 60 | +@respx.mock |
| 61 | +async def test_async_request_returns_empty_dict_for_204(): |
| 62 | + respx.delete(f"{BASE}{WEBHOOK}").mock(return_value=httpx.Response(204)) |
| 63 | + assert await _async().request("DELETE", WEBHOOK) == {} |
| 64 | + |
| 65 | + |
| 66 | +@respx.mock |
| 67 | +def test_request_with_headers_accepts_204(): |
| 68 | + """The third decode site, which #103 names alongside the other two.""" |
| 69 | + respx.delete(f"{BASE}{WEBHOOK}").mock( |
| 70 | + return_value=httpx.Response(204, headers={"X-Request-Id": "abc"}) |
| 71 | + ) |
| 72 | + body, headers = _sync().request_with_headers("DELETE", WEBHOOK) |
| 73 | + assert body == {} |
| 74 | + assert headers["X-Request-Id"] == "abc" |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize("status", [200, 202, 204]) |
| 78 | +@respx.mock |
| 79 | +def test_any_2xx_with_an_empty_body_is_success(status): |
| 80 | + """A 200 or 202 with a genuinely empty body is the same situation.""" |
| 81 | + respx.delete(f"{BASE}{WEBHOOK}").mock( |
| 82 | + return_value=httpx.Response(status, content=b"") |
| 83 | + ) |
| 84 | + assert _sync().request("DELETE", WEBHOOK) == {} |
| 85 | + |
| 86 | + |
| 87 | +@respx.mock |
| 88 | +def test_whitespace_only_body_is_treated_as_empty(): |
| 89 | + respx.delete(f"{BASE}{WEBHOOK}").mock( |
| 90 | + return_value=httpx.Response(200, content=b"\n \n") |
| 91 | + ) |
| 92 | + assert _sync().request("DELETE", WEBHOOK) == {} |
| 93 | + |
| 94 | + |
| 95 | +# --- what must STILL fail --------------------------------------------------- |
| 96 | + |
| 97 | +@respx.mock |
| 98 | +def test_malformed_nonempty_200_is_still_an_error(): |
| 99 | + """A real parse failure must not be laundered into an empty success.""" |
| 100 | + respx.get(f"{BASE}/v1/prices/latest").mock( |
| 101 | + return_value=httpx.Response(200, content=b"{not json") |
| 102 | + ) |
| 103 | + with pytest.raises(Exception) as exc: |
| 104 | + _sync().request("GET", "/v1/prices/latest") |
| 105 | + assert not isinstance(exc.value, type(None)) |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.asyncio |
| 109 | +@respx.mock |
| 110 | +async def test_async_malformed_nonempty_200_is_still_an_error(): |
| 111 | + respx.get(f"{BASE}/v1/prices/latest").mock( |
| 112 | + return_value=httpx.Response(200, content=b"{not json") |
| 113 | + ) |
| 114 | + with pytest.raises(Exception): |
| 115 | + await _async().request("GET", "/v1/prices/latest") |
| 116 | + |
| 117 | + |
| 118 | +@respx.mock |
| 119 | +def test_204_is_not_retried(): |
| 120 | + """A success must not burn the retry budget.""" |
| 121 | + route = respx.delete(f"{BASE}{WEBHOOK}").mock( |
| 122 | + return_value=httpx.Response(204) |
| 123 | + ) |
| 124 | + _sync().request("DELETE", WEBHOOK) |
| 125 | + assert route.call_count == 1 |
| 126 | + |
| 127 | + |
| 128 | +@respx.mock |
| 129 | +def test_401_still_raises_authentication_error(): |
| 130 | + respx.delete(f"{BASE}{WEBHOOK}").mock( |
| 131 | + return_value=httpx.Response(401, json={"error": "bad key"}) |
| 132 | + ) |
| 133 | + with pytest.raises(AuthenticationError): |
| 134 | + _sync().request("DELETE", WEBHOOK) |
| 135 | + |
| 136 | + |
| 137 | +@respx.mock |
| 138 | +def test_403_still_raises_a_typed_error(): |
| 139 | + respx.delete(f"{BASE}{WEBHOOK}").mock( |
| 140 | + return_value=httpx.Response(403, json={"error": "forbidden"}) |
| 141 | + ) |
| 142 | + with pytest.raises(OilPriceAPIError): |
| 143 | + _sync().request("DELETE", WEBHOOK) |
| 144 | + |
| 145 | + |
| 146 | +@pytest.mark.asyncio |
| 147 | +@respx.mock |
| 148 | +async def test_async_401_still_raises_authentication_error(): |
| 149 | + respx.delete(f"{BASE}{WEBHOOK}").mock( |
| 150 | + return_value=httpx.Response(401, json={"error": "bad key"}) |
| 151 | + ) |
| 152 | + with pytest.raises(AuthenticationError): |
| 153 | + await _async().request("DELETE", WEBHOOK) |
| 154 | + |
| 155 | + |
| 156 | +# --- sync/async parity ------------------------------------------------------ |
| 157 | + |
| 158 | +def test_all_three_decode_sites_share_one_helper(): |
| 159 | + """client.request, client.request_with_headers and async_client.request |
| 160 | + must decode identically; three copies of the branch would drift.""" |
| 161 | + import inspect |
| 162 | + |
| 163 | + from oilpriceapi import async_client, client |
| 164 | + |
| 165 | + sync_src = inspect.getsource(client) |
| 166 | + async_src = inspect.getsource(async_client) |
| 167 | + for src, name in ((sync_src, "client"), (async_src, "async_client")): |
| 168 | + assert "decode_json_body" in src, name |
| 169 | + # No bare `return response.json()` left behind. |
| 170 | + assert "return response.json()" not in src, name |
| 171 | + assert "return response.json(), response.headers" not in sync_src |
0 commit comments