Skip to content

Commit

Permalink
Merge branch 'master' into router_slash_support
Browse files Browse the repository at this point in the history
  • Loading branch information
kgriffs committed Jun 1, 2022
2 parents bec616c + 83682f6 commit 18fcc6a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/_newsfragments/2044.newandimproved.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
An informative representation was added to :class:`testing.Result <falcon.testing.Result>`
for easier development and interpretation of failed tests. The form of ``__repr__`` is as follows:
``Result<{status_code} {content-type header} {content}>``, where the content part will reflect
up to 40 bytes of the result's content.
13 changes: 13 additions & 0 deletions falcon/testing/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,19 @@ def json(self) -> Optional[Union[dict, list, str, int, float, bool]]:

return json_module.loads(self.text)

def __repr__(self):
content_type = self.headers.get('Content-Type', '')

if len(self.content) > 40:
content = self.content[:20] + b'...' + self.content[-20:]
else:
content = self.content

args = [self.status, content_type, str(content)]

repr_result = ' '.join(filter(None, args))
return 'Result<{}>'.format(repr_result)


class StreamedResult(_ResultBase):
"""Encapsulates the streamed result of an ASGI request.
Expand Down
59 changes: 59 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,65 @@ def test_status(self, app):
result = client.simulate_get()
assert result.status == falcon.HTTP_702

@pytest.mark.parametrize(
'simulate',
[
testing.simulate_get,
testing.simulate_post,
],
)
@pytest.mark.parametrize(
'value',
(
'd\xff\xff\x00',
'quick fox jumps over the lazy dog',
'{"hello": "WORLD!"}',
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praese',
'{"hello": "WORLD!", "greetings": "fellow traveller"}',
'\xe9\xe8',
),
)
def test_repr_result_when_body_varies(self, asgi, value, simulate):
if isinstance(value, str):
value = bytes(value, 'UTF-8')

if asgi:
resource = testing.SimpleTestResourceAsync(body=value)
else:
resource = testing.SimpleTestResource(body=value)

app = create_app(asgi)
app.add_route('/hello', resource)

result = simulate(app, '/hello')
captured_resp = resource.captured_resp
content = captured_resp.text

if len(value) > 40:
content = value[:20] + b'...' + value[-20:]
else:
content = value

args = [
captured_resp.status,
captured_resp.headers['content-type'],
str(content),
]

expected_content = ' '.join(filter(None, args))

expected_result = 'Result<{}>'.format(expected_content)

assert str(result) == expected_result

def test_repr_without_content_type_header(self, asgi):
value = b'huh'
header = [('Not-content-type', 'no!')]
result = falcon.testing.Result([value], falcon.HTTP_200, header)

expected_result = 'Result<200 OK {}>'.format(value)
assert str(result) == expected_result

def test_wsgi_iterable_not_closeable(self):
result = testing.Result([], falcon.HTTP_200, [])
assert not result.content
Expand Down

0 comments on commit 18fcc6a

Please sign in to comment.