| 1 |
import pickle |
| 2 |
import pytest |
| 3 |
import socket |
| 4 |
import sys |
| 5 |
import tests |
| 6 |
import time |
| 7 |
from unittest import mock |
| 8 |
import urllib |
| 9 |
|
| 10 |
import httplib2 |
| 11 |
|
| 12 |
|
| 13 |
def test_convert_byte_str(): |
| 14 |
with tests.assert_raises(TypeError): |
| 15 |
httplib2._convert_byte_str(4) |
| 16 |
assert httplib2._convert_byte_str(b"Hello") == "Hello" |
| 17 |
assert httplib2._convert_byte_str("World") == "World" |
| 18 |
|
| 19 |
|
| 20 |
def test_reflect(): |
| 21 |
http = httplib2.Http() |
| 22 |
with tests.server_reflect() as uri: |
| 23 |
response, content = http.request(uri + "?query", "METHOD") |
| 24 |
assert response.status == 200 |
| 25 |
host = urllib.parse.urlparse(uri).netloc |
| 26 |
assert content.startswith( |
| 27 |
"""\ |
| 28 |
METHOD /?query HTTP/1.1\r\n\ |
| 29 |
Host: {host}\r\n""".format( |
| 30 |
host=host |
| 31 |
).encode() |
| 32 |
), content |
| 33 |
|
| 34 |
|
| 35 |
def test_pickle_http(): |
| 36 |
http = httplib2.Http(cache=tests.get_cache_path()) |
| 37 |
new_http = pickle.loads(pickle.dumps(http)) |
| 38 |
|
| 39 |
assert tuple(sorted(new_http.__dict__)) == tuple(sorted(http.__dict__)) |
| 40 |
assert new_http.credentials.credentials == http.credentials.credentials |
| 41 |
assert new_http.certificates.credentials == http.certificates.credentials |
| 42 |
assert new_http.cache.cache == http.cache.cache |
| 43 |
for key in new_http.__dict__: |
| 44 |
if key not in ("cache", "certificates", "credentials"): |
| 45 |
assert getattr(new_http, key) == getattr(http, key) |
| 46 |
|
| 47 |
|
| 48 |
def test_pickle_http_with_connection(): |
| 49 |
http = httplib2.Http() |
| 50 |
http.request("http://random-domain:81/", connection_type=tests.MockHTTPConnection) |
| 51 |
new_http = pickle.loads(pickle.dumps(http)) |
| 52 |
assert tuple(http.connections) == ("http:random-domain:81",) |
| 53 |
assert new_http.connections == {} |
| 54 |
|
| 55 |
|
| 56 |
def test_pickle_custom_request_http(): |
| 57 |
http = httplib2.Http() |
| 58 |
http.request = lambda: None |
| 59 |
http.request.dummy_attr = "dummy_value" |
| 60 |
new_http = pickle.loads(pickle.dumps(http)) |
| 61 |
assert getattr(new_http.request, "dummy_attr", None) is None |
| 62 |
|
| 63 |
|
| 64 |
@pytest.mark.xfail( |
| 65 |
sys.version_info >= (3,), |
| 66 |
reason=( |
| 67 |
"FIXME: for unknown reason global timeout test fails in Python3 " |
| 68 |
"with response 200" |
| 69 |
), |
| 70 |
) |
| 71 |
def test_timeout_global(): |
| 72 |
def handler(request): |
| 73 |
time.sleep(0.5) |
| 74 |
return tests.http_response_bytes() |
| 75 |
|
| 76 |
try: |
| 77 |
socket.setdefaulttimeout(0.1) |
| 78 |
except Exception: |
| 79 |
pytest.skip("cannot set global socket timeout") |
| 80 |
try: |
| 81 |
http = httplib2.Http() |
| 82 |
http.force_exception_to_status_code = True |
| 83 |
with tests.server_request(handler) as uri: |
| 84 |
response, content = http.request(uri) |
| 85 |
assert response.status == 408 |
| 86 |
assert response.reason.startswith("Request Timeout") |
| 87 |
finally: |
| 88 |
socket.setdefaulttimeout(None) |
| 89 |
|
| 90 |
|
| 91 |
def test_timeout_individual(): |
| 92 |
def handler(request): |
| 93 |
time.sleep(0.5) |
| 94 |
return tests.http_response_bytes() |
| 95 |
|
| 96 |
http = httplib2.Http(timeout=0.1) |
| 97 |
http.force_exception_to_status_code = True |
| 98 |
|
| 99 |
with tests.server_request(handler) as uri: |
| 100 |
response, content = http.request(uri) |
| 101 |
assert response.status == 408 |
| 102 |
assert response.reason.startswith("Request Timeout") |
| 103 |
|
| 104 |
|
| 105 |
def test_timeout_subsequent(): |
| 106 |
class Handler(object): |
| 107 |
number = 0 |
| 108 |
|
| 109 |
@classmethod |
| 110 |
def handle(cls, request): |
| 111 |
# request.number is always 1 because of |
| 112 |
# the new socket connection each time |
| 113 |
cls.number += 1 |
| 114 |
if cls.number % 2 != 0: |
| 115 |
time.sleep(0.6) |
| 116 |
return tests.http_response_bytes(status=500) |
| 117 |
return tests.http_response_bytes(status=200) |
| 118 |
|
| 119 |
http = httplib2.Http(timeout=0.5) |
| 120 |
http.force_exception_to_status_code = True |
| 121 |
|
| 122 |
with tests.server_request(Handler.handle, request_count=2) as uri: |
| 123 |
response, _ = http.request(uri) |
| 124 |
assert response.status == 408 |
| 125 |
assert response.reason.startswith("Request Timeout") |
| 126 |
|
| 127 |
response, _ = http.request(uri) |
| 128 |
assert response.status == 200 |
| 129 |
|
| 130 |
|
| 131 |
def test_timeout_https(): |
| 132 |
c = httplib2.HTTPSConnectionWithTimeout("localhost", 80, timeout=47) |
| 133 |
assert 47 == c.timeout |
| 134 |
|
| 135 |
|
| 136 |
# @pytest.mark.xfail( |
| 137 |
# sys.version_info >= (3,), |
| 138 |
# reason='[py3] last request should open new connection, but client does not realize socket was closed by server', |
| 139 |
# ) |
| 140 |
def test_connection_close(): |
| 141 |
http = httplib2.Http() |
| 142 |
g = [] |
| 143 |
|
| 144 |
def handler(request): |
| 145 |
g.append(request.number) |
| 146 |
return tests.http_response_bytes(proto="HTTP/1.1") |
| 147 |
|
| 148 |
with tests.server_request(handler, request_count=3) as uri: |
| 149 |
http.request(uri, "GET") # conn1 req1 |
| 150 |
for c in http.connections.values(): |
| 151 |
assert c.sock is not None |
| 152 |
http.request(uri, "GET", headers={"connection": "close"}) |
| 153 |
time.sleep(0.7) |
| 154 |
http.request(uri, "GET") # conn2 req1 |
| 155 |
assert g == [1, 2, 1] |
| 156 |
|
| 157 |
|
| 158 |
def test_get_end2end_headers(): |
| 159 |
# one end to end header |
| 160 |
response = {"content-type": "application/atom+xml", "te": "deflate"} |
| 161 |
end2end = httplib2._get_end2end_headers(response) |
| 162 |
assert "content-type" in end2end |
| 163 |
assert "te" not in end2end |
| 164 |
assert "connection" not in end2end |
| 165 |
|
| 166 |
# one end to end header that gets eliminated |
| 167 |
response = { |
| 168 |
"connection": "content-type", |
| 169 |
"content-type": "application/atom+xml", |
| 170 |
"te": "deflate", |
| 171 |
} |
| 172 |
end2end = httplib2._get_end2end_headers(response) |
| 173 |
assert "content-type" not in end2end |
| 174 |
assert "te" not in end2end |
| 175 |
assert "connection" not in end2end |
| 176 |
|
| 177 |
# Degenerate case of no headers |
| 178 |
response = {} |
| 179 |
end2end = httplib2._get_end2end_headers(response) |
| 180 |
assert len(end2end) == 0 |
| 181 |
|
| 182 |
# Degenerate case of connection referring to a header not passed in |
| 183 |
response = {"connection": "content-type"} |
| 184 |
end2end = httplib2._get_end2end_headers(response) |
| 185 |
assert len(end2end) == 0 |
| 186 |
|
| 187 |
|
| 188 |
@pytest.mark.parametrize("scheme", ("http", "https")) |
| 189 |
def test_ipv6(scheme): |
| 190 |
# Even if IPv6 isn't installed on a machine it should just raise socket.error |
| 191 |
uri = "{scheme}://[::1]:1/".format(scheme=scheme) |
| 192 |
try: |
| 193 |
httplib2.Http(timeout=0.1).request(uri) |
| 194 |
except socket.gaierror: |
| 195 |
assert False, "should get the address family right for IPv6" |
| 196 |
except socket.error: |
| 197 |
pass |
| 198 |
|
| 199 |
|
| 200 |
@pytest.mark.parametrize( |
| 201 |
"conn_type", |
| 202 |
(httplib2.HTTPConnectionWithTimeout, httplib2.HTTPSConnectionWithTimeout), |
| 203 |
) |
| 204 |
def test_connection_proxy_info_attribute_error(conn_type): |
| 205 |
# HTTPConnectionWithTimeout did not initialize its .proxy_info attribute |
| 206 |
# https://github.com/httplib2/httplib2/pull/97 |
| 207 |
# Thanks to Joseph Ryan https://github.com/germanjoey |
| 208 |
conn = conn_type("no-such-hostname.", 80) |
| 209 |
# TODO: replace mock with dummy local server |
| 210 |
with tests.assert_raises(socket.gaierror): |
| 211 |
with mock.patch("socket.socket.connect", side_effect=socket.gaierror): |
| 212 |
conn.request("GET", "/") |
| 213 |
|
| 214 |
|
| 215 |
def test_http_443_forced_https(): |
| 216 |
http = httplib2.Http() |
| 217 |
http.force_exception_to_status_code = True |
| 218 |
uri = "http://localhost:443/" |
| 219 |
# sorry, using internal structure of Http to check chosen scheme |
| 220 |
with mock.patch("httplib2.Http._request") as m: |
| 221 |
http.request(uri) |
| 222 |
assert len(m.call_args) > 0, "expected Http._request() call" |
| 223 |
conn = m.call_args[0][0] |
| 224 |
assert isinstance(conn, httplib2.HTTPConnectionWithTimeout) |
| 225 |
|
| 226 |
|
| 227 |
def test_close(): |
| 228 |
http = httplib2.Http() |
| 229 |
assert len(http.connections) == 0 |
| 230 |
with tests.server_const_http() as uri: |
| 231 |
http.request(uri) |
| 232 |
assert len(http.connections) == 1 |
| 233 |
http.close() |
| 234 |
assert len(http.connections) == 0 |
| 235 |
|
| 236 |
|
| 237 |
def test_connect_exception_type(): |
| 238 |
# This autoformatting PR actually changed the behavior of error handling: |
| 239 |
# https://github.com/httplib2/httplib2/pull/105/files#diff-c6669c781a2dee1b2d2671cab4e21c66L985 |
| 240 |
# potentially changing the type of the error raised by connect() |
| 241 |
# https://github.com/httplib2/httplib2/pull/150 |
| 242 |
http = httplib2.Http() |
| 243 |
with mock.patch("httplib2.socket.socket.connect", side_effect=socket.timeout("foo")): |
| 244 |
with tests.assert_raises(socket.timeout): |
| 245 |
http.request(tests.DUMMY_URL) |