Unverified Commit c5d46eea authored by Niklas Adolfsson's avatar Niklas Adolfsson
Browse files

fix tests

parent 6e21fa00
Pipeline #200587 passed with stages
in 6 minutes and 1 second
......@@ -15,7 +15,6 @@ jsonrpsee-types = { path = "../types", version = "0.14.0" }
thiserror = "1"
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = { version = "1", features = ["raw_value"] }
http = "0.2.7"
tracing = "0.1.34"
# optional deps
......@@ -35,6 +34,7 @@ futures-timer = { version = "3", optional = true }
globset = { version = "0.4", optional = true }
lazy_static = { version = "1", optional = true }
unicase = { version = "2.6.0", optional = true }
http = { version = "0.2.7", optional = true }
[features]
default = []
......@@ -50,6 +50,7 @@ server = [
"tokio/sync",
"lazy_static",
"unicase",
"http",
]
client = ["futures-util/sink", "futures-channel/sink", "futures-channel/std"]
async-client = [
......
......@@ -39,9 +39,6 @@ pub mod error;
/// Traits
pub mod traits;
/// Middleware trait and implementation.
pub mod middleware;
cfg_http_helpers! {
pub mod http_helpers;
}
......@@ -49,6 +46,7 @@ cfg_http_helpers! {
cfg_server! {
pub mod id_providers;
pub mod server;
pub mod middleware;
}
cfg_client! {
......@@ -59,7 +57,6 @@ cfg_client! {
pub mod tracing;
pub use async_trait::async_trait;
pub use error::Error;
pub use http::HeaderMap;
/// JSON-RPC result.
pub type RpcResult<T> = std::result::Result<T, Error>;
......
......@@ -26,10 +26,11 @@
//! Middleware for `jsonrpsee` servers.
use http::HeaderMap;
use jsonrpsee_types::Params;
use std::net::SocketAddr;
pub use http::HeaderMap as Headers;
pub use jsonrpsee_types::Params;
/// Defines a middleware specifically for HTTP requests with callbacks during the RPC request life-cycle.
/// The primary use case for this is to collect timings for a larger metrics collection solution.
///
......@@ -41,7 +42,7 @@ pub trait HttpMiddleware: Send + Sync + Clone + 'static {
type Instant: std::fmt::Debug + Send + Sync + Copy;
/// Called when a new JSON-RPC request comes to the server.
fn on_request(&self, remote_addr: SocketAddr, headers: &HeaderMap) -> Self::Instant;
fn on_request(&self, remote_addr: SocketAddr, headers: &Headers) -> Self::Instant;
/// Called on each JSON-RPC method call, batch requests will trigger `on_call` multiple times.
fn on_call(&self, method_name: &str, params: Params);
......@@ -64,7 +65,7 @@ pub trait WsMiddleware: Send + Sync + Clone + 'static {
type Instant: std::fmt::Debug + Send + Sync + Copy;
/// Called when a new client connects
fn on_connect(&self, remote_addr: SocketAddr, headers: &http::HeaderMap);
fn on_connect(&self, remote_addr: SocketAddr, headers: &Headers);
/// Called when a new JSON-RPC request comes to the server.
fn on_request(&self) -> Self::Instant;
......@@ -85,7 +86,7 @@ pub trait WsMiddleware: Send + Sync + Clone + 'static {
impl HttpMiddleware for () {
type Instant = ();
fn on_request(&self, _: std::net::SocketAddr, _: &http::HeaderMap) -> Self::Instant {}
fn on_request(&self, _: std::net::SocketAddr, _: &Headers) -> Self::Instant {}
fn on_call(&self, _: &str, _: Params) {}
......@@ -97,7 +98,7 @@ impl HttpMiddleware for () {
impl WsMiddleware for () {
type Instant = ();
fn on_connect(&self, _: std::net::SocketAddr, _: &http::HeaderMap) {}
fn on_connect(&self, _: std::net::SocketAddr, _: &Headers) {}
fn on_request(&self) -> Self::Instant {}
......@@ -117,7 +118,7 @@ where
{
type Instant = (A::Instant, B::Instant);
fn on_connect(&self, remote_addr: std::net::SocketAddr, headers: &http::HeaderMap) {
fn on_connect(&self, remote_addr: std::net::SocketAddr, headers: &Headers) {
(self.0.on_connect(remote_addr, headers), self.1.on_connect(remote_addr, headers));
}
......@@ -152,7 +153,7 @@ where
{
type Instant = (A::Instant, B::Instant);
fn on_request(&self, remote_addr: std::net::SocketAddr, headers: &HeaderMap) -> Self::Instant {
fn on_request(&self, remote_addr: std::net::SocketAddr, headers: &Headers) -> Self::Instant {
(self.0.on_request(remote_addr, headers), self.1.on_request(remote_addr, headers))
}
......
......@@ -27,11 +27,10 @@
use std::net::SocketAddr;
use std::time::Instant;
use jsonrpsee::core::HeaderMap;
use jsonrpsee::core::{client::ClientT, middleware};
use jsonrpsee::core::client::ClientT;
use jsonrpsee::core::middleware::{self, Headers, Params};
use jsonrpsee::http_client::HttpClientBuilder;
use jsonrpsee::http_server::{HttpServerBuilder, HttpServerHandle, RpcModule};
use jsonrpsee::types::Params;
#[derive(Clone)]
struct Timings;
......@@ -39,7 +38,7 @@ struct Timings;
impl middleware::HttpMiddleware for Timings {
type Instant = Instant;
fn on_request(&self, remote_addr: SocketAddr, headers: &HeaderMap) -> Self::Instant {
fn on_request(&self, remote_addr: SocketAddr, headers: &Headers) -> Self::Instant {
println!("[Middleware::on_request] remote_addr {}, headers: {:?}", remote_addr, headers);
Instant::now()
}
......
......@@ -27,9 +27,8 @@
use std::net::SocketAddr;
use std::time::Instant;
use jsonrpsee::core::HeaderMap;
use jsonrpsee::core::{client::ClientT, middleware};
use jsonrpsee::types::Params;
use jsonrpsee::core::client::ClientT;
use jsonrpsee::core::middleware::{self, Headers, Params};
use jsonrpsee::ws_client::WsClientBuilder;
use jsonrpsee::ws_server::{RpcModule, WsServerBuilder};
......@@ -39,7 +38,7 @@ struct Timings;
impl middleware::WsMiddleware for Timings {
type Instant = Instant;
fn on_connect(&self, remote_addr: SocketAddr, headers: &HeaderMap) {
fn on_connect(&self, remote_addr: SocketAddr, headers: &Headers) {
println!("[Middleware::on_connect] remote_addr {}, headers: {:?}", remote_addr, headers);
}
......
......@@ -30,7 +30,7 @@ use std::net::SocketAddr;
use std::process::Command;
use std::time::Instant;
use jsonrpsee::core::{client::ClientT, middleware, HeaderMap};
use jsonrpsee::core::{client::ClientT, middleware, middleware::Headers};
use jsonrpsee::rpc_params;
use jsonrpsee::types::Params;
use jsonrpsee::ws_client::WsClientBuilder;
......@@ -43,7 +43,7 @@ struct Timings;
impl middleware::WsMiddleware for Timings {
type Instant = Instant;
fn on_connect(&self, remote_addr: SocketAddr, headers: &HeaderMap) {
fn on_connect(&self, remote_addr: SocketAddr, headers: &Headers) {
println!("[Timings::on_connect] remote_addr {}, headers: {:?}", remote_addr, headers);
}
......@@ -90,7 +90,7 @@ impl ThreadWatcher {
impl middleware::WsMiddleware for ThreadWatcher {
type Instant = isize;
fn on_connect(&self, remote_addr: SocketAddr, headers: &HeaderMap) {
fn on_connect(&self, remote_addr: SocketAddr, headers: &Headers) {
println!("[ThreadWatcher::on_connect] remote_addr {}, headers: {:?}", remote_addr, headers);
}
......
......@@ -101,9 +101,7 @@ impl<M> Builder<M> {
/// ```
/// use std::{time::Instant, net::SocketAddr};
///
/// use jsonrpsee_core::middleware::HttpMiddleware;
/// use jsonrpsee_core::HeaderMap;
/// use jsonrpsee_types::Params;
/// use jsonrpsee_core::middleware::{HttpMiddleware, Headers, Params};
/// use jsonrpsee_http_server::HttpServerBuilder;
///
/// #[derive(Clone)]
......@@ -114,7 +112,7 @@ impl<M> Builder<M> {
///
/// // Called once the HTTP request is received, it may be a single JSON-RPC call
/// // or batch.
/// fn on_request(&self, _remote_addr: SocketAddr, _headers: &HeaderMap) -> Instant {
/// fn on_request(&self, _remote_addr: SocketAddr, _headers: &Headers) -> Instant {
/// Instant::now()
/// }
///
......
......@@ -682,22 +682,37 @@ impl<M> Builder<M> {
/// ```
/// use std::{time::Instant, net::SocketAddr};
///
/// use jsonrpsee_core::middleware::Middleware;
/// use jsonrpsee_core::HeaderMap;
/// use jsonrpsee_core::middleware::{WsMiddleware, Headers, Params};
/// use jsonrpsee_ws_server::WsServerBuilder;
///
/// #[derive(Clone)]
/// struct MyMiddleware;
///
/// impl Middleware for MyMiddleware {
/// impl WsMiddleware for MyMiddleware {
/// type Instant = Instant;
///
/// fn on_request(&self, _remote_addr: SocketAddr, _headers: &HeaderMap) -> Instant {
/// fn on_connect(&self, remote_addr: SocketAddr, headers: &Headers) {
/// println!("[MyMiddleware::on_call] remote_addr: {}, headers: {:?}", remote_addr, headers);
/// }
///
/// fn on_request(&self) -> Self::Instant {
/// Instant::now()
/// }
///
/// fn on_result(&self, name: &str, success: bool, started_at: Instant) {
/// println!("Call to '{}' took {:?}", name, started_at.elapsed());
/// fn on_call(&self, method_name: &str, params: Params) {
/// println!("[MyMiddleware::on_call] method: '{}' params: {:?}", method_name, params);
/// }
///
/// fn on_result(&self, method_name: &str, success: bool, started_at: Self::Instant) {
/// println!("[MyMiddleware::on_result] '{}', worked? {}, time elapsed {:?}", method_name, success, started_at.elapsed());
/// }
///
/// fn on_response(&self, result: &str, started_at: Self::Instant) {
/// println!("[MyMiddleware::on_response] result: {}, time elapsed {:?}", result, started_at.elapsed());
/// }
///
/// fn on_disconnect(&self, remote_addr: SocketAddr) {
/// println!("[MyMiddleware::on_disconnect] remote_addr: {}", remote_addr);
/// }
/// }
///
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment