-
-
Notifications
You must be signed in to change notification settings - Fork 346
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
mbeliaev/recorder #545
Merged
Merged
mbeliaev/recorder #545
Changes from 11 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ee1c617
added responses recorder
beliaev-maksim 46f42ea
changelist
beliaev-maksim ba58124
clean up
beliaev-maksim d041a2d
Merge branch 'master' into mbeliaev/recorder
beliaev-maksim 15710b5
simplify recorder
beliaev-maksim 0d3944e
simplify recorder
beliaev-maksim 5b69002
fix some mypy issues
beliaev-maksim 0b62909
type annotations
beliaev-maksim e8e7db4
type annotations
beliaev-maksim 9a77d68
type annotations
beliaev-maksim 7129168
added body
beliaev-maksim a0031b4
Update responses/registries.py
beliaev-maksim fef3f0c
Merge branch 'master' into mbeliaev/recorder
beliaev-maksim 9b7ca72
Merge remote-tracking branch 'origin/mbeliaev/recorder' into mbeliaev…
beliaev-maksim c4e59a3
CHANGES
beliaev-maksim b664e36
Merge branch 'master' into mbeliaev/recorder
beliaev-maksim 3abd841
added tests
beliaev-maksim 1e75eae
added tests
beliaev-maksim f7f937e
move dumping to the recorder
beliaev-maksim File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from functools import wraps | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from typing import Any | ||
from typing import Callable | ||
from typing import Type | ||
from typing import Union | ||
import os | ||
from responses import FirstMatchRegistry | ||
from responses import HTTPAdapter | ||
from responses import PreparedRequest | ||
from responses import models | ||
from responses import _F | ||
|
||
from responses import RequestsMock | ||
from responses import Response | ||
from responses import _real_send | ||
from responses.registries import OrderedRegistry | ||
|
||
|
||
class Recorder(RequestsMock): | ||
def __init__( | ||
self, | ||
target: str = "requests.adapters.HTTPAdapter.send", | ||
registry: "Type[FirstMatchRegistry]" = OrderedRegistry, | ||
) -> None: | ||
super().__init__(target=target, registry=registry) | ||
|
||
def reset(self) -> None: | ||
self._registry = OrderedRegistry() | ||
|
||
def record( | ||
self, *, file_path: "Union[str, bytes, os.PathLike[Any]]" = "response.toml" | ||
) -> "Union[Callable[[_F], _F], _F]": | ||
def deco_record(function: "_F") -> "Callable[..., Any]": | ||
@wraps(function) | ||
def wrapper(*args: "Any", **kwargs: "Any") -> "Any": # type: ignore[misc] | ||
with self: | ||
ret = function(*args, **kwargs) | ||
with open(file_path, "w") as file: | ||
self.get_registry()._dump(file) | ||
|
||
return ret | ||
|
||
return wrapper | ||
|
||
return deco_record | ||
|
||
def _on_request( | ||
self, | ||
adapter: "HTTPAdapter", | ||
request: "PreparedRequest", | ||
**kwargs: "Any", | ||
) -> "models.Response": | ||
# add attributes params and req_kwargs to 'request' object for further match comparison | ||
# original request object does not have these attributes | ||
request.params = self._parse_request_params(request.path_url) # type: ignore[attr-defined] | ||
request.req_kwargs = kwargs # type: ignore[attr-defined] | ||
requests_response = _real_send(adapter, request, **kwargs) | ||
responses_response = Response( | ||
method=str(request.method), | ||
url=str(requests_response.request.url), | ||
status=requests_response.status_code, | ||
body=requests_response.text, | ||
) | ||
self._registry.add(responses_response) | ||
return requests_response | ||
|
||
def stop(self, allow_assert: bool = True) -> None: | ||
super().stop(allow_assert=False) | ||
|
||
|
||
recorder = Recorder() | ||
record = recorder.record |
This file contains 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 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
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.
Should dumping to a file be a separate thing? That would let userland code or us in the future change the serialization format that is being used to save response fixtures.
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.
I decided to create it in the registry because this will allow to dump existing tests to the file
Eg
If user ran
responses.add()
multiple times, then user can runresponses.get_registry()._dump(*args)
That will allow safe (compared to brand new recording) transfer to the new method if required
In the future we can provide
serializer
argument, but at the moment I am a bit reluctant to support other serializers than one we decideAlso, user can also overwrite this method after inheriting
Or what are the other options to introduce this method?
As a separate function?
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.
Couldn't dumping be on the
Recorder()
? I'm concerned that recording feels like it is half in the Recorder and half in the registry right now.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.
@markstory
done!
this could be done even as a separate function. Then it will be possible to easily dump existing registries into the file