Skip to content

Commit

Permalink
Handle json and text errors in ParsedRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
DevL committed Mar 8, 2024
1 parent fe09136 commit 93106d2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
16 changes: 11 additions & 5 deletions src/requtests/parsed_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,30 @@ def headers(self) -> Dict[str, str]:

@property
def json(self) -> Any:
"""
The body of the prepared request, parsed as JSON.
"""
return json.loads(self.prepared_request.body)

@property
def method(self) -> str:
return self.prepared_request.method

@property
def query(self) -> Dict[str, Any]:
return {key: _delist(value) for key, value in self._parsed_query.items()}

@property
def text(self) -> str:
return self.prepared_request.body.decode()
"""
The body of the prepared request, decoded as Unicode.
"""
return self.prepared_request.body.decode() if self.prepared_request.body else ""

@property
def url(self) -> str:
return self.prepared_request.url

@property
def url_params(self) -> Dict[str, Any]:
return {key: _delist(value) for key, value in self._parsed_query.items()}


def _delist(value: List[Any]) -> Union[Any, List[Any]]:
"""
Expand Down
39 changes: 19 additions & 20 deletions tests/parsed_request_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from json import JSONDecodeError
import re
import pytest
from requtests import ParsedRequest
from tests.test_utils import build_request
Expand Down Expand Up @@ -35,31 +37,28 @@ def test_parsing_a_prepared_request(prepared_request):
assert parsed.headers is prepared_request.headers

assert parsed.endpoint == "https://api.example.com/"
assert parsed.url_params == {"a": "1", "b": "2", "c": ["3", "4", "5"]}
assert parsed.query == {"a": "1", "b": "2", "c": ["3", "4", "5"]}
assert parsed.json == {"some": "data"}
assert parsed.text == '{"some": "data"}'


@pytest.mark.skip
def test_endpoint():
pass


@pytest.mark.skip
def test_url_params():
pass


@pytest.mark.skip
def test_json_with_an_empty_body():
pass
def test_json_with_an_empty_body(prepared_request):
parsed = ParsedRequest(prepared_request)
parsed.prepared_request.body = None
expected_message = "the JSON object must be str, bytes or bytearray, not NoneType"
with pytest.raises(TypeError, match=expected_message):
assert parsed.json


@pytest.mark.skip
def test_json_with_an_invalid_json_body():
pass
def test_json_with_an_invalid_json_body(prepared_request):
parsed = ParsedRequest(prepared_request)
parsed.prepared_request.body = '{"broken": "json'
expected_message = re.escape("Unterminated string starting at: line 1 column 12 (char 11)")
with pytest.raises(JSONDecodeError, match=expected_message):
assert parsed.json


@pytest.mark.skip
def test_text_with_an_empty_body():
pass
def test_text_with_an_empty_body(prepared_request):
parsed = ParsedRequest(prepared_request)
parsed.prepared_request.body = None
assert parsed.text == ""

0 comments on commit 93106d2

Please sign in to comment.