Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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:master
base:master
Choose a base branch
Loading
fromalbertedwardson:request-obj-tests
Draft
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletionstests/test_request_attrs.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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))

[8]ページ先頭

©2009-2025 Movatter.jp