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
|
import http.client
import urllib.request
import pytest
from geventhttpclient.httplib import HTTPConnection, patched
from tests.common import HTTPBIN_HOST, LISTENER, server
def wrong_response_status_line(sock, addr):
sock.recv(4096)
sock.sendall(b"HTTP/1.1 apfais df0 asdf\r\n\r\n")
def test_httplib_exception():
with server(wrong_response_status_line):
connection = HTTPConnection(*LISTENER)
connection.request("GET", "/")
with pytest.raises(http.client.HTTPException):
connection.getresponse()
def success_response(sock, addr):
sock.recv(4096)
sock.sendall(
b"HTTP/1.1 200 Ok\r\n"
b"Content-Type: text/plain\r\n"
b"Set-Cookie: foo=bar\r\n"
b"Set-Cookie: baz=bar\r\n"
b"Content-Length: 12\r\n\r\n"
b"Hello World!"
)
def test_success_response():
with server(success_response):
connection = HTTPConnection(*LISTENER)
connection.request("GET", "/")
response = connection.getresponse()
assert response.should_keep_alive()
assert response.message_complete
assert not response.should_close()
assert response.read().decode() == "Hello World!"
assert response.content_length == 12
def test_msg():
with server(success_response):
connection = HTTPConnection(*LISTENER)
connection.request("GET", "/")
response = connection.getresponse()
assert response.msg["Set-Cookie"] == "foo=bar, baz=bar"
assert response.msg["Content-Type"] == "text/plain"
def test_patched():
assert http.client.HTTPResponse.__module__ == "http.client"
assert http.client.HTTPConnection.__module__ == "http.client"
with patched():
assert http.client.HTTPResponse.__module__ == "geventhttpclient.httplib"
assert http.client.HTTPConnection.__module__ == "geventhttpclient.httplib"
assert http.client.HTTPResponse.__module__ == "http.client"
assert http.client.HTTPConnection.__module__ == "http.client"
@pytest.mark.network
@pytest.mark.parametrize("url", [f"http://{HTTPBIN_HOST}", "https://github.com"])
def test_urllib_request(url):
with patched():
content = urllib.request.urlopen(url).read()
assert content
assert b"body" in content
|