mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-04-08 03:01:12 -04:00
feat(rpc-builder): test requests params (#5267)
Co-authored-by: Roman Krasiuk <rokrassyuk@gmail.com>
This commit is contained in:
committed by
GitHub
parent
13fd00c117
commit
6c4a9cd1c3
@@ -1,5 +1,4 @@
|
||||
//! Standalone http tests
|
||||
|
||||
use crate::utils::{launch_http, launch_http_ws, launch_ws};
|
||||
use jsonrpsee::{
|
||||
core::{
|
||||
@@ -22,6 +21,8 @@ use reth_rpc_types::{
|
||||
trace::filter::TraceFilter, CallRequest, Filter, Index, PendingTransactionFilterKind,
|
||||
TransactionRequest,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use std::collections::HashSet;
|
||||
|
||||
fn is_unimplemented(err: Error) -> bool {
|
||||
@@ -34,6 +35,54 @@ fn is_unimplemented(err: Error) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents a builder for creating JSON-RPC requests.
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct RawRpcBuilder {
|
||||
endpoint: String,
|
||||
method: Option<String>,
|
||||
params: Vec<Value>,
|
||||
id: Option<i32>,
|
||||
}
|
||||
|
||||
impl RawRpcBuilder {
|
||||
/// Creates a new `RawRpcBuilder` with a given endpoint.
|
||||
fn new(endpoint: impl Into<String>) -> Self {
|
||||
Self { endpoint: endpoint.into(), method: None, params: Vec::new(), id: None }
|
||||
}
|
||||
|
||||
/// Sets the method name for the JSON-RPC request.
|
||||
fn method(mut self, method: impl Into<String>) -> Self {
|
||||
self.method = Some(method.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Adds a parameter to the JSON-RPC request.
|
||||
fn add_param<S: Serialize>(mut self, param: S) -> Self {
|
||||
self.params.push(serde_json::to_value(param).expect("Failed to serialize parameter"));
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the ID for the JSON-RPC request.
|
||||
fn set_id(mut self, id: i32) -> Self {
|
||||
self.id = Some(id);
|
||||
self
|
||||
}
|
||||
|
||||
/// Constructs the JSON-RPC request string based on the provided configurations.
|
||||
fn build(self) -> String {
|
||||
let method = self.method.unwrap_or_else(|| panic!("JSON-RPC method not set"));
|
||||
let id = self.id.unwrap_or_else(|| panic!("JSON-RPC id not set"));
|
||||
let params: Vec<String> = self.params.into_iter().map(|p| p.to_string()).collect();
|
||||
|
||||
format!(
|
||||
r#"{{"jsonrpc":"2.0","id":{},"method":"{}","params":[{}]}}"#,
|
||||
id,
|
||||
method,
|
||||
params.join(",")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
async fn test_filter_calls<C>(client: &C)
|
||||
where
|
||||
C: ClientT + SubscriptionClientT + Sync,
|
||||
@@ -458,3 +507,20 @@ async fn test_call_otterscan_functions_http_and_ws() {
|
||||
let client = handle.http_client().unwrap();
|
||||
test_basic_otterscan_calls(&client).await;
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_rpc_builder_basic() {
|
||||
let rpc_string = RawRpcBuilder::new("http://localhost:8545")
|
||||
.method("eth_getBalance")
|
||||
.add_param("0xaa00000000000000000000000000000000000000")
|
||||
.add_param("0x898753d8fdd8d92c1907ca21e68c7970abd290c647a202091181deec3f30a0b2")
|
||||
.set_id(1)
|
||||
.build();
|
||||
|
||||
let expected = r#"{"jsonrpc":"2.0","id":1,"method":"eth_getBalance","params":["0xaa00000000000000000000000000000000000000","0x898753d8fdd8d92c1907ca21e68c7970abd290c647a202091181deec3f30a0b2"]}"#;
|
||||
assert_eq!(rpc_string, expected, "RPC string did not match expected format.");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user