-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add conversion methods to Request (#5)
- Loading branch information
Showing
7 changed files
with
273 additions
and
23 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
[flake8] | ||
extend-select = TC, TC1 | ||
ignore = | ||
# D205: 1 blank line required between summary line and description | ||
# D400: First line should end with a period | ||
# We need longer summary lines, specially since we use Sphinx syntax. | ||
D205, D400 | ||
max-line-length = 88 | ||
per-file-ignores = | ||
# F401: Imported but unused | ||
form2request/__init__.py:F401 | ||
# D100-D104: Missing docstring | ||
docs/conf.py:D100 | ||
tests/__init__.py:D104 | ||
tests/test_conversion.py:D100,D103 | ||
tests/test_main.py:D100,D103 |
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
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
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,156 @@ | ||
import pytest | ||
|
||
from form2request import Request | ||
|
||
web_poet = pytest.importorskip("web_poet") | ||
scrapy = pytest.importorskip("scrapy") | ||
requests = pytest.importorskip("requests") | ||
|
||
|
||
def fake_scrapy_callback(self, response): | ||
pass | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("request_data", "method", "kwargs", "expected"), | ||
( | ||
# GET | ||
*( | ||
( | ||
Request( | ||
url="https://example.com?foo=bar", | ||
method="GET", | ||
headers=[], | ||
body=b"", | ||
), | ||
method, | ||
kwargs, | ||
expected, | ||
) | ||
for method, kwargs, expected in ( | ||
( | ||
"poet", | ||
{}, | ||
web_poet.HttpRequest( | ||
url=web_poet.RequestUrl("https://example.com?foo=bar"), | ||
method="GET", | ||
headers=web_poet.HttpRequestHeaders(), | ||
body=web_poet.HttpRequestBody(b""), | ||
), | ||
), | ||
( | ||
"requests", | ||
{}, | ||
requests.Request("GET", "https://example.com?foo=bar").prepare(), | ||
), | ||
( | ||
"scrapy", | ||
{"callback": fake_scrapy_callback}, | ||
scrapy.Request( | ||
"https://example.com?foo=bar", callback=fake_scrapy_callback | ||
), | ||
), | ||
) | ||
), | ||
# POST | ||
*( | ||
( | ||
Request( | ||
url="https://example.com", | ||
method="POST", | ||
headers=[("Content-Type", "application/x-www-form-urlencoded")], | ||
body=b"foo=bar", | ||
), | ||
method, | ||
kwargs, | ||
expected, | ||
) | ||
for method, kwargs, expected in ( | ||
( | ||
"poet", | ||
{}, | ||
web_poet.HttpRequest( | ||
url=web_poet.RequestUrl("https://example.com"), | ||
method="POST", | ||
headers=web_poet.HttpRequestHeaders( | ||
{"Content-Type": "application/x-www-form-urlencoded"} | ||
), | ||
body=web_poet.HttpRequestBody(b"foo=bar"), | ||
), | ||
), | ||
( | ||
"requests", | ||
{}, | ||
requests.Request( | ||
"POST", | ||
"https://example.com", | ||
headers={"Content-Type": "application/x-www-form-urlencoded"}, | ||
data=b"foo=bar", | ||
).prepare(), | ||
), | ||
( | ||
"scrapy", | ||
{"callback": fake_scrapy_callback}, | ||
scrapy.Request( | ||
"https://example.com", | ||
method="POST", | ||
headers={"Content-Type": "application/x-www-form-urlencoded"}, | ||
body=b"foo=bar", | ||
callback=fake_scrapy_callback, | ||
), | ||
), | ||
) | ||
), | ||
# kwargs | ||
( | ||
Request( | ||
url="https://example.com", | ||
method="POST", | ||
headers=[("Content-Type", "application/x-www-form-urlencoded")], | ||
body=b"foo=bar", | ||
), | ||
"requests", | ||
{"params": {"foo": "bar"}}, | ||
requests.Request( | ||
"POST", | ||
"https://example.com?foo=bar", | ||
headers={"Content-Type": "application/x-www-form-urlencoded"}, | ||
data=b"foo=bar", | ||
).prepare(), | ||
), | ||
( | ||
Request( | ||
url="https://example.com", | ||
method="POST", | ||
headers=[("Content-Type", "application/x-www-form-urlencoded")], | ||
body=b"foo=bar", | ||
), | ||
"scrapy", | ||
{"callback": fake_scrapy_callback, "meta": {"foo": "bar"}}, | ||
scrapy.Request( | ||
"https://example.com", | ||
method="POST", | ||
headers={"Content-Type": "application/x-www-form-urlencoded"}, | ||
body=b"foo=bar", | ||
callback=fake_scrapy_callback, | ||
meta={"foo": "bar"}, | ||
), | ||
), | ||
), | ||
) | ||
def test_conversion(request_data, method, kwargs, expected): | ||
actual = getattr(request_data, f"to_{method}")(**kwargs) | ||
if method == "poet": | ||
for field in ("method", "headers", "body"): | ||
assert getattr(actual, field) == getattr(expected, field) | ||
# RequestUrl(…) != RequestUrl(…) | ||
assert str(actual.url) == str(expected.url) | ||
elif method == "requests": | ||
# Request(…).prepare() != Request(…).prepare() | ||
for field in ("url", "method", "headers", "body"): | ||
assert getattr(actual, field) == getattr(expected, field) | ||
else: | ||
assert method == "scrapy" | ||
# Request(…) != Request(…) | ||
for field in ("url", "method", "headers", "body", "callback", "meta"): | ||
assert getattr(actual, field) == getattr(expected, field) |
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