Example #1
0
 def test_502(self):
     client = HttpClient("localhost", "example")
     client.connect()
     client.login("test")
     transport_response = self._load_response("tests/data/http_502.txt")
     client.receive(transport_response)
     response = client.next_response()
     assert response.status_code == 502
Example #2
0
def connect(cli):
    context = ssl.create_default_context()

    if cli.ssl_insecure:
        context.check_hostname = False
        context.verify_mode = ssl.CERT_NONE

    context.set_alpn_protocols(["h2", "http/1.1"])

    try:
        context.set_npn_protocols(["h2", "http/1.1"])
    except NotImplementedError:
        pass

    sock = socks.socksocket()

    if cli.proxy:
        sock.set_proxy(cli.proxy_type, cli.proxy, cli.proxy_port)

    try:
        sock.connect((cli.host, cli.port))
    except socket.error as e:
        raise SystemExit(e)

    try:
        ssl_socket = context.wrap_socket(sock, server_hostname=cli.host)
    except (ssl.SSLError, socket.error) as e:
        raise SystemExit(e)

    negotiated_protocol = ssl_socket.selected_alpn_protocol()
    if negotiated_protocol is None:
        negotiated_protocol = ssl_socket.selected_npn_protocol()

    transport_type = None

    if negotiated_protocol == "http/1.1":
        transport_type = TransportType.HTTP
    elif negotiated_protocol == "h2":
        transport_type = TransportType.HTTP2
    else:
        raise NotImplementedError

    client = HttpClient(cli.host, cli.user)
    data = client.connect(transport_type)

    try:
        ssl_socket.sendall(data)
    except socket.error as e:
        raise SystemExit(e)

    return ssl_socket, client
Example #3
0
    def test_client_lag(self, frame_factory):
        client = HttpClient("localhost", "example")
        client.connect(TransportType.HTTP2)
        response = Http2Response()
        response.send_time = 0
        response.receive_time = 30
        response.timeout = 25 * 1000

        response2 = Http2Response()
        response2.send_time = 0
        response2.receive_time = 31
        response2.timeout = 25 * 1000

        client.connection._responses[response.uuid] = response
        client.connection._responses[response2.uuid] = response2
        typed_response = RequestInfo("sync", 25 * 1000)
        client.requests_made[response.uuid] = typed_response

        assert client.lag == 6
Example #4
0
    def test_frame_splitting(self, frame_factory):
        client = HttpClient("localhost", "example")
        data = client.connect(TransportType.HTTP2)
        client.connection._connection.outbound_flow_control_window = 5
        uuid, request = client.login("wordpass")

        assert client.connection._data_to_send

        to_send = data + request

        while to_send:
            f = frame_factory.build_window_update_frame(
                stream_id=0,
                increment=5,
            )
            client.receive(f.serialize())
            to_send = client.data_to_send()

        assert not client.connection._data_to_send
Example #5
0
    def test_client_receive(self, frame_factory):
        client = HttpClient("localhost", "example")
        client.connect(TransportType.HTTP2)
        uuid, request = client.login("wordpass")

        conf = h2.config.H2Configuration(client_side=True)

        server = h2.connection.H2Connection(conf)
        server.initiate_connection()
        server.receive_data(frame_factory.preamble())

        events = server.receive_data(request)
        # assert events[0].headers == []

        client.receive(self.login_response(1, frame_factory))
        response = client.next_response()

        assert isinstance(response, LoginResponse)
        assert response.uuid == uuid

        uuid, request = client.sync()

        events = server.receive_data(request)

        client.receive(self.sync_response(3, frame_factory))
        response = client.next_response()

        assert isinstance(response, SyncResponse)
        assert response.uuid == uuid

        sync_uuid, request = client.sync()

        server.receive_data(request)

        content = {"body": "test", "msgtype": "m.text"}

        send_uuid, send_request = client.room_send("!test:localhost",
                                                   "m.room.message", content)
Example #6
0
    def test_client_local_error(self, frame_factory):
        client = HttpClient("localhost", "example")

        with pytest.raises(LocalProtocolError):
            uuid, request = client.login("wordpass")

        client.connect(TransportType.HTTP2)
        uuid, request = client.login("wordpass")

        with pytest.raises(LocalProtocolError):
            uuid, request = client.sync()

        client.receive(self.login_response(1, frame_factory))
        client.next_response()
        uuid, request = client.sync()