Esempio n. 1
0
def test_safe_call_method_not_found():
    response = safe_call(
        Request(method="nonexistant", id=1),
        Methods(ping),
        debug=True,
        serialize=default_serialize,
    )
    assert isinstance(response, MethodNotFoundResponse)
Esempio n. 2
0
def test_safe_call_invalid_args():
    response = safe_call(
        Request(method="ping", params=[1], id=1),
        Methods(ping),
        debug=True,
        serialize=default_serialize,
    )
    assert isinstance(response, InvalidParamsResponse)
Esempio n. 3
0
def test_safe_call_notification():
    response = safe_call(
        Request(method="ping", params=[], id=NOID),
        Methods(ping),
        extra=None,
        serialize=default_serialize,
    )
    assert isinstance(response, NotificationResponse)
Esempio n. 4
0
def test_safe_call_notification_failure():
    def fail():
        raise ValueError()

    response = safe_call(Request(method="foo"),
                         Methods(fail),
                         debug=True,
                         serialize=default_serialize)
    assert isinstance(response, NotificationResponse)
Esempio n. 5
0
def test_safe_call_success_response():
    response = safe_call(
        Request(method="ping", id=1),
        Methods(ping),
        debug=True,
        serialize=default_serialize,
    )
    assert isinstance(response, SuccessResponse)
    assert response.result == "pong"
    assert response.id == 1
def test_safe_call_api_error():
    def error():
        raise ApiError("Client Error", code=123, data={"data": 42})

    response = safe_call(Request(method="error", id=1), Methods(error), debug=True)
    assert isinstance(response, ErrorResponse)
    error_dict = response.deserialized()["error"]
    assert error_dict["message"] == "Client Error"
    assert error_dict["code"] == 123
    assert error_dict["data"] == {"data": 42}
def test_safe_call_api_error_minimal():
    def error():
        raise ApiError("Client Error")

    response = safe_call(Request(method="error", id=1), Methods(error), debug=True)
    assert isinstance(response, ErrorResponse)
    response_dict = response.deserialized()
    error_dict = response_dict["error"]
    assert error_dict["message"] == "Client Error"
    assert error_dict["code"] == 1
    assert "data" not in error_dict
Esempio n. 8
0
def test_safe_call_notification_failure():
    def fail():
        1 / 0

    response = safe_call(
        Request(method="fail", params=[], id=NOID),
        Methods(fail),
        extra=None,
        serialize=default_serialize,
    )
    assert isinstance(response, NotificationResponse)
Esempio n. 9
0
def test_safe_call_api_error_minimal():
    def error(context: Context):
        return ApiErrorResponse("Client Error", code=123, id=context.request.id)

    response = safe_call(
        Request(method="error", params=[], id=1),
        Methods(error),
        extra=None,
        serialize=default_serialize,
    )
    assert isinstance(response, ErrorResponse)
    response_dict = response.deserialized()
    error_dict = response_dict["error"]
    assert error_dict["message"] == "Client Error"
    assert error_dict["code"] == 123
    assert "data" not in error_dict
Esempio n. 10
0
def test_non_json_encodable_resonse():
    def method():
        return b"Hello, World"

    response = safe_call(
        Request(method="method", id=1),
        Methods(method),
        debug=False,
        serialize=default_serialize,
    )
    # response must be serializable here
    str(response)
    assert isinstance(response, ErrorResponse)
    response_dict = response.deserialized()
    error_dict = response_dict["error"]
    assert error_dict["message"] == "Server error"
    assert error_dict["code"] == -32000
    assert "data" not in error_dict
Esempio n. 11
0
def test_safe_call_notification():
    response = safe_call(Request(method="ping"),
                         Methods(ping),
                         debug=True,
                         serialize=default_serialize)
    assert isinstance(response, NotificationResponse)