Skip to content

Commit 72f5df6

Browse files
committed
test: encode JSON responses in base64 for secure request handling
1 parent 06641ec commit 72f5df6

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

tests/test_browser/test_browser_base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import base64
12
from unittest.mock import ANY, AsyncMock, MagicMock, patch
23

34
import pytest
@@ -701,7 +702,8 @@ async def test_fulfill_request_with_all_params(mock_browser):
701702
request_id = 'test_request_123'
702703
response_code = 200
703704
response_headers = [{'name': 'Content-Type', 'value': 'application/json'}]
704-
body = '{"status": "success", "data": "test"}'
705+
json_response = '{"status": "success", "data": "test"}'
706+
body = base64.b64encode(json_response.encode('utf-8')).decode('utf-8')
705707
response_phrase = 'OK'
706708

707709
await mock_browser.fulfill_request(

tests/test_browser/test_browser_tab.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import base64
12
import pytest
23
import pytest_asyncio
34
import uuid
@@ -1358,7 +1359,8 @@ async def test_fulfill_request_with_all_params(self, tab):
13581359
request_id = 'test_request_complete'
13591360
response_code = 200
13601361
response_headers = [{'name': 'Content-Type', 'value': 'application/json'}]
1361-
body = '{"status": "success", "data": "test"}'
1362+
json_response = '{"status": "success", "data": "test"}'
1363+
body = base64.b64encode(json_response.encode('utf-8')).decode('utf-8')
13621364
response_phrase = 'OK'
13631365

13641366
await tab.fulfill_request(
@@ -1390,7 +1392,8 @@ async def test_fulfill_request_with_different_status_code(self, tab):
13901392
request_id = 'test_request_404'
13911393
response_code = 404
13921394
response_headers = [{'name': 'Content-Type', 'value': 'text/html'}]
1393-
response_body = {'error': 'Not Found'}
1395+
html_response = '<html><body><h1>404 - Not Found</h1></body></html>'
1396+
response_body = base64.b64encode(html_response.encode('utf-8')).decode('utf-8')
13941397

13951398
await tab.fulfill_request(
13961399
request_id, response_code, response_headers, response_body
@@ -1411,7 +1414,8 @@ async def test_fulfill_request_empty_headers(self, tab):
14111414
request_id = 'test_request_empty_headers'
14121415
response_code = 200
14131416
response_headers = []
1414-
response_body = {'message': 'success'}
1417+
json_response = '{"message": "success"}'
1418+
response_body = base64.b64encode(json_response.encode('utf-8')).decode('utf-8')
14151419

14161420
await tab.fulfill_request(
14171421
request_id, response_code, response_headers, response_body
@@ -1423,6 +1427,7 @@ async def test_fulfill_request_empty_headers(self, tab):
14231427
call_args = tab._connection_handler.execute_command.call_args_list[-1]
14241428
command = call_args[0][0]
14251429
assert command['params']['responseHeaders'] == []
1430+
assert command['params']['body'] == response_body
14261431

14271432

14281433
class TestTabEdgeCases:

0 commit comments

Comments
 (0)