-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtest_connection.py
More file actions
192 lines (143 loc) · 5.6 KB
/
test_connection.py
File metadata and controls
192 lines (143 loc) · 5.6 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import pytest
import requests
import responses
from client.connection import Connection, Session
from client.exceptions import ArkHTTPException
def test_connection_creation_sets_default_session_headers_and_variables():
connection = Connection('http://127.0.0.1:4003')
assert connection.hosts == {
'api': 'http://127.0.0.1:4003',
'transactions': None,
'evm': None,
}
assert isinstance(connection.session, requests.Session)
assert connection.session.headers['Content-Type'] == 'application/json'
def test_connection_with_hosts_dict():
connection = Connection({
'api': 'http://127.0.0.1:4003/api',
'transactions': 'http://127.0.0.1:4003/transactions',
'evm': 'http://127.0.0.1:4003/evm',
})
assert connection.session.hostname == 'http://127.0.0.1:4003/api'
connection.with_endpoint('transactions')
assert connection.session.hostname == 'http://127.0.0.1:4003/transactions'
connection.with_endpoint('evm')
assert connection.session.hostname == 'http://127.0.0.1:4003/evm'
connection.with_endpoint('api')
assert connection.session.hostname == 'http://127.0.0.1:4003/api'
def test_connection_request_retry_successful():
responses.add(
responses.GET,
'http://127.0.0.1:4003/spongebob',
body=requests.exceptions.RequestException())
responses.add(
responses.GET,
'http://127.0.0.1:4003/spongebob',
body=requests.exceptions.RequestException())
responses.add(
responses.GET,
'http://127.0.0.1:4003/spongebob',
json={'success': True},
status=200
)
connection = Connection('http://127.0.0.1:4003')
data = connection.get('spongebob')
assert data == {'success': True}
assert len(responses.calls) == 3
assert responses.calls[0].request.url == 'http://127.0.0.1:4003/spongebob'
def test_connection_raises_for_request_retry_failure():
responses.add(
responses.GET,
'http://127.0.0.1:4003/spongebob',
body=requests.exceptions.RequestException())
connection = Connection('http://127.0.0.1:4003')
with pytest.raises(ArkHTTPException):
connection.get('spongebob')
assert len(responses.calls) == 3
def test_handle_response_raises_for_no_content_in_response():
responses.add(
responses.GET,
'http://127.0.0.1:4003/spongebob',
status=404
)
connection = Connection('http://127.0.0.1:4003')
response = requests.get('http://127.0.0.1:4003/spongebob')
with pytest.raises(ArkHTTPException) as exception:
connection._handle_response(response)
assert str(exception.value) == 'No content in response'
assert exception.value.response == response
def test_handle_response_raises_for_success_false_in_response():
responses.add(
responses.GET,
'http://127.0.0.1:4003/spongebob',
json={'success': False, 'error': 'Best error ever'},
status=404
)
connection = Connection('http://127.0.0.1:4003')
response = requests.get('http://127.0.0.1:4003/spongebob')
with pytest.raises(ArkHTTPException) as exception:
connection._handle_response(response)
assert str(exception.value) == 'GET 404 http://127.0.0.1:4003/spongebob - Best error ever'
assert exception.value.response == response
def test_handle_response_retuns_body_from_request():
responses.add(
responses.GET,
'http://127.0.0.1:4003/spongebob',
json={'success': True},
status=200
)
connection = Connection('http://127.0.0.1:4003')
response = requests.get('http://127.0.0.1:4003/spongebob')
body = connection._handle_response(response)
assert body == {'success': True}
@pytest.mark.parametrize('method,func_name', [
(responses.GET, 'get'),
(responses.POST, 'post'),
])
def test_http_methods_call_correct_url_and_return_correct_response(method, func_name):
responses.add(
method,
'http://127.0.0.1:4003/spongebob',
json={'success': True},
status=200
)
connection = Connection('http://127.0.0.1:4003')
data = getattr(connection, func_name)('spongebob')
assert data == {'success': True}
assert len(responses.calls) == 1
assert responses.calls[0].request.url == 'http://127.0.0.1:4003/spongebob'
@pytest.mark.parametrize('method,func_name', [
(responses.GET, 'get'),
# (responses.POST, 'post'),
])
def test_http_methods_call_correct_url_with_params_and_return_correct_response(method, func_name):
responses.add(
method,
'http://127.0.0.1:4003/spongebob',
json={'success': True},
status=200
)
connection = Connection('http://127.0.0.1:4003')
data = getattr(connection, func_name)('spongebob', params={'foo': 'bar'})
assert data == {'success': True}
assert len(responses.calls) == 1
assert responses.calls[0].request.url == 'http://127.0.0.1:4003/spongebob?foo=bar'
def test_session_detects_hostname_correctly():
session = Session(hostname="test.com")
assert session.hostname == "test.com"
assert isinstance(session, requests.Session)
def test_session_throws_error_when_missing_hostname():
with pytest.raises(ValueError) as exception:
Session()
assert exception.value == 'hostname is required'
def test_session_prepends_hostname_to_url():
responses.add(
responses.GET,
'http://test.com/spongebob',
json={'success': True},
status=200
)
session = Session(hostname="http://test.com")
session.get('spongebob')
assert len(responses.calls) == 1
assert responses.calls[0].request.url == 'http://test.com/spongebob'