falconry / falcon

The no-magic web data plane API and microservices framework for Python developers, with a focus on reliability, correctness, and performance at scale.
https://falcon.readthedocs.io/en/stable/
Apache License 2.0
9.51k stars 937 forks source link

Add `__repr__` to test Result #2044

Closed vytas7 closed 2 years ago

vytas7 commented 2 years ago

At the time of this writing, falcon.testing.Result has no custom __repr__:

>>> import falcon
>>> import falcon.testing
>>> app = falcon.App()
>>> client = falcon.testing.TestClient(app)
>>> result = client.get('/something')
>>> result
<falcon.testing.client.Result object at 0x7f292ce046d0>

__repr__ is also picked up and shown by the popular pytest.

It could be nice to add a representation with a concise summary describing the most important attributes of the result (status code etc).

CaselIT commented 2 years ago

I think it would make sense to log the status and maybe a substring of the text, something like the first 50 / 100 chars? or maybe the first 25 - last 25?

vytas7 commented 2 years ago

Yeah, something like that. Maybe the response's Content-Type too?

RioAtHome commented 2 years ago

Hello, i would like to work on this issue. From my understatement of this issue i just need to add a __repr__ function to falcon.testing.client.Result class. Should i just log the status code, a part of the text and the header content-type ?

vytas7 commented 2 years ago

Hi @RioAtHome! Sure, go ahead!

Yes, your understanding is correct -- you just need to add a __repr__ for the class in question (and add a test for it). Although it is a bit open ended -- feel free to tweak that representation to your liking or come up with your own suggestions.

RioAtHome commented 2 years ago

Hello!. How about logging the status code, most important headers(Content-Type, Content-length, Authorization and Cookie) and parts of the text/content(as stated above, just log the beginning and end)?

vytas7 commented 2 years ago

Hi again! Cookies are probably not the most common case for APIs, although it of course varies. __repr__ should be reasonably short... So probably we cannot afford cramming in too much stuff, although it is tempting.

vytas7 commented 2 years ago

Implemented in https://github.com/falconry/falcon/pull/2046