forked from sammchardy/python-binance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api_request.py
More file actions
executable file
·54 lines (43 loc) · 1.67 KB
/
Copy pathtest_api_request.py
File metadata and controls
executable file
·54 lines (43 loc) · 1.67 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
from binance.client import Client
from binance.exceptions import BinanceAPIException, BinanceRequestException
import pytest
import requests_mock
import os
proxies = {}
proxy = os.getenv("PROXY")
if proxy:
proxies = {"http": proxy, "https": proxy} # tmp: improve this in the future
else:
print("No proxy set")
client = Client("api_key", "api_secret", {"proxies": proxies})
def test_invalid_json():
"""Test Invalid response Exception"""
with pytest.raises(BinanceRequestException):
with requests_mock.mock() as m:
m.get(
"https://www.binance.com/exchange-api/v1/public/asset-service/product/get-products?includeEtf=true",
text="<head></html>",
)
m.get(
"https://www.binance.com/bapi/asset/v2/public/asset-service/product/get-products?includeEtf=true",
text="<head></html>",
)
client.get_products()
def test_api_exception():
"""Test API response Exception"""
with pytest.raises(BinanceAPIException):
with requests_mock.mock() as m:
json_obj = {"code": 1002, "msg": "Invalid API call"}
m.get("https://api.binance.com/api/v3/time", json=json_obj, status_code=400)
client.get_server_time()
def test_api_exception_invalid_json():
"""Test API response Exception"""
with pytest.raises(BinanceAPIException):
with requests_mock.mock() as m:
not_json_str = "<html><body>Error</body></html>"
m.get(
"https://api.binance.com/api/v3/time",
text=not_json_str,
status_code=400,
)
client.get_server_time()