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
|
from __future__ import absolute_import, unicode_literals
import pytest
from io import BytesIO
from vine import promise
from case import Mock, skip
from kombu.async import http
from kombu.async.http.base import BaseClient, normalize_header
from kombu.exceptions import HttpError
from t.mocks import PromiseMock
class test_Headers:
def test_normalize(self):
assert normalize_header('accept-encoding') == 'Accept-Encoding'
@pytest.mark.usefixtures('hub')
class test_Request:
def test_init(self):
x = http.Request('http://foo', method='POST')
assert x.url == 'http://foo'
assert x.method == 'POST'
x = http.Request('x', max_redirects=100)
assert x.max_redirects == 100
assert isinstance(x.headers, http.Headers)
h = http.Headers()
x = http.Request('x', headers=h)
assert x.headers is h
assert isinstance(x.on_ready, promise)
def test_then(self):
callback = PromiseMock(name='callback')
x = http.Request('http://foo')
x.then(callback)
x.on_ready(1)
callback.assert_called_with(1)
@pytest.mark.usefixtures('hub')
class test_Response:
def test_init(self):
req = http.Request('http://foo')
r = http.Response(req, 200)
assert r.status == 'OK'
assert r.effective_url == 'http://foo'
r.raise_for_error()
def test_raise_for_error(self):
req = http.Request('http://foo')
r = http.Response(req, 404)
assert r.status == 'Not Found'
assert r.error
with pytest.raises(HttpError):
r.raise_for_error()
def test_get_body(self):
req = http.Request('http://foo')
req.buffer = BytesIO()
req.buffer.write(b'hello')
rn = http.Response(req, 200, buffer=None)
assert rn.body is None
r = http.Response(req, 200, buffer=req.buffer)
assert r._body is None
assert r.body == b'hello'
assert r._body == b'hello'
assert r.body == b'hello'
class test_BaseClient:
@pytest.fixture(autouse=True)
def setup_hub(self, hub):
self.hub = hub
def test_init(self):
c = BaseClient(Mock(name='hub'))
assert c.hub
assert c._header_parser
def test_perform(self):
c = BaseClient(Mock(name='hub'))
c.add_request = Mock(name='add_request')
c.perform('http://foo')
c.add_request.assert_called()
assert isinstance(c.add_request.call_args[0][0], http.Request)
req = http.Request('http://bar')
c.perform(req)
c.add_request.assert_called_with(req)
def test_add_request(self):
c = BaseClient(Mock(name='hub'))
with pytest.raises(NotImplementedError):
c.add_request(Mock(name='request'))
def test_header_parser(self):
c = BaseClient(Mock(name='hub'))
parser = c._header_parser
headers = http.Headers()
c.on_header(headers, 'HTTP/1.1')
c.on_header(headers, 'x-foo-bar: 123')
c.on_header(headers, 'People: George Costanza')
assert headers._prev_key == 'People'
c.on_header(headers, ' Jerry Seinfeld')
c.on_header(headers, ' Elaine Benes')
c.on_header(headers, ' Cosmo Kramer')
assert not headers.complete
c.on_header(headers, '')
assert headers.complete
with pytest.raises(KeyError):
parser.throw(KeyError('foo'))
c.on_header(headers, '')
assert headers['X-Foo-Bar'] == '123'
assert (headers['People'] ==
'George Costanza Jerry Seinfeld Elaine Benes Cosmo Kramer')
def test_close(self):
BaseClient(Mock(name='hub')).close()
def test_as_context(self):
c = BaseClient(Mock(name='hub'))
c.close = Mock(name='close')
with c:
pass
c.close.assert_called_with()
@skip.if_pypy()
@skip.unless_module('pycurl')
class test_Client:
def test_get_client(self, hub):
client = http.get_client()
assert client.hub is hub
client2 = http.get_client(hub)
assert client2 is client
assert client2.hub is hub
|