mitmproxy/test/netlib/http/http1/test_assemble.py

103 lines
3.0 KiB
Python
Raw Normal View History

2015-09-15 22:04:23 +00:00
from __future__ import absolute_import, print_function, division
from netlib.exceptions import HttpException
2016-03-26 08:00:51 +00:00
from netlib.http import Headers
2015-09-15 22:04:23 +00:00
from netlib.http.http1.assemble import (
assemble_request, assemble_request_head, assemble_response,
assemble_response_head, _assemble_request_line, _assemble_request_headers,
2015-09-17 00:39:42 +00:00
_assemble_response_headers,
assemble_body)
2015-09-15 22:04:23 +00:00
from netlib.tutils import treq, raises, tresp
def test_assemble_request():
assert assemble_request(treq()) == (
2015-09-15 22:04:23 +00:00
b"GET /path HTTP/1.1\r\n"
b"header: qvalue\r\n"
b"content-length: 7\r\n"
b"host: address:22\r\n"
2015-09-15 22:04:23 +00:00
b"\r\n"
b"content"
)
with raises(HttpException):
2016-03-26 08:00:51 +00:00
assemble_request(treq(content=None))
2015-09-15 22:04:23 +00:00
def test_assemble_request_head():
2016-07-08 00:50:26 +00:00
c = assemble_request_head(treq(content=b"foo"))
2015-09-15 22:04:23 +00:00
assert b"GET" in c
assert b"qvalue" in c
2016-01-30 21:03:24 +00:00
assert b"content-length" in c
assert b"foo" not in c
2015-09-15 22:04:23 +00:00
def test_assemble_response():
assert assemble_response(tresp()) == (
2015-09-15 22:04:23 +00:00
b"HTTP/1.1 200 OK\r\n"
b"header-response: svalue\r\n"
2016-05-29 20:07:09 +00:00
b"content-length: 7\r\n"
2015-09-15 22:04:23 +00:00
b"\r\n"
b"message"
)
with raises(HttpException):
2016-03-26 08:00:51 +00:00
assemble_response(tresp(content=None))
2015-09-15 22:04:23 +00:00
def test_assemble_response_head():
c = assemble_response_head(tresp())
assert b"200" in c
assert b"svalue" in c
assert b"message" not in c
2015-09-17 00:39:42 +00:00
def test_assemble_body():
c = list(assemble_body(Headers(), [b"body"]))
assert c == [b"body"]
c = list(assemble_body(Headers(transfer_encoding="chunked"), [b"123456789a", b""]))
assert c == [b"a\r\n123456789a\r\n", b"0\r\n\r\n"]
c = list(assemble_body(Headers(transfer_encoding="chunked"), [b"123456789a"]))
assert c == [b"a\r\n123456789a\r\n", b"0\r\n\r\n"]
2015-09-15 22:04:23 +00:00
def test_assemble_request_line():
2015-09-25 22:39:04 +00:00
assert _assemble_request_line(treq().data) == b"GET /path HTTP/1.1"
2015-09-15 22:04:23 +00:00
2015-09-25 22:39:04 +00:00
authority_request = treq(method=b"CONNECT", first_line_format="authority").data
2015-09-15 22:04:23 +00:00
assert _assemble_request_line(authority_request) == b"CONNECT address:22 HTTP/1.1"
2015-09-25 22:39:04 +00:00
absolute_request = treq(first_line_format="absolute").data
2015-09-15 22:04:23 +00:00
assert _assemble_request_line(absolute_request) == b"GET http://address:22/path HTTP/1.1"
with raises(RuntimeError):
2015-09-25 22:39:04 +00:00
_assemble_request_line(treq(first_line_format="invalid_form").data)
2015-09-15 22:04:23 +00:00
def test_assemble_request_headers():
# https://github.com/mitmproxy/mitmproxy/issues/186
2015-09-25 22:39:04 +00:00
r = treq(content=b"")
r.headers["Transfer-Encoding"] = "chunked"
c = _assemble_request_headers(r.data)
assert b"Transfer-Encoding" in c
2015-09-15 22:04:23 +00:00
def test_assemble_request_headers_host_header():
r = treq()
r.headers = Headers()
c = _assemble_request_headers(r.data)
assert b"host" in c
r.host = None
c = _assemble_request_headers(r.data)
assert b"host" not in c
2015-09-15 22:04:23 +00:00
def test_assemble_response_headers():
# https://github.com/mitmproxy/mitmproxy/issues/186
2015-09-26 15:39:50 +00:00
r = tresp(content=b"")
r.headers["Transfer-Encoding"] = "chunked"
2015-09-15 22:04:23 +00:00
c = _assemble_response_headers(r)
2015-09-17 00:39:42 +00:00
assert b"Transfer-Encoding" in c