@@ -1348,6 +1348,85 @@ def parse(self, response):
13481348 session_config_registry .__init__ () # type: ignore[misc]
13491349
13501350
1351+ @ensureDeferred
1352+ async def test_session_config_check_meta (mockserver ):
1353+ """When initializing a session, known zyte_api_session-prefixed params
1354+ should be included in the session initialization request, so that they can
1355+ be used from check methods validating those requests.
1356+
1357+ For example, when validating a location, access to
1358+ zyte_api_session_location may be necessary.
1359+ """
1360+ pytest .importorskip ("web_poet" )
1361+
1362+ params = {
1363+ "actions" : [
1364+ {
1365+ "action" : "setLocation" ,
1366+ "address" : {"postalCode" : "10001" },
1367+ }
1368+ ]
1369+ }
1370+
1371+ @session_config (["example.com" ])
1372+ class CustomSessionConfig (SessionConfig ):
1373+
1374+ def check (self , response , request ):
1375+ return (
1376+ bool (self .location (request ))
1377+ and response .meta ["zyte_api_session_params" ] == params
1378+ and (
1379+ (
1380+ response .meta .get ("_is_session_init_request" , False )
1381+ and "zyte_api_session_foo" not in response .meta
1382+ )
1383+ or response .meta ["zyte_api_session_foo" ] == "bar"
1384+ )
1385+ )
1386+
1387+ settings = {
1388+ "RETRY_TIMES" : 0 ,
1389+ "ZYTE_API_URL" : mockserver .urljoin ("/" ),
1390+ "ZYTE_API_SESSION_ENABLED" : True ,
1391+ "ZYTE_API_SESSION_MAX_BAD_INITS" : 1 ,
1392+ }
1393+
1394+ class TestSpider (Spider ):
1395+ name = "test"
1396+ start_urls = ["https://example.com" ]
1397+
1398+ def start_requests (self ):
1399+ for url in self .start_urls :
1400+ yield Request (
1401+ url ,
1402+ meta = {
1403+ "zyte_api_automap" : params ,
1404+ "zyte_api_session_params" : params ,
1405+ "zyte_api_session_location" : {"postalCode" : "10001" },
1406+ "zyte_api_session_foo" : "bar" ,
1407+ },
1408+ )
1409+
1410+ def parse (self , response ):
1411+ pass
1412+
1413+ crawler = await get_crawler (settings , spider_cls = TestSpider , setup_engine = False )
1414+ await crawler .crawl ()
1415+
1416+ session_stats = {
1417+ k : v
1418+ for k , v in crawler .stats .get_stats ().items ()
1419+ if k .startswith ("scrapy-zyte-api/sessions" )
1420+ }
1421+ assert session_stats == {
1422+ "scrapy-zyte-api/sessions/pools/example.com/init/check-passed" : 1 ,
1423+ "scrapy-zyte-api/sessions/pools/example.com/use/check-passed" : 1 ,
1424+ }
1425+
1426+ # Clean up the session config registry.
1427+ session_config_registry .__init__ () # type: ignore[misc]
1428+
1429+
13511430@ensureDeferred
13521431async def test_session_config_param_error (mockserver ):
13531432 pytest .importorskip ("web_poet" )
0 commit comments