Skip to content

Commit 06641ec

Browse files
committed
test: add tests for continue_request and fulfill_request methods
1 parent 447dd51 commit 06641ec

File tree

2 files changed

+169
-16
lines changed

2 files changed

+169
-16
lines changed

tests/test_browser/test_browser_base.py

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919
from pydoll.protocol.fetch.events import FetchEvent
2020
from pydoll.connection.connection_handler import ConnectionHandler
21-
from pydoll.constants import DownloadBehavior, PermissionType, NetworkErrorReason
21+
from pydoll.constants import DownloadBehavior, PermissionType, NetworkErrorReason, RequestMethod
2222

2323

2424
class ConcreteBrowser(Browser):
@@ -612,7 +612,57 @@ async def test_disable_runtime_events(mock_browser):
612612
)
613613

614614

615-
# Tests for fail_request and fulfill_request
615+
# Tests for continue_request, fail_request and fulfill_request
616+
@pytest.mark.asyncio
617+
async def test_continue_request(mock_browser):
618+
"""Test continue_request with minimal parameters."""
619+
request_id = 'test_request_123'
620+
621+
await mock_browser.continue_request(request_id)
622+
623+
mock_browser._connection_handler.execute_command.assert_called_with(
624+
FetchCommands.continue_request(
625+
request_id=request_id,
626+
url=None,
627+
method=None,
628+
post_data=None,
629+
headers=None,
630+
intercept_response=None,
631+
), timeout=10
632+
)
633+
634+
635+
@pytest.mark.asyncio
636+
async def test_continue_request_with_all_params(mock_browser):
637+
"""Test continue_request with all parameters."""
638+
request_id = 'test_request_123'
639+
url = 'https://modified-example.com'
640+
method = RequestMethod.POST
641+
post_data = 'modified_data=test'
642+
headers = [{'name': 'Authorization', 'value': 'Bearer token123'}]
643+
intercept_response = True
644+
645+
await mock_browser.continue_request(
646+
request_id=request_id,
647+
url=url,
648+
method=method,
649+
post_data=post_data,
650+
headers=headers,
651+
intercept_response=intercept_response,
652+
)
653+
654+
mock_browser._connection_handler.execute_command.assert_called_with(
655+
FetchCommands.continue_request(
656+
request_id=request_id,
657+
url=url,
658+
method=method,
659+
post_data=post_data,
660+
headers=headers,
661+
intercept_response=intercept_response,
662+
), timeout=10
663+
)
664+
665+
616666
@pytest.mark.asyncio
617667
async def test_fail_request(mock_browser):
618668
"""Test fail_request."""
@@ -628,19 +678,47 @@ async def test_fail_request(mock_browser):
628678

629679
@pytest.mark.asyncio
630680
async def test_fulfill_request(mock_browser):
631-
"""Test fulfill_request."""
681+
"""Test fulfill_request with minimal parameters."""
682+
request_id = 'test_request_123'
683+
response_code = 200
684+
685+
await mock_browser.fulfill_request(request_id, response_code)
686+
687+
mock_browser._connection_handler.execute_command.assert_called_with(
688+
FetchCommands.fulfill_request(
689+
request_id=request_id,
690+
response_code=response_code,
691+
response_headers=None,
692+
body=None,
693+
response_phrase=None,
694+
), timeout=10
695+
)
696+
697+
698+
@pytest.mark.asyncio
699+
async def test_fulfill_request_with_all_params(mock_browser):
700+
"""Test fulfill_request with all parameters."""
632701
request_id = 'test_request_123'
633702
response_code = 200
634703
response_headers = [{'name': 'Content-Type', 'value': 'application/json'}]
635-
response_body = {'status': 'success', 'data': 'test'}
704+
body = '{"status": "success", "data": "test"}'
705+
response_phrase = 'OK'
636706

637707
await mock_browser.fulfill_request(
638-
request_id, response_code, response_headers, response_body
708+
request_id=request_id,
709+
response_code=response_code,
710+
response_headers=response_headers,
711+
body=body,
712+
response_phrase=response_phrase,
639713
)
640714

641715
mock_browser._connection_handler.execute_command.assert_called_with(
642716
FetchCommands.fulfill_request(
643-
request_id, response_code, response_headers, response_body
717+
request_id=request_id,
718+
response_code=response_code,
719+
response_headers=response_headers,
720+
body=body,
721+
response_phrase=response_phrase,
644722
), timeout=10
645723
)
646724

tests/test_browser/test_browser_tab.py

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from unittest.mock import AsyncMock, MagicMock, patch, ANY
55
from pathlib import Path
66

7-
from pydoll.constants import By, RequestStage, ResourceType
7+
from pydoll.constants import By, RequestStage, ResourceType, RequestMethod
88
from pydoll.browser.tab import Tab
99
from pydoll.exceptions import (
1010
NoDialogPresent,
@@ -1214,7 +1214,7 @@ class TestTabRequestManagement:
12141214

12151215
@pytest.mark.asyncio
12161216
async def test_continue_request(self, tab):
1217-
"""Test continue_request method."""
1217+
"""Test continue_request method with minimal parameters."""
12181218
request_id = 'test_request_123'
12191219

12201220
await tab.continue_request(request_id)
@@ -1229,6 +1229,13 @@ async def test_continue_request(self, tab):
12291229
# Verify it's a FetchCommands.continue_request command
12301230
assert command['method'] == 'Fetch.continueRequest'
12311231
assert command['params']['requestId'] == request_id
1232+
# Verify optional parameters are None/not set
1233+
params = command['params']
1234+
assert params.get('url') is None
1235+
assert params.get('method') is None
1236+
assert params.get('postData') is None
1237+
assert params.get('headers') is None
1238+
assert params.get('interceptResponse') is None
12321239

12331240
@pytest.mark.asyncio
12341241
async def test_fail_request(self, tab):
@@ -1254,15 +1261,11 @@ async def test_fail_request(self, tab):
12541261

12551262
@pytest.mark.asyncio
12561263
async def test_fulfill_request(self, tab):
1257-
"""Test fulfill_request method."""
1264+
"""Test fulfill_request method with minimal parameters."""
12581265
request_id = 'test_request_789'
12591266
response_code = 200
1260-
response_headers = [{'name': 'Content-Type', 'value': 'application/json'}]
1261-
response_body = {'status': 'success', 'data': 'test'}
12621267

1263-
await tab.fulfill_request(
1264-
request_id, response_code, response_headers, response_body
1265-
)
1268+
await tab.fulfill_request(request_id, response_code)
12661269

12671270
# Verify the command was executed with correct parameters
12681271
assert_mock_called_at_least_once(tab._connection_handler)
@@ -1275,8 +1278,48 @@ async def test_fulfill_request(self, tab):
12751278
assert command['method'] == 'Fetch.fulfillRequest'
12761279
assert command['params']['requestId'] == request_id
12771280
assert command['params']['responseCode'] == response_code
1278-
assert command['params']['responseHeaders'] == response_headers
1279-
assert command['params']['body'] == response_body
1281+
# Verify optional parameters are None/not set
1282+
params = command['params']
1283+
assert params.get('responseHeaders') is None
1284+
assert params.get('body') is None
1285+
assert params.get('responsePhrase') is None
1286+
1287+
@pytest.mark.asyncio
1288+
async def test_continue_request_with_all_params(self, tab):
1289+
"""Test continue_request with all parameters."""
1290+
from pydoll.constants import RequestMethod
1291+
1292+
request_id = 'test_request_456'
1293+
url = 'https://modified-example.com'
1294+
method = RequestMethod.POST
1295+
post_data = 'modified_data=test'
1296+
headers = [{'name': 'Authorization', 'value': 'Bearer token123'}]
1297+
intercept_response = True
1298+
1299+
await tab.continue_request(
1300+
request_id=request_id,
1301+
url=url,
1302+
method=method,
1303+
post_data=post_data,
1304+
headers=headers,
1305+
intercept_response=intercept_response,
1306+
)
1307+
1308+
# Verify the command was executed with correct parameters
1309+
assert_mock_called_at_least_once(tab._connection_handler)
1310+
1311+
# Get the call arguments to verify the command
1312+
call_args = tab._connection_handler.execute_command.call_args_list[-1]
1313+
command = call_args[0][0] # First argument is the command
1314+
1315+
# Verify all parameters
1316+
params = command['params']
1317+
assert params['requestId'] == request_id
1318+
assert params['url'] == url
1319+
assert params['method'] == method
1320+
assert params['postData'] == post_data
1321+
assert params['headers'] == headers
1322+
assert params['interceptResponse'] == intercept_response
12801323

12811324
@pytest.mark.asyncio
12821325
async def test_continue_request_with_different_id(self, tab):
@@ -1309,6 +1352,38 @@ async def test_fail_request_with_different_error(self, tab):
13091352
command = call_args[0][0]
13101353
assert command['params']['errorReason'] == error_reason
13111354

1355+
@pytest.mark.asyncio
1356+
async def test_fulfill_request_with_all_params(self, tab):
1357+
"""Test fulfill_request with all parameters."""
1358+
request_id = 'test_request_complete'
1359+
response_code = 200
1360+
response_headers = [{'name': 'Content-Type', 'value': 'application/json'}]
1361+
body = '{"status": "success", "data": "test"}'
1362+
response_phrase = 'OK'
1363+
1364+
await tab.fulfill_request(
1365+
request_id=request_id,
1366+
response_code=response_code,
1367+
response_headers=response_headers,
1368+
body=body,
1369+
response_phrase=response_phrase,
1370+
)
1371+
1372+
# Verify the command was executed with correct parameters
1373+
assert_mock_called_at_least_once(tab._connection_handler)
1374+
1375+
# Get the call arguments to verify the command
1376+
call_args = tab._connection_handler.execute_command.call_args_list[-1]
1377+
command = call_args[0][0] # First argument is the command
1378+
1379+
# Verify all parameters
1380+
params = command['params']
1381+
assert params['requestId'] == request_id
1382+
assert params['responseCode'] == response_code
1383+
assert params['responseHeaders'] == response_headers
1384+
assert params['body'] == body
1385+
assert params['responsePhrase'] == response_phrase
1386+
13121387
@pytest.mark.asyncio
13131388
async def test_fulfill_request_with_different_status_code(self, tab):
13141389
"""Test fulfill_request with different status code."""

0 commit comments

Comments
 (0)