-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for additional request components
- Loading branch information
Showing
5 changed files
with
87 additions
and
6 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
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,71 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
from web_poet.page_inputs import ResponseData | ||
from web_poet.requests import ( | ||
GenericRequest, | ||
perform_request, | ||
HttpClient, | ||
RequestBackendError, | ||
request_backend_var, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def async_mock(): | ||
"""workaround since python 3.7 doesn't ship with asyncmock.""" | ||
|
||
async def async_test(req): | ||
return ResponseData(req.url, req.body) | ||
|
||
mock.MagicMock.__await__ = lambda x: async_test().__await__() | ||
|
||
return async_test | ||
|
||
|
||
def test_generic_request(): | ||
|
||
req = GenericRequest("url") | ||
assert req.url == "url" | ||
assert req.method == "GET" | ||
assert req.headers is None | ||
assert req.body is None | ||
|
||
req = GenericRequest( | ||
"url", method="POST", headers={"User-Agent": "test agent"}, body=b"body" | ||
) | ||
assert req.method == "POST" | ||
assert req.headers == {"User-Agent": "test agent"} | ||
assert req.body == b"body" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_perform_request(async_mock): | ||
|
||
req = GenericRequest("url") | ||
|
||
with pytest.raises(RequestBackendError): | ||
await perform_request(req) | ||
|
||
request_backend_var.set(async_mock) | ||
response = await perform_request(req) | ||
|
||
# The async downloader implementation should return the ResponseData | ||
assert response.url == req.url | ||
assert type(response) == ResponseData | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_http_client(async_mock): | ||
client = HttpClient(async_mock) | ||
assert client.request_downloader == async_mock | ||
|
||
req_1 = GenericRequest("url-1") | ||
req_2 = GenericRequest("url-2") | ||
|
||
# It should be able to accept arbitrary number of requests | ||
client.request(req_1) | ||
responses = await client.request(req_1, req_2) | ||
|
||
assert responses[0].url == req_1.url | ||
assert responses[1].url == req_2.url |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from .pages import WebPage, ItemPage, ItemWebPage, Injectable | ||
from .page_inputs import ResponseData | ||
from .requests import perform_request, request_backend_var, GenericRequest | ||
from .requests import request_backend_var, GenericRequest, HttpClient |
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