Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test coverage #1

Open
vlopes11 opened this issue Apr 16, 2019 · 5 comments

Comments

Projects
None yet
2 participants
@vlopes11
Copy link
Owner

commented Apr 16, 2019

Create a basic test coverage, considering the examples in JSON-RPC official specification
https://www.jsonrpc.org/specification

rpc call with positional parameters:

--> {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}
<-- {"jsonrpc": "2.0", "result": 19, "id": 1}

--> {"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2}
<-- {"jsonrpc": "2.0", "result": -19, "id": 2}

rpc call with named parameters:

--> {"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}
<-- {"jsonrpc": "2.0", "result": 19, "id": 3}

--> {"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4}
<-- {"jsonrpc": "2.0", "result": 19, "id": 4}

a Notification:

--> {"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]}
--> {"jsonrpc": "2.0", "method": "foobar"}

rpc call of non-existent method:

--> {"jsonrpc": "2.0", "method": "foobar", "id": "1"}
<-- {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "1"}

rpc call with invalid JSON:

--> {"jsonrpc": "2.0", "method": "foobar, "params": "bar", "baz]
<-- {"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}

rpc call with invalid Request object:

--> {"jsonrpc": "2.0", "method": 1, "params": "bar"}
<-- {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}

rpc call Batch, invalid JSON:

--> [
  {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
  {"jsonrpc": "2.0", "method"
]
<-- {"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}

rpc call with an empty Array:

--> []
<-- {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}

rpc call with an invalid Batch (but not empty):

--> [1]
<-- [
  {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
]

rpc call with invalid Batch:

--> [1,2,3]
<-- [
  {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},
  {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},
  {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
]

rpc call Batch:

--> [
        {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
        {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
        {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"},
        {"foo": "boo"},
        {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"},
        {"jsonrpc": "2.0", "method": "get_data", "id": "9"} 
    ]
<-- [
        {"jsonrpc": "2.0", "result": 7, "id": "1"},
        {"jsonrpc": "2.0", "result": 19, "id": "2"},
        {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},
        {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "5"},
        {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"}
    ]

rpc call Batch (all notifications):

--> [
        {"jsonrpc": "2.0", "method": "notify_sum", "params": [1,2,4]},
        {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}
    ]
<-- //Nothing is returned for all notification batches
@vladignatyev

This comment has been minimized.

Copy link

commented Apr 19, 2019

@vlopes11 will work on it. Great project btw

@vlopes11

This comment has been minimized.

Copy link
Owner Author

commented Apr 19, 2019

Ty man! If you need any support, just let me know :)

@vladignatyev

This comment has been minimized.

Copy link

commented Apr 28, 2019

My first tests...

vladignatyev@e3fbef0

I need explanation or an example how to get data from JrpcResponse. Can you help @vlopes11 ?

@vlopes11

This comment has been minimized.

Copy link
Owner Author

commented Apr 28, 2019

Hello!

Sure, here is a minimal working example with unwraps and no error handling.

You can replace the second parameter of generate_method_with_data_and_future, in this example i64, for anything (Arc's, tuples, custom structs, etc)

use futures_jsonrpc::futures::prelude::*;
use futures_jsonrpc::*;
use serde_json::json;

generate_method_with_data_and_future!(MathMultiplyByNumber, i64, impl Future for MathMultiplyByNumber {
    type Item = Option<JrpcResponse>;
    type Error = ErrorVariant;

    fn poll(&mut self) -> Result<Async<Self::Item>, Self::Error> {
        let request = self.get_request()?;
        let multiplier = self.get_data();
        let params = request.get_params().clone().unwrap();

        let value = params["value"].as_i64().unwrap();
        let value = value * multiplier;

        let message = JrpcResponseParam::generate_result(
                json!({"result": value})
            ).and_then(|result| request.generate_response(result))?;

        Ok(Async::Ready(Some(message)))
    }
});

fn main() {
    let handler = JrpcHandler::new().unwrap();

    handler
        .register_method(
            "math/multiplyByNumber",
            // Here you send the data via constructor
            MathMultiplyByNumber::new(42).unwrap(),
        )
        .and_then(|h| {
            h.handle_message(
                r#"
                {
                    "jsonrpc": "2.0",
                    "method": "math/multiplyByNumber",
                    "params": {
                        "value": 25
                    },
                    "id": 1
                }"#,
            )
        })
        .and_then(|future| future.wait())
        .and_then(|response| {
            let response = response.unwrap();

            if let Some(r) = response.get_result() {
                let value = &r["result"].as_i64().unwrap();
                dbg!(value);
            }

            Ok(())
        })
        .unwrap();
}
@vladignatyev

This comment has been minimized.

Copy link

commented Apr 28, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.