forked from openai/openai-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api_requestor.py
More file actions
69 lines (56 loc) · 2.3 KB
/
test_api_requestor.py
File metadata and controls
69 lines (56 loc) · 2.3 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
import json
import pytest
import requests
from pytest_mock import MockerFixture
from openai import Model
from openai.api_requestor import APIRequestor
@pytest.mark.requestor
def test_requestor_sets_request_id(mocker: MockerFixture) -> None:
# Fake out 'requests' and confirm that the X-Request-Id header is set.
got_headers = {}
def fake_request(self, *args, **kwargs):
nonlocal got_headers
got_headers = kwargs["headers"]
r = requests.Response()
r.status_code = 200
r.headers["content-type"] = "application/json"
r._content = json.dumps({}).encode("utf-8")
return r
mocker.patch("requests.sessions.Session.request", fake_request)
fake_request_id = "1234"
Model.retrieve("xxx", request_id=fake_request_id) # arbitrary API resource
got_request_id = got_headers.get("X-Request-Id")
assert got_request_id == fake_request_id
@pytest.mark.requestor
def test_requestor_open_ai_headers() -> None:
api_requestor = APIRequestor(key="test_key", api_type="open_ai")
headers = {"Test_Header": "Unit_Test_Header"}
headers = api_requestor.request_headers(
method="get", extra=headers, request_id="test_id"
)
assert "Test_Header" in headers
assert headers["Test_Header"] == "Unit_Test_Header"
assert "Authorization" in headers
assert headers["Authorization"] == "Bearer test_key"
@pytest.mark.requestor
def test_requestor_azure_headers() -> None:
api_requestor = APIRequestor(key="test_key", api_type="azure")
headers = {"Test_Header": "Unit_Test_Header"}
headers = api_requestor.request_headers(
method="get", extra=headers, request_id="test_id"
)
assert "Test_Header" in headers
assert headers["Test_Header"] == "Unit_Test_Header"
assert "api-key" in headers
assert headers["api-key"] == "test_key"
@pytest.mark.requestor
def test_requestor_azure_ad_headers() -> None:
api_requestor = APIRequestor(key="test_key", api_type="azure_ad")
headers = {"Test_Header": "Unit_Test_Header"}
headers = api_requestor.request_headers(
method="get", extra=headers, request_id="test_id"
)
assert "Test_Header" in headers
assert headers["Test_Header"] == "Unit_Test_Header"
assert "Authorization" in headers
assert headers["Authorization"] == "Bearer test_key"