|
18 | 18 |
|
19 | 19 | from crawlee import ConcurrencySettings, Glob, service_locator |
20 | 20 | from crawlee._request import Request |
21 | | -from crawlee._types import BasicCrawlingContext, EnqueueLinksKwargs, HttpHeaders |
| 21 | +from crawlee._types import BasicCrawlingContext, EnqueueLinksKwargs, HttpHeaders, HttpMethod |
22 | 22 | from crawlee._utils.robots import RobotsTxtFile |
23 | 23 | from crawlee.configuration import Configuration |
24 | 24 | from crawlee.crawlers import BasicCrawler |
@@ -300,29 +300,36 @@ async def failed_request_handler(context: BasicCrawlingContext, error: Exception |
300 | 300 | await crawler.run(['http://a.com/', 'http://b.com/', 'http://c.com/']) |
301 | 301 |
|
302 | 302 |
|
303 | | -async def test_send_request_works(server_url: URL) -> None: |
| 303 | +@pytest.mark.parametrize( |
| 304 | + ('method', 'path', 'payload'), |
| 305 | + [ |
| 306 | + pytest.param('GET', 'get', None, id='get send_request'), |
| 307 | + pytest.param('POST', 'post', b'Hello, world!', id='post send_request'), |
| 308 | + ], |
| 309 | +) |
| 310 | +async def test_send_request_works(server_url: URL, method: HttpMethod, path: str, payload: None | bytes) -> None: |
304 | 311 | response_data: dict[str, Any] = {} |
305 | 312 |
|
306 | 313 | crawler = BasicCrawler(max_request_retries=3) |
307 | 314 |
|
308 | 315 | @crawler.router.default_handler |
309 | 316 | async def handler(context: BasicCrawlingContext) -> None: |
310 | | - response = await context.send_request(str(server_url)) |
| 317 | + response = await context.send_request(str(server_url / path), method=method, payload=payload) |
311 | 318 |
|
312 | | - response_data['body'] = response.read() |
| 319 | + response_data['body'] = json.loads(response.read()) |
313 | 320 | response_data['headers'] = response.headers |
314 | 321 |
|
315 | 322 | await crawler.run(['http://a.com/', 'http://b.com/', 'http://c.com/']) |
316 | 323 |
|
317 | 324 | response_body = response_data.get('body') |
318 | 325 | assert response_body is not None |
319 | | - assert b'Hello, world!' in response_body |
| 326 | + assert response_body.get('data') == (payload.decode() if payload else None) |
320 | 327 |
|
321 | 328 | response_headers = response_data.get('headers') |
322 | 329 | assert response_headers is not None |
323 | 330 | content_type = response_headers.get('content-type') |
324 | 331 | assert content_type is not None |
325 | | - assert content_type == 'text/html; charset=utf-8' |
| 332 | + assert content_type == 'application/json' |
326 | 333 |
|
327 | 334 |
|
328 | 335 | @dataclass |
|
0 commit comments