11import json
2- from collections .abc import Generator
2+ from collections .abc import Iterator
33from copy import deepcopy
44from pathlib import Path
5- from typing import Any
5+ from typing import Any , cast
66
7- import httpx
87import pytest
98import respx
10- from httpx import Response
9+ from httpx import Request , Response
10+ from respx import MockRouter
1111
1212WORKING_DIR = Path (__file__ ).parent
1313
1414
1515def load_fixture (name : str ) -> dict [str , Any ]:
1616 with open (WORKING_DIR / "fixtures" / f"{ name } .json" ) as f :
17- return json .load (f ) # type: ignore[no-any-return]
17+ return cast ( dict , json .load (f ))
1818
1919
2020@pytest .fixture
21- def mocked_api () -> Generator [ respx . MockRouter , None , None ]:
21+ def api () -> Iterator [ MockRouter ]:
2222 landing_page_data = load_fixture ("landing_page" )
2323 products = load_fixture ("products" )
2424
25- with respx .mock (base_url = "https ://stapi.example.com " , assert_all_called = False ) as respx_mock :
25+ with respx .mock (base_url = "http ://stapi.test " , assert_all_called = False ) as respx_mock :
2626 landing_page = respx_mock .get ("/" )
2727 landing_page .return_value = Response (200 , json = landing_page_data )
2828
2929 conformance_route = respx_mock .get ("/conformance" )
3030 conformance_route .return_value = Response (200 , json = {"conformsTo" : landing_page_data ["conformsTo" ]})
3131
32- # products_route.return_value = Response(200, json=products)
33-
34- def mock_products_response (request : httpx .Request ) -> httpx .Response :
32+ def mock_products_response (request : Request ) -> Response :
3533 products_limited = deepcopy (products )
3634 limit = request .url .params .get ("limit" )
3735 page = int (request .url .params .get ("page" , 1 ))
@@ -43,7 +41,7 @@ def mock_products_response(request: httpx.Request) -> httpx.Response:
4341 if has_next_page :
4442 products_limited ["links" ].append (
4543 {
46- "href" : "https ://stapi.example.com /products?limit=1&page=2" ,
44+ "href" : "http ://stapi.test /products?limit=1&page=2" ,
4745 "method" : "GET" ,
4846 "rel" : "next" ,
4947 }
0 commit comments