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_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_req_serialize(self): http_message = HttpRequest(version="1.1", method="GET", path="/hello", headers=[("Content-Type", "text/html")], body="body") json = http_message.serialize() self.assertEqual(json["version"], "1.1") self.assertEqual(json["method"], "GET") self.assertEqual(json["path"], "/hello") self.assertEqual(json["headers"], [("Content-Type", "text/html")]) self.assertEqual(json["body"], "body".encode("base64"))
def test_simple_request(self): request = HttpRequest(headers=[("Host", "github.com")]) expected = TextList([ ColorText("Request Headers:", fg_color="blue", attrs=["bold"]), Header([("Host", "github.com")]) ]) self.assertEqual(expected.__dict__, Request(request).__dict__)
def test_safe_mapping_id(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.assertEqual( self.http_layer.safe_mapping_id(self.http_layer.src_to_dest_ids, 1), 0) 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.assertEqual( self.http_layer.safe_mapping_id(self.http_layer.src_to_dest_ids, 1), 1) self.client_stream.close() self.server_stream.close() yield result_future
def test_construct_color_msg_with_header(self): msg = construct_color_msg( ViewerContext.deserialize({ "host": "example.com", "path": "/index", "response": { "code": 200, "headers": [ ("Content-Type", "application/xml"), ], "body": "response".encode("base64"), }, "request": { "method": "GET", "headers": [ ("Content-Type", "application/xml"), ], }, }), "header") self.assertEqual( msg, TextList([ StatusText(200, "GET", "example.com", "/index"), Request( HttpRequest(method="GET", headers=[("Content-Type", "application/xml") ])), Response( HttpResponse(code=200, headers=[("Content-Type", "application/xml") ])) ]))
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_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 handle_request(self, event): headers_dict = dict(event.headers) self.ongoings_streams[event.stream_id] = (HttpRequest( version=self._VERSION, method=headers_dict[":method"], path=headers_dict[":path"], headers=event.headers), [], event.priority_updated)
def test_on_connection_closed(self): client_conn = Connection(h11.CLIENT, self.client_stream, on_response=self.on_response) client_conn.send_request( HttpRequest(method="GET", path="/", headers=[("Host", "localhost"), ("Connection", "close")])) server_conn = Connection(h11.SERVER, self.server_stream, on_request=self.on_request) yield server_conn.read_bytes() server_conn.send_response( HttpResponse(version="HTTP/1.1", code="200", reason="OK", headers=[("Host", "localhost"), ("Content-Length", "4")], body=b"Yaya")) yield client_conn.read_bytes() self.assertTrue(self.client_stream.closed()) self.assertTrue(self.server_stream.closed())
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_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_priority_updates=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")])) self.client_conn.send_priority_updates(1, 0, 10, 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) self.assertEqual(self.dest_events[0], (1, 0, 10, False))
def handle_pushed_stream(self, event): headers_dict = dict(event.headers) request = HttpRequest(version=self._VERSION, method=headers_dict[":method"], path=headers_dict[":path"], headers=event.headers) self.on_push(event.pushed_stream_id, event.parent_stream_id, request)
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_readonly(self): client_conn = Connection(self.client_stream, client_side=True, readonly=True) client_conn.initiate_connection() client_conn.send_request( client_conn.get_next_available_stream_id(), HttpRequest(headers=[ (":method", "GET"), (":path", "/"), ("aaa", "bbb")])) with self.assertRaises(gen.TimeoutError): yield gen.with_timeout( timedelta(milliseconds=100), self.server_stream.read_bytes(1))
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_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_on_reset(self): client_conn = Connection( self.client_stream, client_side=True, on_reset=self.on_reset, 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()) yield server_conn.read_bytes() stream_id, _ = self.request server_conn.send_reset(stream_id, 2) yield client_conn.read_bytes() self.assertIsNotNone(self.reset) self.assertEqual(self.reset, (stream_id, 2))
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_priority_updates(self): client_conn = Connection( self.client_stream, client_side=True, on_unhandled=mock.Mock()) client_conn.initiate_connection() stream_id = client_conn.get_next_available_stream_id() client_conn.send_request( stream_id, HttpRequest(headers=[ (":method", "GET"), (":path", "/"), ("aaa", "bbb")])) client_conn.send_priority_updates( stream_id, 0, 10, False) server_conn = Connection( self.server_stream, client_side=False, on_priority_updates=self.on_priority_updates, on_unhandled=mock.Mock()) server_conn.initiate_connection() yield server_conn.read_bytes() self.assertIsNotNone(self.priority_updates) self.assertEqual( self.priority_updates, dict(stream_id=stream_id, depends_on=0, weight=10, exclusive=False))