Skip to content

Commit

Permalink
Reproduce error object result for batched response in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tadovas committed Jun 2, 2023
1 parent 07bf7ad commit b5f728e
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/transports/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ fn id_of_output(output: &Output) -> Result<RequestId> {
#[cfg(test)]
mod tests {
use super::*;
use crate::Error::Rpc;
use jsonrpc_core::ErrorCode;

async fn server(req: hyper::Request<hyper::Body>) -> hyper::Result<hyper::Response<hyper::Body>> {
use hyper::body::HttpBody;
Expand Down Expand Up @@ -219,6 +221,51 @@ mod tests {
assert_eq!(response, Ok(Value::String("x".into())));
}

#[tokio::test]
async fn catch_generic_json_error_for_batched_request() {
use hyper::service::{make_service_fn, service_fn};

async fn handler(_req: hyper::Request<hyper::Body>) -> hyper::Result<hyper::Response<hyper::Body>> {
let response = r#"{
"jsonrpc":"2.0",
"error":{
"code":0,
"message":"we can't execute this request"
},
"id":null
}"#;
Ok(hyper::Response::<hyper::Body>::new(response.into()))
}

// given
let addr = "127.0.0.1:3001";
// start server
let service = make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(handler)) });
let server = hyper::Server::bind(&addr.parse().unwrap()).serve(service);
tokio::spawn(async move {
println!("Listening on http://{}", addr);
server.await.unwrap();
});

// when
let client = Http::new(&format!("http://{}", addr)).unwrap();
println!("Sending request");
let response = client
.send_batch(vec![client.prepare("some_method", vec![])].into_iter())
.await;
println!("Got response");

// then
assert_eq!(
response,
Err(Rpc(crate::rpc::error::Error {
code: ErrorCode::ServerError(0),
message: "we can't execute this request".to_string(),
data: None,
}))
);
}

#[test]
fn handles_batch_response_being_in_different_order_than_input() {
let ids = vec![0, 1, 2];
Expand Down

0 comments on commit b5f728e

Please sign in to comment.