- Notifications
You must be signed in to change notification settings - Fork11.2k
add tests for #7036#7078
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
albertedwardson wants to merge2 commits intoscrapy:masterChoose a base branch fromalbertedwardson:request-obj-tests
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.
Uh oh!
There was an error while loading.Please reload this page.
Draft
add tests for #7036#7078
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletionstests/test_request_attrs.py
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 |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| from typing import Any | ||
| import pytest | ||
| from scrapy.http.request import NO_CALLBACK, Request | ||
| from scrapy.http.request.form import FormRequest | ||
| from scrapy.http.request.json_request import JsonRequest | ||
| _attr_value_map: dict[str, Any] = { | ||
| "url": "http://example.com/test", | ||
| "callback": NO_CALLBACK, | ||
| "method": "POST", | ||
| "headers": { | ||
| b"X-Test-Header": [b"1"], | ||
| # `JsonRequest` will eventually add those even if they are not present | ||
| b"Accept": [b"application/json, text/javascript, */*; q=0.01"], | ||
| b"Content-Type": [b"application/json"], | ||
| }, | ||
| "body": b"hello", | ||
| "cookies": {"a": "1"}, | ||
| "meta": {"k": "v"}, | ||
| "encoding": "koi8-r", | ||
| "priority": 5, | ||
| "dont_filter": True, | ||
| "errback": NO_CALLBACK, | ||
| "flags": ["f1", "f2"], | ||
| "cb_kwargs": {"x": 1}, | ||
| } | ||
| def _assert_equal_attribute(obj: Request, attr: str, expected: Any): | ||
| val = getattr(obj, attr) | ||
| if attr == "headers": | ||
| # Headers object -> dict | ||
| assert dict(val) == dict(expected) | ||
| else: | ||
| assert val == expected | ||
| @pytest.mark.parametrize("request_class", [Request, JsonRequest, FormRequest]) | ||
| @pytest.mark.parametrize("attr", Request.attributes) | ||
| def test_attribute_setattr_and_replace_behavior( | ||
| request_class: type[Request], attr: str | ||
| ): | ||
| """Ensure current assignment and replace semantics for Request.attributes. | ||
| - If setattr(obj, attr, val) works today, it must keep working and | ||
| replace() should carry the value over. | ||
| - If setattr(obj, attr, val) raises AttributeError today (read-only), | ||
| replace(**{attr: val}) should still allow creating a Request with that attr. | ||
| """ | ||
| r = request_class("http://example.com/") | ||
| if attr not in _attr_value_map: | ||
| return | ||
| val = _attr_value_map[attr] | ||
| # first try direct setattr | ||
| try: | ||
| setattr(r, attr, val) | ||
| except AttributeError: | ||
| # attribute is read-only | ||
| # ensure replace(**{attr: val}) creates a new request with that value | ||
| r2 = r.replace(**{attr: val}) | ||
| _assert_equal_attribute(r2, attr, val) | ||
| # original request must remain unchanged (unless replace mutated it) | ||
| # (for read-only attributes we expect original not to equal val) | ||
| if getattr(r, attr) == val: | ||
| pytest.fail( | ||
| f"Attribute {attr} unexpectedly mutated original Request when " | ||
| f"it should have been read-only (direct setattr raised)." | ||
| ) | ||
| else: | ||
| # direct setattr succeeded; attribute must reflect assigned value | ||
| _assert_equal_attribute(r, attr, val) | ||
| # and replace() must preserve it (replace uses getattr(self, x)) | ||
| r2 = r.replace() | ||
| _assert_equal_attribute(r2, attr, getattr(r, attr)) |
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.