def test_push(self): self.client_conn = Connection(self.client_stream, client_side=True, on_response=self.record_src_event, on_push=self.record_src_event, on_unhandled=self.ignore_event) self.server_conn = Connection(self.server_stream, client_side=False, on_request=self.record_dest_event, on_unhandled=self.ignore_event) result_future = self.http_layer.process_and_return_context() self.client_conn.initiate_connection() self.server_conn.initiate_connection() self.client_conn.send_request( 1, HttpRequest(headers=[(":method", "GET"), (":path", "/"), ("aaa", "bbb")])) yield self.read_until_new_event(self.server_conn, self.dest_events) self.server_conn.send_pushed_stream( 1, 2, HttpRequest(headers=[(":method", "GET"), (":path", "/resource")])) yield self.read_until_new_event(self.client_conn, self.src_events) self.server_conn.send_response( 2, HttpResponse(headers=[(":status", "200"), ("aaa", "bbb")], body=b"ccc")) yield self.read_until_new_event(self.client_conn, self.src_events) self.client_stream.close() self.server_stream.close() yield result_future self.assertEqual(len(self.src_events), 2) stream_id, parent_stream_id, request = self.src_events[0] self.assertEqual(stream_id, 2) self.assertEqual(parent_stream_id, 1) self.assertEqual(request.method, "GET") self.assertEqual(request.path, "/resource") self.assertEqual(request.version, "HTTP/2") self.assertEqual( request.headers, HttpHeaders([(":method", "GET"), (":path", "/resource")])) stream_id, response = self.src_events[1] self.assertEqual(stream_id, 2) self.assertEqual(response.code, "200") self.assertEqual(response.version, "HTTP/2") self.assertEqual(response.headers, HttpHeaders([(":status", "200"), ("aaa", "bbb")]))
def test_on_info_response(self): client_conn = Connection(h11.CLIENT, self.client_stream, on_info_response=self.on_response) client_conn.send_request( HttpRequest(method="GET", path="/chat", version="HTTP/1.1", headers=[("Host", "localhost"), ("Upgrade", "websocket")])) server_conn = Connection(h11.SERVER, self.server_stream, on_request=self.on_request) yield server_conn.read_bytes() server_conn.send_info_response( HttpResponse(version="HTTP/1.1", code="101", reason="Protocol Upgrade", headers=[("Host", "localhost"), ("Upgrade", "websocket")])) yield client_conn.read_bytes() self.assertIsNotNone(self.response) self.assertEqual( self.response.headers, HttpHeaders([("host", "localhost"), ("upgrade", "websocket")])) self.assertEqual(self.response.code, "101") self.assertEqual(self.response.reason, "Protocol Upgrade") self.assertEqual(self.response.version, "HTTP/1.1")
def test_construct_with_dict(self): headers = HttpHeaders( dict(Host="localhost", Accept="application/xml", Yayaya="Yoyoyo")) self.assertEqual(len(headers), 3) self.assertEqual(headers["Host"], "localhost") self.assertEqual(headers["Accept"], "application/xml") self.assertEqual(headers["Yayaya"], "Yoyoyo")
def test_on_response(self): client_conn = Connection( self.client_stream, client_side=True, on_response=self.on_response, on_unhandled=mock.Mock()) client_conn.initiate_connection() client_conn.send_request( client_conn.get_next_available_stream_id(), HttpRequest(headers=[ (":method", "GET"), (":path", "/"), ("aaa", "bbb")])) server_conn = Connection( self.server_stream, client_side=False, on_request=self.on_request, on_unhandled=mock.Mock()) server_conn.initiate_connection() yield server_conn.read_bytes() server_conn.send_response( self.request[0], HttpResponse( headers=[(":status", "200"), ("aaa", "bbb")], body=b"ccc")) yield client_conn.read_bytes() self.assertIsNotNone(self.response) _, response = self.response self.assertEqual(response.headers, HttpHeaders([ (":status", "200"), ("aaa", "bbb")])) self.assertEqual(response.code, "200") self.assertEqual(response.version, "HTTP/2")
def test_on_request(self): client_conn = Connection(self.client_stream, client_side=True) client_conn.initiate_connection() client_conn.send_request( client_conn.get_next_available_stream_id(), HttpRequest(headers=[ (":method", "GET"), (":path", "/"), ("aaa", "bbb")])) server_conn = Connection( self.server_stream, client_side=False, on_request=self.on_request, on_settings=self.on_settings) server_conn.initiate_connection() yield server_conn.read_bytes() self.assertIsNotNone(self.request) _, request = self.request self.assertEqual(request.headers, HttpHeaders([ (":method", "GET"), (":path", "/"), ("aaa", "bbb")])) self.assertEqual(request.method, "GET") self.assertEqual(request.path, "/") self.assertEqual(request.version, "HTTP/2")
def test_on_pushed_stream(self): client_conn = Connection( self.client_stream, client_side=True, on_push=self.on_push, on_unhandled=mock.Mock()) client_conn.initiate_connection() client_conn.send_request( client_conn.get_next_available_stream_id(), HttpRequest(headers=[ (":method", "GET"), (":path", "/")])) server_conn = Connection( self.server_stream, client_side=False, on_request=self.on_request, on_unhandled=mock.Mock()) server_conn.initiate_connection() yield server_conn.read_bytes() stream_id, _ = self.request server_conn.send_pushed_stream( stream_id, 2, HttpRequest(headers=[ (":method", "GET"), (":path", "/resource")])) yield client_conn.read_bytes() self.assertIsNotNone(self.push) self.assertEqual(self.push["parent_stream_id"], 1) self.assertEqual(self.push["pushed_stream_id"], 2) self.assertEqual( self.push["request"].headers, HttpHeaders([ (":method", "GET"), (":path", "/resource")]))
def test_no_content_type(self): self.mock_formatters.first.match = mock.Mock(return_value=False) self.mock_formatters.second.match = mock.Mock(return_value=False) empty_headers = HttpHeaders([]) formatted_body = self.formatter.format_body("body", empty_headers) self.assertEqual(formatted_body, self.formatter.default("body")) self.mock_formatters.first.match.assert_called_with(empty_headers, "") self.mock_formatters.second.match.assert_called_with(empty_headers, "") self.assert_not_called(self.mock_formatters.first) self.assert_not_called(self.mock_formatters.second)
def test_on_request(self): client_conn = Connection(h11.CLIENT, self.client_stream) client_conn.send_request( HttpRequest(method="GET", path="/", headers=[("Host", "localhost")])) server_conn = Connection(h11.SERVER, self.server_stream, on_request=self.on_request) yield server_conn.read_bytes() self.assertIsNotNone(self.request) self.assertEqual(self.request.headers, HttpHeaders([("host", "localhost")])) self.assertEqual(self.request.method, "GET") self.assertEqual(self.request.path, "/") self.assertEqual(self.request.version, "HTTP/1.1")
def test_resp_deserialize(self): response = HttpResponse.deserialize({ "version": "1.1", "code": "200", "reason": "OK", "headers": [("Content-Type", "text/html")], "body": "body".encode("base64") }) self.assertEqual(response.version, "1.1") self.assertEqual(response.code, "200") self.assertEqual(response.reason, "OK") self.assertEqual(response.headers, HttpHeaders([("Content-Type", "text/html")])) self.assertEqual(response.body, "body")
def test_gzip(self): self.mock_formatters.first.match = mock.Mock(return_value=True) self.mock_formatters.first.format_tui = mock.Mock( return_value="formatted body") self.mock_formatters.first.format_console = mock.Mock( return_value="formatted body") headers = HttpHeaders([("content-type", "text/plain"), ("content-encoding", "gzip")]) formatted_body = self.formatter.format_body(_gzip_body("body"), headers) self.assertEqual(formatted_body, "formatted body") self.mock_formatters.first.match.assert_called_with( headers, "text/plain") self.mock_formatters.second.match.assert_not_called() self.assert_called(self.mock_formatters.first, "body") self.assert_not_called(self.mock_formatters.second)
def test_req_deserialize(self): request = HttpRequest.deserialize({ "version": "1.1", "method": "GET", "path": "/hello", "headers": [("Content-Type", "text/html")], "body": "body".encode("base64") }) self.assertEqual(request.version, "1.1") self.assertEqual(request.method, "GET") self.assertEqual(request.path, "/hello") self.assertEqual(request.headers, HttpHeaders([("Content-Type", "text/html")])) self.assertEqual(request.body, "body")
def test_req_with_priority_updated(self): self.client_conn = Connection(self.client_stream, client_side=True, on_unhandled=self.ignore_event) self.server_conn = Connection(self.server_stream, client_side=False, on_request=self.record_dest_event, on_unhandled=self.ignore_event) result_future = self.http_layer.process_and_return_context() self.client_conn.initiate_connection() self.server_conn.initiate_connection() self.client_conn.send_request( 1, HttpRequest(headers=[(":method", "GET"), (":path", "/"), ("aaa", "bbb")]), priority_weight=10, priority_depends_on=0, priority_exclusive=False) yield self.read_until_new_event(self.server_conn, self.dest_events) self.client_stream.close() self.server_stream.close() yield result_future self.assertEqual(len(self.dest_events), 1) stream_id, request, priority_updated = self.dest_events[0] self.assertEqual(stream_id, 1) self.assertEqual(request.method, "GET") self.assertEqual(request.path, "/") self.assertEqual(request.version, "HTTP/2") self.assertEqual( request.headers, HttpHeaders([(":method", "GET"), (":path", "/"), ("aaa", "bbb")])) self.assertIsNotNone(priority_updated) self.assertEqual(priority_updated.weight, 10) self.assertEqual(priority_updated.exclusive, False) self.assertEqual(priority_updated.depends_on, 0)
def test_replay(self): self.http_layer.context.mode = "replay" self.client_conn = Connection(self.client_stream, client_side=True, on_unhandled=self.ignore_event) self.server_conn = Connection(self.server_stream, client_side=False, on_request=self.record_dest_event, on_unhandled=self.ignore_event) result_future = self.http_layer.process_and_return_context() self.client_conn.initiate_connection() self.server_conn.initiate_connection() self.client_conn.send_request( 1, HttpRequest(headers=[(":method", "GET"), (":path", "/"), ("aaa", "bbb")])) yield self.read_until_new_event(self.server_conn, self.dest_events) self.server_conn.send_response( 1, HttpResponse(headers=[(":status", "200"), ("aaa", "bbb")], body=b"ccc")) result = yield result_future self.assertIsNotNone(result) self.assertIsInstance(result, LayerContext) self.assertEqual(len(self.dest_events), 1) stream_id, request, _ = self.dest_events[0] self.assertEqual(stream_id, 1) self.assertEqual(request.method, "GET") self.assertEqual(request.path, "/") self.assertEqual(request.version, "HTTP/2") self.assertEqual( request.headers, HttpHeaders([(":method", "GET"), (":path", "/"), ("aaa", "bbb")]))
def test_on_response(self): server_conn = Connection(h11.SERVER, self.server_stream) server_conn.send_response( HttpResponse(version="HTTP/1.1", code="200", reason="OK", headers=[("Host", "localhost"), ("Content-Length", "1")], body=b"A")) client_conn = Connection(h11.CLIENT, self.client_stream, on_response=self.on_response) yield client_conn.read_bytes() self.assertIsNotNone(self.response) self.assertEqual( self.response.headers, HttpHeaders([("host", "localhost"), ("content-length", "1")])) self.assertEqual(self.response.code, "200") self.assertEqual(self.response.reason, "OK") self.assertEqual(self.response.version, "HTTP/1.1") self.assertEqual(self.response.body, b"A")
def test_construct_failed(self): with self.assertRaises(ValueError): HttpHeaders("aaa")
def test_neq(self): self.assertNotEqual(self.headers, HttpHeaders([]))
def setUp(self): self.headers = HttpHeaders([("Host", "localhost"), ("Accept", "application/xml"), ("Yayaya", "Yoyoyo")])
def test_eq(self): self.assertEqual( self.headers, HttpHeaders([("Host", "localhost"), ("Accept", "application/xml"), ("Yayaya", "Yoyoyo")]))
class TestFormatterMixin(object): DEFAULT_HEADERS = HttpHeaders([("content-type", "text/plain; charset=utf-8")]) def setUp(self): self.mock_formatters = mock.Mock() self.formatters = [ self.mock_formatters.first, self.mock_formatters.second ] self.formatter = None def assert_called(self, formatter, *args, **kwargs): raise NotImplementedError def assert_not_called(self, formatter): raise NotImplementedError def test_match_first(self): self.mock_formatters.first.match = mock.Mock(return_value=True) self.mock_formatters.first.format_tui = mock.Mock( return_value="formatted body") self.mock_formatters.first.format_console = mock.Mock( return_value="formatted body") formatted_body = self.formatter.format_body("body", self.DEFAULT_HEADERS) self.assertEqual(formatted_body, "formatted body") self.mock_formatters.first.match.assert_called_with( self.DEFAULT_HEADERS, "text/plain") self.mock_formatters.second.match.assert_not_called() self.assert_called(self.mock_formatters.first, "body") self.assert_not_called(self.mock_formatters.second) def test_match_second(self): self.mock_formatters.first.match = mock.Mock(return_value=False) self.mock_formatters.second.match = mock.Mock(return_value=True) self.mock_formatters.second.format_tui = mock.Mock( return_value="formatted body") self.mock_formatters.second.format_console = mock.Mock( return_value="formatted body") formatted_body = self.formatter.format_body("body", self.DEFAULT_HEADERS) self.assertEqual(formatted_body, "formatted body") self.mock_formatters.first.match.assert_called_with( self.DEFAULT_HEADERS, "text/plain") self.mock_formatters.second.match.assert_called_with( self.DEFAULT_HEADERS, "text/plain") self.assert_not_called(self.mock_formatters.first) self.assert_called(self.mock_formatters.second, "body") def test_no_match(self): self.mock_formatters.first.match = mock.Mock(return_value=False) self.mock_formatters.second.match = mock.Mock(return_value=False) formatted_body = self.formatter.format_body("body", self.DEFAULT_HEADERS) self.assertEqual(formatted_body, self.formatter.default("body")) self.mock_formatters.first.match.assert_called_with( self.DEFAULT_HEADERS, "text/plain") self.mock_formatters.second.match.assert_called_with( self.DEFAULT_HEADERS, "text/plain") self.assert_not_called(self.mock_formatters.first) self.assert_not_called(self.mock_formatters.second) def test_match_but_format_format(self): self.mock_formatters.first.match = mock.Mock(return_value=True) self.mock_formatters.second.match = mock.Mock(return_value=False) self.mock_formatters.first.format_tui = mock.Mock( side_effect=ValueError) self.mock_formatters.first.format_console = mock.Mock( side_effect=ValueError) formatted_body = self.formatter.format_body("body", self.DEFAULT_HEADERS) self.assertEqual(formatted_body, self.formatter.default("body")) self.mock_formatters.first.match.assert_called_with( self.DEFAULT_HEADERS, "text/plain") self.mock_formatters.second.match.assert_called_with( self.DEFAULT_HEADERS, "text/plain") self.assert_called(self.mock_formatters.first, "body") self.assert_not_called(self.mock_formatters.second) def test_gzip(self): self.mock_formatters.first.match = mock.Mock(return_value=True) self.mock_formatters.first.format_tui = mock.Mock( return_value="formatted body") self.mock_formatters.first.format_console = mock.Mock( return_value="formatted body") headers = HttpHeaders([("content-type", "text/plain"), ("content-encoding", "gzip")]) formatted_body = self.formatter.format_body(_gzip_body("body"), headers) self.assertEqual(formatted_body, "formatted body") self.mock_formatters.first.match.assert_called_with( headers, "text/plain") self.mock_formatters.second.match.assert_not_called() self.assert_called(self.mock_formatters.first, "body") self.assert_not_called(self.mock_formatters.second) def test_no_content_type(self): self.mock_formatters.first.match = mock.Mock(return_value=False) self.mock_formatters.second.match = mock.Mock(return_value=False) empty_headers = HttpHeaders([]) formatted_body = self.formatter.format_body("body", empty_headers) self.assertEqual(formatted_body, self.formatter.default("body")) self.mock_formatters.first.match.assert_called_with(empty_headers, "") self.mock_formatters.second.match.assert_called_with(empty_headers, "") self.assert_not_called(self.mock_formatters.first) self.assert_not_called(self.mock_formatters.second)