120 lines
4.1 KiB
Python
120 lines
4.1 KiB
Python
import json
|
|
import pytest
|
|
|
|
from api_mock_cli.core.har_parser import HARParser, HARParserError, parse_browser_network_export, UnifiedRequest
|
|
|
|
|
|
class TestHARParser:
|
|
def test_parse_valid_har_file(self, sample_har_file):
|
|
parser = HARParser(har_file_path=sample_har_file)
|
|
result = parser.parse()
|
|
|
|
assert result.entry_count == 2
|
|
assert result.skipped_count == 0
|
|
assert len(result.requests) == 2
|
|
assert result.base_url == "https://api.example.com"
|
|
|
|
def test_parse_har_data_directly(self, sample_har_data):
|
|
parser = HARParser(har_data=sample_har_data)
|
|
result = parser.parse()
|
|
|
|
assert result.entry_count == 2
|
|
assert len(result.requests) == 2
|
|
|
|
def test_parse_get_request(self, sample_har_file):
|
|
parser = HARParser(har_file_path=sample_har_file)
|
|
result = parser.parse()
|
|
|
|
get_req = result.requests[0]
|
|
assert get_req.method == "GET"
|
|
assert "users/123" in get_req.url
|
|
assert get_req.status_code == 200
|
|
assert "john@example.com" in get_req.response_body
|
|
|
|
def test_parse_post_request(self, sample_har_file):
|
|
parser = HARParser(har_file_path=sample_har_file)
|
|
result = parser.parse()
|
|
|
|
post_req = result.requests[1]
|
|
assert post_req.method == "POST"
|
|
assert "users" in post_req.url
|
|
assert post_req.status_code == 201
|
|
assert post_req.body is not None
|
|
|
|
def test_extract_auth_headers(self, sample_har_file):
|
|
parser = HARParser(har_file_path=sample_har_file)
|
|
result = parser.parse()
|
|
|
|
get_req = result.requests[0]
|
|
assert "authorization" in get_req.headers
|
|
assert "Bearer test_token" in get_req.headers["authorization"]
|
|
|
|
def test_parse_query_params(self, sample_har_file):
|
|
parser = HARParser(har_file_path=sample_har_file)
|
|
result = parser.parse()
|
|
|
|
get_req = result.requests[0]
|
|
assert "include" in get_req.query_params
|
|
assert get_req.query_params["include"] == ["profile"]
|
|
|
|
def test_invalid_har_format(self):
|
|
parser = HARParser(har_data={"not": "a valid har"})
|
|
with pytest.raises(HARParserError):
|
|
parser.parse()
|
|
|
|
def test_empty_entries(self):
|
|
parser = HARParser(har_data={"log": {"entries": []}})
|
|
with pytest.raises(HARParserError):
|
|
parser.parse()
|
|
|
|
|
|
class TestBrowserNetworkExport:
|
|
def test_parse_network_export_with_log(self, sample_har_data):
|
|
result = parse_browser_network_export(sample_har_data)
|
|
assert result.entry_count == 2
|
|
assert len(result.requests) == 2
|
|
|
|
def test_parse_network_export_entries_format(self):
|
|
data = {
|
|
"entries": [
|
|
{
|
|
"request": {
|
|
"url": "https://api.example.com/items/1",
|
|
"method": "GET",
|
|
"headers": [{"name": "Content-Type", "value": "application/json"}],
|
|
"queryString": [],
|
|
},
|
|
"response": {
|
|
"status": 200,
|
|
"headers": [{"name": "Content-Type", "value": "application/json"}],
|
|
"content": {"mimeType": "application/json", "text": '{"id": 1}'},
|
|
},
|
|
"time": 50,
|
|
}
|
|
]
|
|
}
|
|
result = parse_browser_network_export(data)
|
|
assert len(result.requests) == 1
|
|
assert result.requests[0].url == "https://api.example.com/items/1"
|
|
|
|
|
|
class TestUnifiedRequest:
|
|
def test_unified_request_creation(self):
|
|
req = UnifiedRequest(
|
|
method="GET",
|
|
url="https://api.example.com/test",
|
|
headers={"content-type": "application/json"},
|
|
query_params={},
|
|
body=None,
|
|
content_type="application/json",
|
|
timing=0.5,
|
|
status_code=200,
|
|
response_body='{"result": "ok"}',
|
|
response_headers={"content-type": "application/json"},
|
|
)
|
|
|
|
assert req.method == "GET"
|
|
assert req.url == "https://api.example.com/test"
|
|
assert req.status_code == 200
|
|
assert req.timing == 0.5
|