11import pytest
2+ import pytest_asyncio
23from tests .bidi .network import (
4+ BEFORE_REQUEST_SENT_EVENT ,
35 PAGE_EMPTY_HTML ,
46 RESPONSE_COMPLETED_EVENT ,
57)
68from webdriver .bidi import error
79
10+ MAX_TOTAL_SIZE = 1000
11+
12+ # Prepare various data sizes to test against a max total size of 1000
13+ big_data = MAX_TOTAL_SIZE - 100
14+ half_size_data = int (MAX_TOTAL_SIZE / 2 )
15+ max_size_data = MAX_TOTAL_SIZE
16+ small_data = int (MAX_TOTAL_SIZE / 100 )
17+ too_big_data = MAX_TOTAL_SIZE + 100
18+ too_big_one_byte_data = MAX_TOTAL_SIZE + 1
19+
20+
21+ @pytest_asyncio .fixture
22+ async def send_request (wait_for_event , inline , fetch , wait_for_future_safe ):
23+ # This flag is dedicated to support the "request or response" mode.
24+ mode_flip = False
25+
26+ async def _send_request (size , mode ):
27+ nonlocal mode_flip
28+
29+ # In request or response mode, alternate between request and response
30+ # for every request.
31+ if mode == "request or response" :
32+ mode_flip = not mode_flip
33+ data_type = "request" if mode_flip else "response"
34+ else :
35+ data_type = mode
36+
37+ data = "" .join ("A" for i in range (size ))
38+ if data_type == "request" :
39+ post_data = data
40+ response_data = ""
41+ elif data_type == "response" :
42+ response_data = data
43+ post_data = None
44+
45+ on_response_completed = wait_for_event (RESPONSE_COMPLETED_EVENT )
46+ # Note: We use the "js" doctype here to avoid any boilerplate in the inline
47+ # response, which would inflate the sizes unexpectedly.
48+ await fetch (url = inline (response_data , doctype = "js" ), post_data = post_data )
49+ event = await wait_for_future_safe (on_response_completed )
50+
51+ # Return both the request id and the actual data_type where the data was
52+ # set.
53+ return {"request" : event ["request" ]["request" ], "data_type" : data_type }
54+
55+ return _send_request
56+
857
958@pytest .mark .capabilities (
1059 {
1160 "moz:firefoxOptions" : {
1261 "prefs" : {
13- "remote.network.maxTotalDataSize" : 1000 ,
62+ "remote.network.maxTotalDataSize" : MAX_TOTAL_SIZE ,
1463 },
1564 },
1665 }
1766)
67+ @pytest .mark .parametrize (
68+ "mode" ,
69+ [
70+ "request" ,
71+ "response" ,
72+ "request or response" ,
73+ ],
74+ )
1875@pytest .mark .asyncio
1976async def test_max_total_data_size (
2077 bidi_session ,
21- url ,
22- inline ,
2378 setup_network_test ,
2479 top_context ,
25- wait_for_event ,
26- wait_for_future_safe ,
2780 add_data_collector ,
28- fetch ,
81+ send_request ,
82+ mode ,
2983):
3084 await setup_network_test (
3185 events = [
86+ BEFORE_REQUEST_SENT_EVENT ,
3287 RESPONSE_COMPLETED_EVENT ,
3388 ]
3489 )
@@ -41,46 +96,27 @@ async def test_max_total_data_size(
4196
4297 # Add a collector, with the same max size as the total size.
4398 await add_data_collector (
44- collector_type = "blob" , data_types = ["response" ], max_encoded_data_size = 1000
99+ collector_type = "blob" ,
100+ data_types = ["request" , "response" ],
101+ max_encoded_data_size = MAX_TOTAL_SIZE ,
45102 )
46103
47- # Build a url with a response size slightly below the maximum.
48- big_response = "" .join ("A" for i in range (900 ))
49- big_url = inline (big_response , doctype = "js" )
50-
51- # Build a url with a small response size.
52- small_response = "" .join ("A" for i in range (10 ))
53- small_url = inline (small_response , doctype = "js" )
54-
55- # Build a url with a response size slightly over the maximum.
56- too_big_response = "" .join ("A" for i in range (1100 ))
57- too_big_url = inline (too_big_response , doctype = "js" )
58-
59- # Note: We use the "js" doctype here to avoid any boilerplate in the inline
60- # response, which would inflate the sizes unexpectedly.
61-
62- async def send_request (url ):
63- on_response_completed = wait_for_event (RESPONSE_COMPLETED_EVENT )
64- await fetch (url , method = "GET" )
65- event = await wait_for_future_safe (on_response_completed )
66- return event ["request" ]["request" ]
67-
68104 # Send a request to store the 900 chars (uncompressed) response.
69- request_1_big = await send_request (big_url )
105+ request_1_big = await send_request (size = big_data , mode = mode )
70106
71107 await assert_request_data_available (request_1_big , bidi_session )
72108
73109 # Send another big request.
74110 # Check a previous request is evicted if more space is needed.
75- request_2_big = await send_request (big_url )
111+ request_2_big = await send_request (size = big_data , mode = mode )
76112
77113 # Expected: 1->evicted, 2->OK
78114 await assert_request_data_unavailable (request_1_big , bidi_session )
79115 await assert_request_data_available (request_2_big , bidi_session )
80116
81117 # Send a small request for a 10 chars response.
82118 # Check eviction only done if more space is required.
83- request_3_small = await send_request (small_url )
119+ request_3_small = await send_request (size = small_data , mode = mode )
84120
85121 # Expected: 2->OK, 3->OK
86122 await assert_request_data_available (request_2_big , bidi_session )
@@ -89,7 +125,7 @@ async def send_request(url):
89125 # Send another big request.
90126 # Check eviction only removes requests as needed (preserves small request if
91127 # enough space is available).
92- request_4_big = await send_request (big_url )
128+ request_4_big = await send_request (size = big_data , mode = mode )
93129
94130 # Expected: 2->evicted, 3->OK, 4->OK
95131 await assert_request_data_unavailable (request_2_big , bidi_session )
@@ -98,7 +134,7 @@ async def send_request(url):
98134
99135 # Send another small request.
100136 # This is a preparatory step for the next check.
101- request_5_small = await send_request (small_url )
137+ request_5_small = await send_request (size = small_data , mode = mode )
102138
103139 # Expected: 3->OK, 4->OK, 5->OK
104140 await assert_request_data_available (request_3_small , bidi_session )
@@ -110,7 +146,7 @@ async def send_request(url):
110146 # evicted because it arrived before the 4th big request (which is
111147 # mandatory to delete to store the new one).
112148 # But the 5th small request should still be available.
113- request_6_big = await send_request (big_url )
149+ request_6_big = await send_request (size = big_data , mode = mode )
114150
115151 # Expected: 3->evicted, 4->evicted, 5->OK, 6->OK
116152 await assert_request_data_unavailable (request_3_small , bidi_session )
@@ -121,7 +157,7 @@ async def send_request(url):
121157 # Send a request which is too big for the collector.
122158 # No other request should be evicted in this case, 5th and 6th requests
123159 # should still be available.
124- request_7_too_big = await send_request (too_big_url )
160+ request_7_too_big = await send_request (size = too_big_data , mode = mode )
125161
126162 # Expected: 5->OK, 6->OK, 7->no such data
127163 await assert_request_data_available (request_5_small , bidi_session )
@@ -130,14 +166,14 @@ async def send_request(url):
130166 # case.
131167 with pytest .raises (error .NoSuchNetworkDataException ):
132168 await bidi_session .network .get_data (
133- request = request_7_too_big ,
134- data_type = "response" ,
169+ request = request_7_too_big [ "request" ] ,
170+ data_type = request_7_too_big [ "data_type" ] ,
135171 )
136172
137173 # Send a request which is too big by just one byte.
138- too_big_one_byte_response = "" . join ( "A" for i in range ( 1001 ))
139- too_big_one_byte_url = inline ( too_big_one_byte_response , doctype = "js" )
140- request_8_too_big_one_byte = await send_request ( too_big_one_byte_url )
174+ request_8_too_big_one_byte = await send_request (
175+ size = too_big_one_byte_data , mode = mode
176+ )
141177
142178 # Expected: 5->OK, 6->OK, 8->no such data
143179 await assert_request_data_available (request_5_small , bidi_session )
@@ -146,25 +182,21 @@ async def send_request(url):
146182 # case.
147183 with pytest .raises (error .NoSuchNetworkDataException ):
148184 await bidi_session .network .get_data (
149- request = request_8_too_big_one_byte ,
150- data_type = "response" ,
185+ request = request_8_too_big_one_byte [ "request" ] ,
186+ data_type = request_8_too_big_one_byte [ "data_type" ] ,
151187 )
152188
153189 # Send a request which is exactly the max size.
154- max_size_response = "" .join ("A" for i in range (1000 ))
155- max_size_url = inline (max_size_response , doctype = "js" )
156- request_9_max_size = await send_request (max_size_url )
190+ request_9_max_size = await send_request (size = max_size_data , mode = mode )
157191
158192 # Expected: 5->evicted, 6->evicted, 9->OK
159193 await assert_request_data_unavailable (request_5_small , bidi_session )
160194 await assert_request_data_unavailable (request_6_big , bidi_session )
161195 await assert_request_data_available (request_9_max_size , bidi_session )
162196
163197 # Send two requests which add up to the max size.
164- half_size_response = "" .join ("A" for i in range (500 ))
165- half_size_url = inline (half_size_response , doctype = "js" )
166- request_10_half_size = await send_request (half_size_url )
167- request_11_half_size = await send_request (half_size_url )
198+ request_10_half_size = await send_request (size = half_size_data , mode = mode )
199+ request_11_half_size = await send_request (size = half_size_data , mode = mode )
168200
169201 # Expected: 9->evicted, 10->OK, 11->OK
170202 await assert_request_data_unavailable (request_9_max_size , bidi_session )
@@ -174,15 +206,15 @@ async def send_request(url):
174206
175207async def assert_request_data_available (request , bidi_session ):
176208 data = await bidi_session .network .get_data (
177- request = request ,
178- data_type = "response" ,
209+ request = request [ "request" ] ,
210+ data_type = request [ "data_type" ] ,
179211 )
180212 assert isinstance (data ["value" ], str )
181213
182214
183215async def assert_request_data_unavailable (request , bidi_session ):
184216 with pytest .raises (error .UnavailableNetworkDataException ):
185217 await bidi_session .network .get_data (
186- request = request ,
187- data_type = "response" ,
218+ request = request [ "request" ] ,
219+ data_type = request [ "data_type" ] ,
188220 )
0 commit comments