66from typing import Any
77
88import pytest
9+ from aiohttp import web
10+ from aiohttp .test_utils import make_mocked_request
11+ from servicelib .aiohttp import status
912from servicelib .rest_constants import X_PRODUCT_NAME_HEADER
1013from simcore_postgres_database .models .products import LOGIN_SETTINGS_DEFAULT
1114from simcore_postgres_database .webserver_models import products
@@ -49,21 +52,16 @@ def mock_postgres_product_table():
4952
5053
5154@pytest .fixture
52- def mock_app (mock_postgres_product_table : dict [str , Any ]):
53- class MockApp (dict ):
54- def __init__ (self ):
55- super ().__init__ ()
56- self .middlewares = []
57-
58- mock_app = MockApp ()
55+ def mock_app (mock_postgres_product_table : dict [str , Any ]) -> web .Application :
56+ app = web .Application ()
5957
6058 app_products : dict [str , Product ] = {
6159 entry ["name" ]: Product (** entry ) for entry in mock_postgres_product_table
6260 }
6361 default_product_name = next (iter (app_products .keys ()))
64- _set_app_state (mock_app , app_products , default_product_name )
62+ _set_app_state (app , app_products , default_product_name )
6563
66- return mock_app
64+ return app
6765
6866
6967@pytest .mark .parametrize (
@@ -82,44 +80,36 @@ def __init__(self):
8280 ],
8381)
8482async def test_middleware_product_discovery (
85- request_url , product_from_client , expected_product : str , mock_app
83+ request_url : str ,
84+ product_from_client : str | None ,
85+ expected_product : str ,
86+ mock_app : web .Application ,
8687):
8788 """
8889 A client's request reaches the middleware with
8990 - an url (request_url),
9091 - a product name in the header from client (product_from_client)
9192 """
92- requested_url = URL (request_url )
93-
94- # mocks
95- class MockRequest (dict ):
96- @property
97- def headers (self ):
98- return (
99- {X_PRODUCT_NAME_HEADER : product_from_client }
100- if product_from_client
101- else {}
102- )
103-
104- @property
105- def app (self ):
106- return mock_app
107-
108- @property
109- def path (self ):
110- return requested_url .path
111-
112- @property
113- def host (self ):
114- return requested_url .host
93+ url = URL (request_url )
94+ headers = {
95+ "Host" : url .host ,
96+ }
97+ if product_from_client :
98+ headers .update ({X_PRODUCT_NAME_HEADER : product_from_client })
11599
116- mock_request = MockRequest ()
100+ mock_request = make_mocked_request (
101+ "GET" ,
102+ url .path ,
103+ headers = headers ,
104+ app = mock_app ,
105+ )
117106
118- async def mock_handler ( request ):
119- return "OK"
107+ async def _mock_handler ( _request : web . Request ):
108+ return web . Response ( text = "OK" )
120109
121- # under test ---------
122- response = await discover_product_middleware (mock_request , mock_handler )
110+ # run middleware
111+ response = await discover_product_middleware (mock_request , _mock_handler )
123112
124113 # checks
125114 assert get_product_name (mock_request ) == expected_product
115+ assert response .status == status .HTTP_200_OK
0 commit comments