mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-07 08:26:10 +00:00
fix: add State() to mock requests in http_parsing_utils tests
The _safe_get_request_headers caching uses request.state._cached_headers, which returns a truthy MagicMock on bare MagicMock() objects instead of None, breaking content-type detection for form-data tests.
This commit is contained in:
parent
ecb04187b5
commit
3275fb09cb
1 changed files with 21 additions and 6 deletions
|
|
@ -7,6 +7,7 @@ import orjson
|
|||
import pytest
|
||||
from fastapi import Request
|
||||
from fastapi.testclient import TestClient
|
||||
from starlette.datastructures import State
|
||||
|
||||
sys.path.insert(
|
||||
0, os.path.abspath("../../../..")
|
||||
|
|
@ -36,6 +37,7 @@ async def test_request_body_caching():
|
|||
"""
|
||||
# Create a mock request with a JSON body
|
||||
mock_request = MagicMock()
|
||||
mock_request.state = State()
|
||||
test_data = {"key": "value"}
|
||||
# Use AsyncMock for the body method
|
||||
mock_request.body = AsyncMock(return_value=orjson.dumps(test_data))
|
||||
|
|
@ -69,6 +71,7 @@ async def test_form_data_parsing():
|
|||
"""
|
||||
# Create a mock request with form data
|
||||
mock_request = MagicMock()
|
||||
mock_request.state = State()
|
||||
test_data = {"name": "test_user", "message": "hello world"}
|
||||
|
||||
# Mock the form method to return the test data as an awaitable
|
||||
|
|
@ -104,7 +107,8 @@ async def test_form_data_with_json_metadata():
|
|||
"""
|
||||
# Create a mock request with form data containing JSON metadata
|
||||
mock_request = MagicMock()
|
||||
|
||||
mock_request.state = State()
|
||||
|
||||
# Metadata is sent as a JSON string in form data
|
||||
metadata_json_string = json.dumps({
|
||||
"user_id": "12345",
|
||||
|
|
@ -152,7 +156,8 @@ async def test_form_data_with_invalid_json_metadata():
|
|||
"""
|
||||
# Create a mock request with form data containing invalid JSON metadata
|
||||
mock_request = MagicMock()
|
||||
|
||||
mock_request.state = State()
|
||||
|
||||
test_data = {
|
||||
"model": "whisper-1",
|
||||
"file": "audio.mp3",
|
||||
|
|
@ -178,7 +183,8 @@ async def test_form_data_without_metadata():
|
|||
"""
|
||||
# Create a mock request with form data without metadata
|
||||
mock_request = MagicMock()
|
||||
|
||||
mock_request.state = State()
|
||||
|
||||
test_data = {
|
||||
"model": "whisper-1",
|
||||
"file": "audio.mp3",
|
||||
|
|
@ -208,7 +214,8 @@ async def test_form_data_with_empty_metadata():
|
|||
"""
|
||||
# Create a mock request with form data containing empty metadata
|
||||
mock_request = MagicMock()
|
||||
|
||||
mock_request.state = State()
|
||||
|
||||
test_data = {
|
||||
"model": "whisper-1",
|
||||
"file": "audio.mp3",
|
||||
|
|
@ -240,7 +247,8 @@ async def test_form_data_with_dict_metadata():
|
|||
"""
|
||||
# Create a mock request with form data where metadata is already a dict
|
||||
mock_request = MagicMock()
|
||||
|
||||
mock_request.state = State()
|
||||
|
||||
metadata_dict = {
|
||||
"user_id": "12345",
|
||||
"tags": ["test"]
|
||||
|
|
@ -275,7 +283,8 @@ async def test_form_data_with_none_metadata():
|
|||
"""
|
||||
# Create a mock request with form data where metadata is None
|
||||
mock_request = MagicMock()
|
||||
|
||||
mock_request.state = State()
|
||||
|
||||
test_data = {
|
||||
"model": "whisper-1",
|
||||
"file": "audio.mp3",
|
||||
|
|
@ -303,6 +312,7 @@ async def test_empty_request_body():
|
|||
"""
|
||||
# Create a mock request with an empty body
|
||||
mock_request = MagicMock()
|
||||
mock_request.state = State()
|
||||
mock_request.body = AsyncMock(return_value=b"") # Empty bytes as an awaitable
|
||||
mock_request.headers = {"content-type": "application/json"}
|
||||
mock_request.scope = {}
|
||||
|
|
@ -327,6 +337,7 @@ async def test_circular_reference_handling():
|
|||
"""
|
||||
# Create a mock request with initial data
|
||||
mock_request = MagicMock()
|
||||
mock_request.state = State()
|
||||
initial_body = {
|
||||
"model": "gpt-4",
|
||||
"messages": [{"role": "user", "content": "Hello"}],
|
||||
|
|
@ -366,6 +377,7 @@ async def test_json_parsing_error_handling():
|
|||
"""
|
||||
# Test case 1: Trailing comma error
|
||||
mock_request = MagicMock()
|
||||
mock_request.state = State()
|
||||
invalid_json_with_trailing_comma = b'''{
|
||||
"model": "gpt-4o",
|
||||
"tools": [
|
||||
|
|
@ -394,6 +406,7 @@ async def test_json_parsing_error_handling():
|
|||
|
||||
# Test case 2: Unquoted property name error
|
||||
mock_request2 = MagicMock()
|
||||
mock_request2.state = State()
|
||||
invalid_json_unquoted_property = b'''{
|
||||
"model": "gpt-4o",
|
||||
"tools": [
|
||||
|
|
@ -418,6 +431,7 @@ async def test_json_parsing_error_handling():
|
|||
|
||||
# Test case 3: Valid JSON should work normally
|
||||
mock_request3 = MagicMock()
|
||||
mock_request3.state = State()
|
||||
valid_json = b'''{
|
||||
"model": "gpt-4o",
|
||||
"tools": [
|
||||
|
|
@ -749,6 +763,7 @@ async def test_request_body_with_html_script_tags():
|
|||
}
|
||||
|
||||
mock_request = MagicMock()
|
||||
mock_request.state = State()
|
||||
mock_request.body = AsyncMock(return_value=orjson.dumps(test_payload))
|
||||
mock_request.headers = {"content-type": "application/json"}
|
||||
mock_request.scope = {}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue