|
3 | 3 |
|
4 | 4 | """Tests for Requests HTTPClient wrapper.""" |
5 | 5 |
|
6 | | -from __future__ import division |
7 | | -import json |
8 | 6 | import os |
9 | | -import pickle |
10 | | -import collections |
11 | | -import contextlib |
12 | | -import warnings |
| 7 | +import sys |
13 | 8 |
|
14 | | -import io |
15 | 9 | import pytest |
16 | | -import os |
17 | | -import sys |
18 | 10 |
|
19 | 11 | curpath = os.path.dirname(os.path.abspath(__file__)) |
20 | 12 | sys.path[:0] = [os.path.join(curpath, os.pardir)] |
21 | 13 |
|
22 | | -from pancloud import httpclient |
23 | | - |
24 | | -# from .compat import StringIO, u |
25 | | -# from .utils import override_environ |
26 | | -from urllib3.util import Timeout as Urllib3Timeout |
27 | | -import socket |
| 14 | +from pancloud.httpclient import HTTPClient |
| 15 | +from pancloud.exceptions import HTTPError, RequiredKwargsError, \ |
| 16 | + UnexpectedKwargsError |
28 | 17 |
|
29 | | -# Requests to this URL should always fail with a connection timeout (nothing |
30 | | -# listening on that port) |
31 | | -TARPIT = 'http://10.255.255.1' |
32 | 18 |
|
33 | | -try: |
34 | | - from ssl import SSLContext |
35 | | - del SSLContext |
36 | | - HAS_MODERN_SSL = True |
37 | | -except ImportError: |
38 | | - HAS_MODERN_SSL = False |
39 | | - |
40 | | -try: |
41 | | - httpclient.pyopenssl |
42 | | - HAS_PYOPENSSL = True |
43 | | -except AttributeError: |
44 | | - HAS_PYOPENSSL = False |
| 19 | +HTTPBIN = os.environ.get('HTTPBIN_URL', 'http://httpbin.org') |
| 20 | +TARPIT = os.environ.get('TARPIT', 'http://10.255.255.1') |
45 | 21 |
|
46 | 22 |
|
47 | 23 | class TestHTTPClient: |
48 | 24 |
|
49 | 25 | def test_entry_points(self): |
50 | | - pass |
| 26 | + |
| 27 | + HTTPClient(url=TARPIT).request |
| 28 | + HTTPClient(url=TARPIT).pyopenssl |
| 29 | + |
| 30 | + def test_invalid_url(self): |
| 31 | + with pytest.raises(HTTPError): |
| 32 | + HTTPClient(url='asdaksjhdakjsdh').request(method='GET') |
| 33 | + with pytest.raises(HTTPError): |
| 34 | + HTTPClient(url='http://').request(method='GET') |
| 35 | + |
| 36 | + def test_required_kwargs(self): |
| 37 | + with pytest.raises(RequiredKwargsError): |
| 38 | + HTTPClient().request() |
| 39 | + |
| 40 | + def test_connection_timeout(self): |
| 41 | + with pytest.raises(HTTPError): |
| 42 | + HTTPClient(url=TARPIT).request( |
| 43 | + method='GET', timeout=(.1, None) |
| 44 | + ) |
| 45 | + |
| 46 | + def test_read_timeout(self): |
| 47 | + with pytest.raises(HTTPError): |
| 48 | + HTTPClient(url=HTTPBIN, port=80).request( |
| 49 | + method='GET', timeout=(None, .0001), path='/' |
| 50 | + ) |
| 51 | + |
| 52 | + def test_httpclient_unexpected_kwargs(self): |
| 53 | + with pytest.raises(UnexpectedKwargsError): |
| 54 | + HTTPClient(url=TARPIT, foo='foo').request(method='GET') |
| 55 | + |
| 56 | + def test_request_unexpected_kwargs(self): |
| 57 | + with pytest.raises(UnexpectedKwargsError): |
| 58 | + HTTPClient(url=TARPIT).request(method='GET', foo='foo') |
| 59 | + |
| 60 | + def test_enforce_json(self): |
| 61 | + with pytest.raises(HTTPError): |
| 62 | + HTTPClient( |
| 63 | + url=HTTPBIN, |
| 64 | + port=80, |
| 65 | + enforce_json=True, |
| 66 | + headers={'Accept': 'application/json'} |
| 67 | + ).request(method='GET', path='/') |
| 68 | + |
| 69 | + def test_raise_for_status(self): |
| 70 | + with pytest.raises(HTTPError): |
| 71 | + HTTPClient( |
| 72 | + url=HTTPBIN, |
| 73 | + port=80, |
| 74 | + raise_for_status=True |
| 75 | + ).request(method='GET', path='/status/400') |
| 76 | + |
51 | 77 |
|
52 | 78 |
|
0 commit comments