|
| 1 | +import json |
| 2 | + |
| 3 | +import aiohttp |
| 4 | +import httpx |
| 5 | +import pytest |
| 6 | +from asgiref.sync import async_to_sync |
| 7 | + |
| 8 | +from mocket.mocket import Mocket, mocketize |
| 9 | +from mocket.mockhttp import Entry |
| 10 | +from mocket.plugins.httpretty import httprettified, httpretty |
| 11 | + |
| 12 | +timeout = aiohttp.ClientTimeout(total=3) |
| 13 | + |
| 14 | + |
| 15 | +@mocketize |
| 16 | +@pytest.mark.parametrize("url", ["http://httpbin.org/ip", "https://httpbin.org/ip"]) |
| 17 | +def test_body(url): |
| 18 | + body = "asd" * 100 |
| 19 | + Entry.single_register(Entry.GET, url, body=body, status=404) |
| 20 | + Entry.single_register(Entry.POST, url, body=body * 2, status=201) |
| 21 | + |
| 22 | + @async_to_sync |
| 23 | + async def perform_async_transactions(): |
| 24 | + async with httpx.AsyncClient() as client: |
| 25 | + response = await client.get(url) |
| 26 | + assert response.status_code == 404 |
| 27 | + assert response.text == body |
| 28 | + |
| 29 | + response = await client.post(url, data=body * 6) |
| 30 | + assert response.status_code == 201 |
| 31 | + assert response.text == body * 2 |
| 32 | + |
| 33 | + assert Mocket.last_request().method == "POST" |
| 34 | + assert Mocket.last_request().body == body * 6 |
| 35 | + |
| 36 | + perform_async_transactions() |
| 37 | + assert len(Mocket.request_list()) == 2 |
| 38 | + |
| 39 | + |
| 40 | +@httprettified |
| 41 | +def test_httprettish_session(): |
| 42 | + url = "https://httpbin.org/ip" |
| 43 | + |
| 44 | + expected_response = {"origin": "127.0.0.2"} |
| 45 | + |
| 46 | + httpretty.register_uri( |
| 47 | + httpretty.GET, |
| 48 | + url, |
| 49 | + body=json.dumps(expected_response), |
| 50 | + ) |
| 51 | + |
| 52 | + @async_to_sync |
| 53 | + async def perform_async_transactions(): |
| 54 | + async with httpx.AsyncClient() as client: |
| 55 | + response = await client.get(url) |
| 56 | + assert response.status_code == 200 |
| 57 | + assert response.json() == expected_response |
| 58 | + |
| 59 | + perform_async_transactions() |
| 60 | + assert len(httpretty.latest_requests) == 1 |
0 commit comments