forked from sammchardy/python-binance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_headers.py
More file actions
92 lines (76 loc) · 2.88 KB
/
Copy pathtest_headers.py
File metadata and controls
92 lines (76 loc) · 2.88 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
81
82
83
84
85
86
87
88
89
90
91
92
import requests_mock
import pytest
from aioresponses import aioresponses
from binance import Client, AsyncClient
client = Client(api_key="api_key", api_secret="api_secret", ping=False)
def test_get_headers():
with requests_mock.mock() as m:
m.get("https://api.binance.com/api/v3/account", json={}, status_code=200)
client.get_account()
headers = m.last_request._request.headers
assert "Content-Type" in headers
assert headers["Content-Type"] == "application/json"
def test_post_headers():
with requests_mock.mock() as m:
m.post("https://api.binance.com/api/v3/order", json={}, status_code=200)
client.create_order(symbol="LTCUSDT", side="BUY", type="MARKET", quantity=0.1)
headers = m.last_request._request.headers
assert "Content-Type" in headers
assert headers["Content-Type"] == "application/x-www-form-urlencoded"
def test_post_headers_overriden():
with requests_mock.mock() as m:
m.post("https://api.binance.com/api/v3/order", json={}, status_code=200)
client.create_order(
symbol="LTCUSDT",
side="BUY",
type="MARKET",
quantity=0.1,
headers={"Content-Type": "myvalue"},
)
headers = m.last_request._request.headers
assert "Content-Type" in headers
assert headers["Content-Type"] == "myvalue"
@pytest.mark.asyncio()
async def test_post_headers_async():
clientAsync = AsyncClient(
api_key="api_key", api_secret="api_secret"
) # reuse client later
with aioresponses() as m:
def handler(url, **kwargs):
headers = kwargs["headers"]
assert "Content-Type" in headers
assert headers["Content-Type"] == "application/x-www-form-urlencoded"
m.post(
"https://api.binance.com/api/v3/order",
payload={"id": 1},
status=200,
callback=handler,
)
await clientAsync.create_order(
symbol="LTCUSDT", side="BUY", type="MARKET", quantity=0.1
)
await clientAsync.close_connection()
@pytest.mark.asyncio()
async def test_post_headers_overriden_async():
clientAsync = AsyncClient(
api_key="api_key", api_secret="api_secret"
) # reuse client later
with aioresponses() as m:
def handler(url, **kwargs):
headers = kwargs["headers"]
assert "Content-Type" in headers
assert headers["Content-Type"] == "myvalue"
m.post(
"https://api.binance.com/api/v3/order",
payload={"id": 1},
status=200,
callback=handler,
)
await clientAsync.create_order(
symbol="LTCUSDT",
side="BUY",
type="MARKET",
quantity=0.1,
headers={"Content-Type": "myvalue"},
)
await clientAsync.close_connection()