def test_headers_iadd_dict(self): added = Headers() for dictionary in dictionaries: added += dictionary assert added == Headers({"str": "str", "bytes": "bytes"}) assert added == Headers({"str": "str", b"bytes": b"bytes"}) assert added == Headers({b"str": b"str", "bytes": b"bytes"}) assert added == Headers({b"str": b"str", b"bytes": b"bytes"})
def test_headers_iadd_headers(self): added = Headers() for header in headers: added += header assert added == Headers({"str": "str", "bytes": "bytes"}) assert added == Headers({"str": "str", b"bytes": b"bytes"}) assert added == Headers({b"str": b"str", "bytes": b"bytes"}) assert added == Headers({b"str": b"str", b"bytes": b"bytes"})
def test_headers_setattr_and_getattr_single_word(self): headers = Headers() headers.Host = "github.com" assert headers.Host == "github.com" assert headers.Host == b"github.com" assert headers["Host"] == "github.com" assert headers["Host"] == b"github.com" assert headers[b"Host"] == "github.com" assert headers[b"Host"] == b"github.com"
def test_headers_setattr_and_getattr_double_word(self): headers = Headers() headers.Cache_Control = "no-cache" assert headers.Cache_Control == "no-cache" assert headers.Cache_Control == b"no-cache" assert headers["Cache-Control"] == "no-cache" assert headers["Cache-Control"] == b"no-cache" assert headers[b"Cache-Control"] == "no-cache" assert headers[b"Cache-Control"] == b"no-cache"
def test_response_init_large(self): Response( protocol="GET", status=200, status_msg="OK", headers=Headers({ "Host": "www.google.com", "Accept": "*/*", "Connection": "keep-alive", }), body=json.dumps({"hello": "world"}), )
def test_request_init_large(self): Request( method="GET", target="/", protocol="HTTP/1.1", headers=Headers({ "Host": "www.google.com", "Accept": "*/*", "User-Agent": "httpsuite/1.0.0", "Connection": "keep-alive", }), body=json.dumps({"hello": "world"}), )
def test_response_str(self): parsed = Response.parse(response_raw) assert isinstance(parsed, Response) assert parsed.protocol == "HTTP/1.1" assert parsed.status == 200 assert parsed.status_msg == "OK" assert parsed.headers == Headers({ "Test": "Headers", "Host": "www.google.com", "Accept": "*/*", "Connection": "keep-alive", }) assert parsed.body == '{"hello": "world"}'
def test_request_str(self): parsed = Request.parse(request_raw) assert isinstance(parsed, Request) assert parsed.method == "POST" assert parsed.target == "/" assert parsed.protocol == "HTTP/1.1" assert parsed.headers == Headers({ "Host": "www.google.com", "Accept": "*/*", "User-Agent": "httpsuite/1.0.0", "Connection": "keep-alive", }) assert parsed.body == '{"hello": "world"}'
def test_headers_add_invalid_type(self, other): added = Headers() with pytest.raises(TypeError): added + other
def test_headers_add_dict(self): added = headers[0] + dictionaries[1] assert added == Headers({"str": "str", "bytes": "bytes"}) assert added == Headers({"str": "str", b"bytes": b"bytes"}) assert added == Headers({b"str": b"str", "bytes": b"bytes"}) assert added == Headers({b"str": b"str", b"bytes": b"bytes"})
def test_headers_add_headers(self): added = headers[0] + headers[1] assert added == Headers({"str": "str", "bytes": "bytes"}) assert added == Headers({"str": "str", b"bytes": b"bytes"}) assert added == Headers({b"str": b"str", "bytes": b"bytes"}) assert added == Headers({b"str": b"str", b"bytes": b"bytes"})
def test_message_setter_request_headers_valid(self): request.headers = {} request.headers = Headers({})
from httpsuite import Headers import pytest dictionaries = [{"str": "str"}, {b"bytes": b"bytes"}] headers = [Headers(d.copy()) for d in dictionaries] valid_types = [{ "str": "str" }, { b"bytes": b"bytes" }, *[header for header in headers]] invalid_types = [ "str", b"bytes", 200, ["List"], ("Tuple", ), {"Set"}, ] class Test_headers_init: @pytest.mark.parametrize("other", valid_types) def test_headers_init_valid_types(self, other): headers = Headers(other) @pytest.mark.parametrize("other", invalid_types) def test_headers_init_invalid_types(self, other): with pytest.raises(TypeError): headers = Headers(other)
def test_message_getter_headers_valid(self, zipped): assert zipped[0].headers == zipped[1] assert zipped[0].headers == Headers(zipped[1])
def test_message_setter_response_headers_valid(self): response.headers = {} response.headers = Headers({})
}), body=json.dumps({"hello": "world"}), ) def test_init_wrong_type(self): with pytest.raises(TypeError): Request(method={}, target={}, protocol={}) request = Request( method="GET", target="/", protocol="HTTP/1.1", headers=Headers({ "Host": "www.google.com", "Accept": "*/*", "User-Agent": "httpsuite/1.0.0", "Connection": "keep-alive", }), body=json.dumps({"hello": "world"}), ) request_raw = (b"POST / HTTP/1.1\r\n" b"Host: www.google.com\r\n" b"Accept: */*\r\n" b"User-Agent: httpsuite/1.0.0\r\n" b"Connection: keep-alive\r\n" b"\r\n" b'{"hello": "world"}') class Test_request_getters:
def test_headers_init_valid_types(self, other): headers = Headers(other)
def test_headers_init_invalid_types(self, other): with pytest.raises(TypeError): headers = Headers(other)
) def test_init_wrong_type(self): with pytest.raises(TypeError): # pylint: disable=unexpected-keyword-arg, no-value-for-parameter Response(method=None, target=None, protocol=None) response = Response( protocol="HTTP/1.1", status=200, status_msg="OK", headers=Headers( { "Host": "www.google.com", "Accept": "*/*", "Connection": "keep-alive", } ), body=json.dumps({"hello": "world"}), ) response_raw = ( b"HTTP/1.1 200 OK\r\n" b"Test: Headers\r\n" b"Host: www.google.com\r\n" b"Accept: */*\r\n" b"Connection: keep-alive\r\n" b"\r\n" b'{"hello": "world"}' )
# ==================== # Request # ==================== request_headers = { "Host": "www.google.com", "Accept": "*/*", "User-Agent": "httpsuite/1.0.0", "Connection": "keep-alive", } request_body = json.dumps({"hello": "world"}) request = Request( method="GET", target="/", protocol="HTTP/1.1", headers=Headers(request_headers), body=request_body, ) request_raw = b'GET / HTTP/1.1\r\nHost: www.google.com\r\nAccept: */*\r\nUser-Agent: httpsuite/1.0.0\r\nConnection: keep-alive\r\n\r\n{"hello": "world"}' request_first_line = b"GET / HTTP/1.1" # ==================== # Response # ==================== response_headers = { "Host": "www.google.com", "Accept": "*/*", "Connection": "keep-alive", "Keep-Alive": "timeout=5, max=1000", "Server": "httpsuite/1.0.0", }