-
-
Notifications
You must be signed in to change notification settings - Fork 358
✨ feat: add option to save additional headers when recording #773
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
Open
iamrajjoshi
wants to merge
2
commits into
master
Choose a base branch
from
raj/additional-headers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+192
−26
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,24 +37,35 @@ def _remove_nones(d: "Any") -> "Any": | |
return d | ||
|
||
|
||
def _remove_default_headers(data: "Any") -> "Any": | ||
def _remove_default_headers( | ||
data: "Any", additional_headers: "Optional[List[str]]" = None | ||
) -> "Any": | ||
""" | ||
It would be too verbose to store these headers in the file generated by the | ||
record functionality. | ||
record functionality. If additional_headers is provided, those headers | ||
will be preserved in addition to the normal behavior. | ||
""" | ||
if isinstance(data, dict): | ||
keys_to_remove = [ | ||
default_keys_to_remove = [ | ||
"Content-Length", | ||
"Content-Type", | ||
"Date", | ||
"Server", | ||
"Connection", | ||
"Content-Encoding", | ||
] | ||
|
||
for i, response in enumerate(data["responses"]): | ||
for key in keys_to_remove: | ||
if key in response["response"]["headers"]: | ||
# Remove default headers as before, but preserve additional headers | ||
keys_to_preserve = set(additional_headers) if additional_headers else set() | ||
|
||
for key in default_keys_to_remove: | ||
if ( | ||
key in response["response"]["headers"] | ||
and key not in keys_to_preserve | ||
): | ||
del data["responses"][i]["response"]["headers"][key] | ||
|
||
if not response["response"]["headers"]: | ||
del data["responses"][i]["response"]["headers"] | ||
return data | ||
|
@@ -64,6 +75,7 @@ def _dump( | |
registered: "List[BaseResponse]", | ||
destination: "Union[BinaryIO, TextIOWrapper]", | ||
dumper: "Callable[[Union[Dict[Any, Any], List[Any]], Union[BinaryIO, TextIOWrapper]], Any]", | ||
additional_headers: "Optional[List[str]]" = None, | ||
) -> None: | ||
data: Dict[str, Any] = {"responses": []} | ||
for rsp in registered: | ||
|
@@ -88,7 +100,9 @@ def _dump( | |
"Probably you use custom Response object that is missing required attributes" | ||
) from exc | ||
|
||
dumper(_remove_default_headers(_remove_nones(data)), destination) | ||
dumper( | ||
_remove_default_headers(_remove_nones(data), additional_headers), destination | ||
) | ||
|
||
|
||
class Recorder(RequestsMock): | ||
|
@@ -104,15 +118,20 @@ def reset(self) -> None: | |
self._registry = OrderedRegistry() | ||
|
||
def record( | ||
self, *, file_path: "Union[str, bytes, os.PathLike[Any]]" = "response.yaml" | ||
self, | ||
*, | ||
file_path: "Union[str, bytes, os.PathLike[Any]]" = "response.yaml", | ||
additional_headers: "Optional[List[str]]" = None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here with |
||
) -> "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) | ||
self.dump_to_file( | ||
file_path=file_path, registered=self.get_registry().registered | ||
file_path=file_path, | ||
registered=self.get_registry().registered, | ||
additional_headers=additional_headers, | ||
) | ||
|
||
return ret | ||
|
@@ -126,12 +145,13 @@ def dump_to_file( | |
file_path: "Union[str, bytes, os.PathLike[Any]]", | ||
*, | ||
registered: "Optional[List[BaseResponse]]" = None, | ||
additional_headers: "Optional[List[str]]" = None, | ||
) -> None: | ||
"""Dump the recorded responses to a file.""" | ||
if registered is None: | ||
registered = self.get_registry().registered | ||
with open(file_path, "w") as file: | ||
_dump(registered, file, yaml.dump) | ||
_dump(registered, file, yaml.dump, additional_headers) | ||
|
||
def _on_request( | ||
self, | ||
|
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 | ||||
---|---|---|---|---|---|---|
|
@@ -22,7 +22,7 @@ def get_data(host, port): | |||||
{ | ||||||
"response": { | ||||||
"method": "GET", | ||||||
"url": f"http://{host}:{port}/404", | ||||||
"url": f"http://{host}:{port}/404", # noqa: E231 | ||||||
"headers": {"x": "foo"}, | ||||||
"body": "404 Not Found", | ||||||
"status": 404, | ||||||
|
@@ -33,7 +33,7 @@ def get_data(host, port): | |||||
{ | ||||||
"response": { | ||||||
"method": "GET", | ||||||
"url": f"http://{host}:{port}/status/wrong", | ||||||
"url": f"http://{host}:{port}/status/wrong", # noqa: E231 | ||||||
"headers": {"x": "foo"}, | ||||||
"body": "Invalid status code", | ||||||
"status": 400, | ||||||
|
@@ -44,7 +44,7 @@ def get_data(host, port): | |||||
{ | ||||||
"response": { | ||||||
"method": "GET", | ||||||
"url": f"http://{host}:{port}/500", | ||||||
"url": f"http://{host}:{port}/500", # noqa: E231 | ||||||
"headers": {"x": "foo"}, | ||||||
"body": "500 Internal Server Error", | ||||||
"status": 500, | ||||||
|
@@ -55,7 +55,7 @@ def get_data(host, port): | |||||
{ | ||||||
"response": { | ||||||
"method": "PUT", | ||||||
"url": f"http://{host}:{port}/202", | ||||||
"url": f"http://{host}:{port}/202", # noqa: E231 | ||||||
"body": "OK", | ||||||
"status": 202, | ||||||
"content_type": "text/plain", | ||||||
|
@@ -97,11 +97,13 @@ def run(): | |||||
def test_recorder_toml(self, httpserver): | ||||||
custom_recorder = _recorder.Recorder() | ||||||
|
||||||
def dump_to_file(file_path, registered): | ||||||
def dump_to_file(file_path, registered=None, additional_headers=None): | ||||||
if registered is None: | ||||||
registered = custom_recorder.get_registry().registered | ||||||
with open(file_path, "wb") as file: | ||||||
_dump(registered, file, tomli_w.dump) # type: ignore[arg-type] | ||||||
_dump(registered, file, tomli_w.dump, additional_headers) # type: ignore[arg-type] | ||||||
|
||||||
custom_recorder.dump_to_file = dump_to_file # type: ignore[assignment] | ||||||
custom_recorder.dump_to_file = dump_to_file # type: ignore[method-assign] | ||||||
|
||||||
url202, url400, url404, url500 = self.prepare_server(httpserver) | ||||||
|
||||||
|
@@ -238,3 +240,145 @@ def _parse_resp_f(file_path): | |||||
assert responses.registered()[3].content_type == "text/plain" | ||||||
|
||||||
run() | ||||||
|
||||||
|
||||||
class TestRecorderAdditionalHeaders: | ||||||
def setup_method(self): | ||||||
self.out_file = Path("response_record_headers") | ||||||
if self.out_file.exists(): | ||||||
self.out_file.unlink() | ||||||
assert not self.out_file.exists() | ||||||
|
||||||
def teardown_method(self): | ||||||
if self.out_file.exists(): | ||||||
self.out_file.unlink() | ||||||
|
||||||
def prepare_server_with_headers(self, httpserver): | ||||||
httpserver.expect_request("/test").respond_with_data( | ||||||
"Test Response", | ||||||
status=200, | ||||||
content_type="text/plain", | ||||||
headers={ | ||||||
"Content-Length": "13", | ||||||
"Server": "nginx/1.0", | ||||||
"Connection": "keep-alive", | ||||||
"Content-Encoding": "identity", | ||||||
"Authorization": "Bearer token123", | ||||||
"X-Custom-Header": "custom-value", | ||||||
"User-Agent": "test-agent", | ||||||
}, | ||||||
) | ||||||
return httpserver.url_for("/test") | ||||||
|
||||||
def test_recorder_with_additional_headers(self, httpserver): | ||||||
url = self.prepare_server_with_headers(httpserver) | ||||||
|
||||||
@_recorder.record( | ||||||
file_path=self.out_file, | ||||||
additional_headers=["Authorization", "X-Custom-Header", "Date"], | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think this reads better, what do you think? |
||||||
) | ||||||
def run(): | ||||||
requests.get(url) | ||||||
|
||||||
run() | ||||||
|
||||||
with open(self.out_file) as file: | ||||||
data = yaml.safe_load(file) | ||||||
|
||||||
response_headers = data["responses"][0]["response"]["headers"] | ||||||
|
||||||
# Additional headers should be preserved | ||||||
assert "Authorization" in response_headers | ||||||
assert response_headers["Authorization"] == "Bearer token123" | ||||||
assert "X-Custom-Header" in response_headers | ||||||
assert response_headers["X-Custom-Header"] == "custom-value" | ||||||
|
||||||
# Default headers should still be removed (not in additional_headers) | ||||||
assert "Content-Length" not in response_headers | ||||||
assert "Server" not in response_headers | ||||||
assert "Connection" not in response_headers | ||||||
assert "Content-Encoding" not in response_headers | ||||||
|
||||||
# Other headers not in default removal list should remain | ||||||
assert "User-Agent" in response_headers | ||||||
|
||||||
def test_recorder_with_additional_headers_preserves_default_removal( | ||||||
self, httpserver | ||||||
): | ||||||
url = self.prepare_server_with_headers(httpserver) | ||||||
|
||||||
@_recorder.record( | ||||||
file_path=self.out_file, additional_headers=["Content-Type", "Server"] | ||||||
) | ||||||
def run(): | ||||||
requests.get(url) | ||||||
|
||||||
run() | ||||||
|
||||||
with open(self.out_file) as file: | ||||||
data = yaml.safe_load(file) | ||||||
|
||||||
response_headers = data["responses"][0]["response"]["headers"] | ||||||
|
||||||
# Headers in additional_headers should be preserved even if normally removed | ||||||
assert "Content-Type" in response_headers | ||||||
assert response_headers["Content-Type"] == "text/plain" | ||||||
assert "Server" in response_headers | ||||||
assert "nginx/1.0" in response_headers["Server"] | ||||||
|
||||||
# Other default headers should still be removed | ||||||
assert "Content-Length" not in response_headers | ||||||
assert "Connection" not in response_headers | ||||||
assert "Content-Encoding" not in response_headers | ||||||
|
||||||
def test_recorder_without_additional_headers_default_behavior(self, httpserver): | ||||||
url = self.prepare_server_with_headers(httpserver) | ||||||
|
||||||
@_recorder.record(file_path=self.out_file) | ||||||
def run(): | ||||||
requests.get(url) | ||||||
|
||||||
run() | ||||||
|
||||||
with open(self.out_file) as file: | ||||||
data = yaml.safe_load(file) | ||||||
|
||||||
response_headers = data["responses"][0]["response"]["headers"] | ||||||
|
||||||
# Default headers should be removed | ||||||
assert "Content-Length" not in response_headers | ||||||
assert "Content-Type" not in response_headers | ||||||
assert "Server" not in response_headers | ||||||
assert "Connection" not in response_headers | ||||||
assert "Content-Encoding" not in response_headers | ||||||
|
||||||
# Non-default headers should remain | ||||||
assert "Authorization" in response_headers | ||||||
assert "X-Custom-Header" in response_headers | ||||||
assert "User-Agent" in response_headers | ||||||
|
||||||
def test_dump_to_file_with_additional_headers(self, httpserver): | ||||||
url = self.prepare_server_with_headers(httpserver) | ||||||
|
||||||
_recorder.recorder.start() | ||||||
requests.get(url) | ||||||
_recorder.recorder.stop() | ||||||
|
||||||
_recorder.recorder.dump_to_file( | ||||||
self.out_file, additional_headers=["Content-Length"] | ||||||
) | ||||||
|
||||||
with open(self.out_file) as file: | ||||||
data = yaml.safe_load(file) | ||||||
|
||||||
response_headers = data["responses"][0]["response"]["headers"] | ||||||
|
||||||
# Additional headers should be preserved | ||||||
assert "Content-Length" in response_headers | ||||||
|
||||||
# Other default headers should be removed | ||||||
assert "Server" not in response_headers | ||||||
assert "Connection" not in response_headers | ||||||
assert "Content-Encoding" not in response_headers | ||||||
|
||||||
_recorder.recorder.reset() |
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.
Should this be
include_headers
orcapture_headers
? On first read my interpretation was that these headers would be added into the recorded responses.