Exemplo n.º 1
0
    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")
Exemplo n.º 2
0
    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())
Exemplo n.º 3
0
    def test_parse_version(self):
        self.assertEqual(
            Connection(h11.CLIENT, None)._parse_version(None), "HTTP/1.1")

        http_content = mock.Mock()
        http_content.http_version = "1.1"
        self.assertEqual(
            Connection(h11.CLIENT, None)._parse_version(http_content),
            "HTTP/1.1")
Exemplo n.º 4
0
 def __init__(self, server_state, context):
     super(Http1Layer, self).__init__(server_state, context)
     self.src_conn = Connection(h11.SERVER,
                                self.src_stream,
                                conn_type="src",
                                readonly=(context.mode == "replay"),
                                on_request=self.on_request)
     self.dest_conn = Connection(h11.CLIENT,
                                 self.dest_stream,
                                 conn_type="dest",
                                 on_response=self.on_response,
                                 on_info_response=self.on_info_response)
     self.req = None
     self.resp = None
     self.switch_protocol = False
Exemplo n.º 5
0
    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")
Exemplo n.º 6
0
    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")