Esempio 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")
Esempio 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())
Esempio n. 3
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())
Esempio n. 4
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")
Esempio 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")
Esempio n. 6
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")
Esempio n. 7
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")
Esempio n. 8
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")