|
1 | 1 | import os |
2 | 2 | from asyncio import iscoroutine |
3 | 3 | from typing import Any, Dict |
| 4 | +from unittest import mock |
4 | 5 |
|
5 | 6 | import pytest |
6 | 7 | from _pytest.logging import LogCaptureFixture # NOQA |
|
23 | 24 |
|
24 | 25 |
|
25 | 26 | class TestAPI: |
26 | | - |
27 | 27 | @staticmethod |
28 | | - async def produce_request_response(meta): |
| 28 | + async def produce_request_response(meta, custom_settings=None): |
29 | 29 | with MockServer() as server: |
30 | | - async with make_handler({}, server.urljoin("/")) as handler: |
| 30 | + async with make_handler(custom_settings, server.urljoin("/")) as handler: |
31 | 31 | req = Request( |
32 | 32 | "http://example.com", |
33 | 33 | method="POST", |
34 | 34 | meta=meta, |
35 | 35 | ) |
36 | | - coro = handler._download_request(req, Spider("test")) |
| 36 | + coro = handler._download_request(req, None) |
37 | 37 | assert iscoroutine(coro) |
38 | 38 | assert not isinstance(coro, Deferred) |
39 | 39 | resp = await coro # type: ignore |
@@ -101,6 +101,53 @@ async def test_http_response_headers_request(self, meta: Dict[str, Dict[str, Any |
101 | 101 | assert resp.body == b"<html></html>" |
102 | 102 | assert resp.headers == {b"Test_Header": [b"test_value"]} |
103 | 103 |
|
| 104 | + @pytest.mark.parametrize( |
| 105 | + "meta,custom_settings,expected", |
| 106 | + [ |
| 107 | + ({}, {}, {}), |
| 108 | + ({"zyte_api": {}}, {}, {}), |
| 109 | + ( |
| 110 | + {}, |
| 111 | + {"ZYTE_API_DEFAULT_PARAMS": {"browserHtml": True, "geolocation": "CA"}}, |
| 112 | + {"browserHtml": True, "geolocation": "CA"}, |
| 113 | + ), |
| 114 | + ( |
| 115 | + {"zyte_api": {}}, |
| 116 | + {"ZYTE_API_DEFAULT_PARAMS": {"browserHtml": True, "geolocation": "CA"}}, |
| 117 | + {"browserHtml": True, "geolocation": "CA"}, |
| 118 | + ), |
| 119 | + ( |
| 120 | + {"zyte_api": {"javascript": True, "geolocation": "US"}}, |
| 121 | + {"ZYTE_API_DEFAULT_PARAMS": {"browserHtml": True, "geolocation": "CA"}}, |
| 122 | + {"browserHtml": True, "geolocation": "US", "javascript": True}, |
| 123 | + ), |
| 124 | + ], |
| 125 | + ) |
| 126 | + @mock.patch("tests.AsyncClient") |
| 127 | + @pytest.mark.asyncio |
| 128 | + async def test_empty_zyte_api_request_meta( |
| 129 | + self, |
| 130 | + mock_client, |
| 131 | + meta: Dict[str, Dict[str, Any]], |
| 132 | + custom_settings: Dict[str, str], |
| 133 | + expected: Dict[str, str], |
| 134 | + ): |
| 135 | + try: |
| 136 | + # This would always error out since the mocked client doesn't |
| 137 | + # return the expected API response. |
| 138 | + await self.produce_request_response(meta, custom_settings=custom_settings) |
| 139 | + except: |
| 140 | + pass |
| 141 | + |
| 142 | + request_call = [c for c in mock_client.mock_calls if "request_raw(" in str(c)] |
| 143 | + if not request_call: |
| 144 | + pytest.fail("The client's request_raw() method was not called.") |
| 145 | + |
| 146 | + args_used = request_call[0].args[0] |
| 147 | + args_used.pop("url") |
| 148 | + |
| 149 | + assert args_used == expected |
| 150 | + |
104 | 151 | @pytest.mark.parametrize( |
105 | 152 | "meta, api_relevant", |
106 | 153 | [ |
|
0 commit comments