Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor encode/decode raw response #82

Merged
merged 4 commits into from
Oct 5, 2020
Merged

Refactor encode/decode raw response #82

merged 4 commits into from
Oct 5, 2020

Conversation

lundberg
Copy link
Owner

@lundberg lundberg commented Oct 2, 2020

Refactors encoding and decoding of raw responses by using httpx.Response.

Enables support for creating response templates with content, text, html and json body variants.

Public api and MockTransport can easily be extended with these "body" args in a separate PR, but probably after #81 which restricting kwargs in api methods.

@lundberg lundberg added the enhancement New feature or request label Oct 2, 2020
@codecov-commenter
Copy link

codecov-commenter commented Oct 2, 2020

Codecov Report

Merging #82 into master will increase coverage by 0.53%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           master       #82      +/-   ##
===========================================
+ Coverage   99.46%   100.00%   +0.53%     
===========================================
  Files          11        11              
  Lines        1130      1172      +42     
  Branches       74        80       +6     
===========================================
+ Hits         1124      1172      +48     
+ Misses          5         0       -5     
+ Partials        1         0       -1     
Impacted Files Coverage Δ
respx/api.py 100.00% <ø> (+7.69%) ⬆️
tests/test_transports.py 100.00% <ø> (ø)
respx/models.py 100.00% <100.00%> (+0.58%) ⬆️
respx/transports.py 100.00% <100.00%> (+1.53%) ⬆️
tests/test_api.py 100.00% <100.00%> (ø)
tests/test_mock.py 100.00% <100.00%> (ø)
tests/test_stats.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 706ae48...29f38e7. Read the comment docs.

Comment on lines +283 to +287
# Pre-read response, but only if mocked, not pass-through streams streams
if response and not isinstance(
response.stream, (SyncByteStream, AsyncByteStream)
):
response.read()
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved the response auto reading from decode_response, previously build_response, to here since the pre-read is only for easier stats and recording.

I also needed to reverse the stream type check now that we rely on HTTPX streams and not our own ContentStream, which now is dropped.

@bhumilsarvaiya, could you please try this PR and verify that pass through requests still works as expected, like #68 fixed.

@lundberg lundberg mentioned this pull request Oct 2, 2020
4 tasks
@lundberg lundberg removed the enhancement New feature or request label Oct 2, 2020
Comment on lines +351 to +355
@pytest.mark.skipif(
os.environ.get("PASS_THROUGH") is None, reason="External pass-through disabled"
)
@pytest.mark.asyncio
async def test_external_pass_through(client): # pragma: nocover
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test case for a real external pass-through call, for easier testing/verifying that #68 still works.

The test is skipped by default, to not call remote, but can be run individually with:

$ PASS_THROUGH=1 nox -s test -- -k test_external_pass_through

Pass-through responses still seams to work, but a second pair of eyes is appreciated @bhumilsarvaiya ;-)

@lundberg lundberg merged commit 7719fab into master Oct 5, 2020
@lundberg lundberg deleted the text-html-json branch October 5, 2020 19:06
lundberg added a commit that referenced this pull request Oct 15, 2020
Added
- Added `text`, `html` and `json` content shorthands to ResponseTemplate. (PR #82)
- Added `text`, `html` and `json` content shorthands to high level API. (PR #93)
- Added support to set `http_version` for a mocked response. (PR #82)
- Added support for mocking by lowercase http methods, thanks @lbillinghamtn. (PR #80)
- Added query `params` to align with HTTPX API, thanks @jocke-l. (PR #81)
- Easier API to get request/response from call stats, thanks @SlavaSkvortsov. (PR #85)
- Enhanced test to verify better content encoding by HTTPX. (PR #78)
- Added Python 3.9 to supported versions and test suite, thanks @jairhenrique. (PR #89)

Changed
- `ResponseTemplate.content` as proper getter, i.e. no resolve/encode to bytes. (PR #82)
- Enhanced headers by using HTTPX Response when encoding raw responses. (PR #82)
- Deprecated `respx.stats` in favour of `respx.calls`, thanks @SlavaSkvortsov. (PR #92)

Fixed
- Recorded requests in call stats are pre-read like the responses. (PR #86)
- Postponed request decoding for enhanced performance. (PR #91)
- Lazy call history for enhanced performance, thanks @SlavaSkvortsov. (PR #92)

Removed
- Removed auto setting the `Content-Type: text/plain` header. (PR #82)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants