-
Notifications
You must be signed in to change notification settings - Fork 2
Make MockVWS intercept both requests and httpx #2998
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
740846a
Make MockVWS intercept both requests and httpx
adamtheturtle 13c1696
Fix pylint C0413 and C0402 in respx decorators
adamtheturtle cbcd178
Suppress pyrefly false positive with inline ignore comments
adamtheturtle fdfb20a
Replace concrete type imports with Protocol to fix pyrefly false posi…
adamtheturtle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,9 @@ | ||
| Getting started | ||
| --------------- | ||
|
|
||
| Mocking calls made to Vuforia with Python ``requests`` | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| Mocking calls made to Vuforia | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| .. include:: basic-example.rst | ||
|
|
||
| Mocking calls made to Vuforia with Python ``httpx`` | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| .. include:: httpx-example.rst |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,18 @@ | ||
| Using the mock redirects requests to Vuforia made with `httpx`_ to an in-memory implementation. | ||
| ``MockVWS`` also intercepts requests made with `httpx`_. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| """Make a request to the Vuforia Web Services API mock.""" | ||
| """Make a request to the Vuforia Web Services API mock using httpx.""" | ||
|
|
||
| import httpx | ||
|
|
||
| from mock_vws import MockVWSForHttpx | ||
| from mock_vws import MockVWS | ||
| from mock_vws.database import CloudDatabase | ||
|
|
||
| with MockVWSForHttpx() as mock: | ||
| with MockVWS() as mock: | ||
| database = CloudDatabase() | ||
| mock.add_cloud_database(cloud_database=database) | ||
| # This will use the Vuforia mock. | ||
| httpx.get(url="https://vws.vuforia.com/summary", timeout=30) | ||
|
|
||
| By default, an exception will be raised if any requests to unmocked addresses are made. | ||
|
|
||
| See :ref:`mock-api-reference` for details of what can be changed and how. | ||
|
|
||
| .. _httpx: https://pypi.org/project/httpx/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,9 @@ | ||
| """Tools for using a fake implementation of Vuforia.""" | ||
|
|
||
| from mock_vws._requests_mock_server.decorators import ( | ||
| MissingSchemeError, | ||
| MockVWS, | ||
| ) | ||
| from mock_vws._respx_mock_server.decorators import MockVWSForHttpx | ||
| from mock_vws._mock_common import MissingSchemeError | ||
| from mock_vws._requests_mock_server.decorators import MockVWS | ||
|
|
||
| __all__ = [ | ||
| "MissingSchemeError", | ||
| "MockVWS", | ||
| "MockVWSForHttpx", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Responses mock leaked if respx router setup fails
Medium Severity
In
__enter__,self._mock.start()is called beforestart_respx_router(). Ifstart_respx_routerraises an exception,__exit__never runs (the context manager wasn't successfully entered), so theresponsesmock remains active and silently intercepts all subsequentrequestscalls. Similarly in__exit__, ifself._mock.stop()raises,self._router.stop()is skipped and therespxrouter stays active. Both resources needtry/finallyprotection since neither cleanup is guaranteed to run if the other fails.Additional Locations (1)
src/mock_vws/_requests_mock_server/decorators.py#L263-L265