|
| 1 | +import json |
| 2 | +import pytest |
| 3 | +from requests import RequestException |
| 4 | +from unittest.mock import Mock |
| 5 | + |
| 6 | +from dimo.request import Request, HTTPError |
| 7 | + |
| 8 | + |
| 9 | +class DummyResponse: |
| 10 | + def __init__( |
| 11 | + self, status_code=200, headers=None, json_data=None, content=b"", txt="" |
| 12 | + ): |
| 13 | + self.status_code = status_code |
| 14 | + self.headers = headers or {} |
| 15 | + self._json_data = json_data |
| 16 | + self.content = content |
| 17 | + self.txt = txt |
| 18 | + # .raise_for_status will be set per-test via side_effect |
| 19 | + |
| 20 | + def json(self): |
| 21 | + if isinstance(self._json_data, Exception): |
| 22 | + raise self._json_data |
| 23 | + return self._json_data |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def session(): |
| 28 | + return Mock() |
| 29 | + |
| 30 | + |
| 31 | +def make_request(session, **kwargs): |
| 32 | + req = Request("POST", "https://api.example.com/endpoint", session) |
| 33 | + return req(**kwargs) |
| 34 | + |
| 35 | + |
| 36 | +def test_json_request_body_is_serialized_and_passed_through(session): |
| 37 | + data = {"foo": "bar"} |
| 38 | + headers = {"Content-Type": "application/json"} |
| 39 | + resp = DummyResponse( |
| 40 | + status_code=200, |
| 41 | + headers={"Content-Type": "application/json"}, |
| 42 | + json_data={"ok": True}, |
| 43 | + ) |
| 44 | + resp.raise_for_status = Mock() |
| 45 | + session.request.return_value = resp |
| 46 | + |
| 47 | + result = make_request(session, headers=headers, data=data) |
| 48 | + |
| 49 | + # ensure data was JSON-dumped |
| 50 | + _, call_kwargs = session.request.call_args |
| 51 | + assert call_kwargs["data"] == json.dumps(data) |
| 52 | + |
| 53 | + # ensure we got the parsed JSON back |
| 54 | + assert result == {"ok": True} |
| 55 | + |
| 56 | + |
| 57 | +def test_non_json_response_returns_raw_content(session): |
| 58 | + resp = DummyResponse( |
| 59 | + status_code=200, |
| 60 | + headers={"Content-Type": "text/plain"}, |
| 61 | + content=b"hello world", |
| 62 | + ) |
| 63 | + resp.raise_for_status = Mock() |
| 64 | + session.request.return_value = resp |
| 65 | + |
| 66 | + result = make_request(session) |
| 67 | + assert result == b"hello world" |
| 68 | + |
| 69 | + |
| 70 | +def test_http_error_wraps_json_body(session): |
| 71 | + # prepare a RequestException with a response whose .json() returns a dict |
| 72 | + err_resp = DummyResponse( |
| 73 | + status_code=400, |
| 74 | + json_data={"error": "Bad things"}, |
| 75 | + txt="Bad things text", |
| 76 | + ) |
| 77 | + exc = RequestException("Bad Request") |
| 78 | + exc.response = err_resp |
| 79 | + |
| 80 | + # have raise_for_status raise that exception |
| 81 | + good_resp = DummyResponse() |
| 82 | + good_resp.raise_for_status = Mock(side_effect=exc) |
| 83 | + session.request.return_value = good_resp |
| 84 | + |
| 85 | + with pytest.raises(HTTPError) as ei: |
| 86 | + make_request(session) |
| 87 | + err = ei.value |
| 88 | + assert err.status == 400 |
| 89 | + assert err.body == {"error": "Bad things"} |
| 90 | + assert "Bad Request" in str(err) |
0 commit comments