-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_requests.py
134 lines (97 loc) · 4.18 KB
/
test_requests.py
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
import pytest
import requests
import recurl
def test_bad_url():
request = requests.PreparedRequest()
request.__dict__.update(method='GET', url='nowhere')
with pytest.raises(requests.exceptions.ConnectionError):
response = recurl.CurlEasyAdapter().send(request)
def test_bad_http_version_value():
with pytest.raises(ValueError):
recurl.CurlEasyAdapter(http_version='abc')
def test_bad_http_version_type():
with pytest.raises(TypeError):
recurl.CurlEasyAdapter(http_version=1.1)
def test_bad_maxconnects_type():
with pytest.raises(TypeError):
recurl.CurlEasyAdapter(maxconnects=1.1)
def test_close():
adapter = recurl.CurlEasyAdapter()
adapter.close()
assert True # just testing this method exists and can be called without raising
def test_google():
import datetime
response = recurl.get('https://www.google.com/', http_version='1.1')
assert response.apparent_encoding == 'windows-1250'
# assert response.cookies == None # TODO
assert isinstance(response.elapsed, datetime.timedelta)
assert response.encoding == 'ISO-8859-1'
assert 'Date' in response.headers
assert response.headers['date'] == response.headers['Date']
assert response.headers['Expires'] == '-1'
# assert response.history == None # TODO
assert response.is_permanent_redirect == False
assert response.is_redirect == False
# assert response.links == None # TODO
assert response.next == None # TODO
assert response.ok == True
assert response.raw == None # TODO
assert response.reason == 'OK'
assert isinstance(response.request, requests.PreparedRequest)
assert response.status_code == 200
assert response.text == response.content.decode('latin-1')
assert response.url == 'https://www.google.com/'
def test_head():
response = recurl.head('https://www.google.com/')
assert response.status_code == 200
assert response.content == None # TODO: requests sets b''
assert 'date' in response.headers
assert 'expires' in response.headers
def test_get():
response = recurl.get('https://httpbin.org/get', params={'val1': 4, 'val2': 2})
data = response.json()
assert data['args'] == {'val1': '4', 'val2': '2'}
assert data['url'] == 'https://httpbin.org/get?val1=4&val2=2'
def test_post():
response = recurl.post('https://httpbin.org/post', data={'val1': 4, 'val2': 2})
data = response.json()
assert data['form'] == {'val1': '4', 'val2': '2'}
def test_put():
response = recurl.put('https://httpbin.org/put', data={'val1': 4, 'val2': 2})
data = response.json()
assert data['form'] == {'val1': '4', 'val2': '2'}
def test_patch():
response = recurl.patch('https://httpbin.org/patch', data={'val1': 4, 'val2': 2})
data = response.json()
assert data['form'] == {'val1': '4', 'val2': '2'}
def test_delete():
response = recurl.delete('https://httpbin.org/delete')
assert response.ok
def test_headers():
response = recurl.get('https://httpbin.org/anything', headers={'x-myheader': 'some-value'})
data = response.json()
assert requests.structures.CaseInsensitiveDict(data['headers']).get('x-myheader') == 'some-value'
def test_timeout():
'''
NOTE:
libcurl does not provide separate error codes corresponding
to requests's ConnectionTimeout and ReadTimeout.
We can only catch the generic Timeout exception.
'''
with pytest.raises(requests.exceptions.Timeout):
recurl.get('https://httpbin.org/delay/2', timeout=1)
response = recurl.get('https://httpbin.org/delay/2')
assert response.ok
def test_timeout_tuple():
with pytest.raises(requests.exceptions.Timeout):
recurl.get('https://httpbin.org/delay/2', timeout=(1, 1))
def test_proxy():
with pytest.raises(requests.exceptions.ProxyError):
recurl.get('https://www.google.com/', proxies={'https': 'http://proxy.google.com:1080'})
response = recurl.get('https://www.google.com/')
assert response.ok
def test_cert():
with pytest.raises(requests.exceptions.RequestException): # TODO: OSError?
recurl.get('https://www.google.com/', cert='/badcert')
response = recurl.get('https://www.google.com/')
assert response.ok