-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_proxy_server_codex.py
More file actions
80 lines (55 loc) · 2.57 KB
/
Copy pathtest_proxy_server_codex.py
File metadata and controls
80 lines (55 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import json
import pytest
import proxy_server
DEFAULT_REGEX = proxy_server.build_allowed_paths_regex(
proxy_server.DEFAULT_ALLOWED_PATH_PATTERNS
)
class DummyRequest:
def __init__(self, path, method="POST", content=b"{}"):
self.path = path
self.method = method
self.content = content
class DummyFlow:
def __init__(self, path, method="POST", content=b"{}"):
self.request = DummyRequest(path, method, content)
self.response = None
@pytest.mark.asyncio
async def test_chat_completions_success(monkeypatch):
interceptor = proxy_server.AIInterceptor(DEFAULT_REGEX)
async def mock_messages(data, method):
return {"id": "1"}
monkeypatch.setattr(interceptor.codex_handler, "handle_messages_request", mock_messages)
body = json.dumps({"messages": [{"role": "user", "content": "hi"}]})
flow = DummyFlow("/v1/chat/completions", content=body.encode())
await interceptor._handle_codex_request(flow)
assert flow.response.status_code == 200
@pytest.mark.asyncio
async def test_chat_completions_not_found(monkeypatch):
interceptor = proxy_server.AIInterceptor(DEFAULT_REGEX)
async def mock_messages(data, method):
return {"error": {"type": "not_found_error", "message": "missing"}}
monkeypatch.setattr(interceptor.codex_handler, "handle_messages_request", mock_messages)
body = json.dumps({"messages": []})
flow = DummyFlow("/v1/chat/completions", content=body.encode())
await interceptor._handle_codex_request(flow)
assert flow.response.status_code == 404
@pytest.mark.asyncio
async def test_completions_success(monkeypatch):
interceptor = proxy_server.AIInterceptor(DEFAULT_REGEX)
async def mock_complete(data, method):
return {"completion": "ok"}
monkeypatch.setattr(interceptor.codex_handler, "handle_complete_request", mock_complete)
body = json.dumps({"prompt": "hi"})
flow = DummyFlow("/v1/completions", content=body.encode())
await interceptor._handle_codex_request(flow)
assert flow.response.status_code == 200
@pytest.mark.asyncio
async def test_completions_invalid(monkeypatch):
interceptor = proxy_server.AIInterceptor(DEFAULT_REGEX)
async def mock_complete(data, method):
return {"error": {"type": "invalid_request_error", "message": "bad"}}
monkeypatch.setattr(interceptor.codex_handler, "handle_complete_request", mock_complete)
body = json.dumps({"prompt": "hi"})
flow = DummyFlow("/v1/completions", content=body.encode())
await interceptor._handle_codex_request(flow)
assert flow.response.status_code == 400