Skip to content

Commit

Permalink
chore(tests_integration): move http client test util
Browse files Browse the repository at this point in the history
commit-id:2d145538
  • Loading branch information
Itay-Tsabary-Starkware committed Nov 27, 2024
1 parent 18e06a7 commit 0da3e11
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 42 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/starknet_http_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ edition.workspace = true
license.workspace = true
repository.workspace = true

[features]
testing = ["mempool_test_utils", "reqwest"]

[lints]
workspace = true

[dependencies]
axum.workspace = true
hyper.workspace = true
mempool_test_utils = { workspace = true, optional = true }
papyrus_config.workspace = true
reqwest = { workspace = true, optional = true }
serde.workspace = true
starknet_api.workspace = true
starknet_gateway_types.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions crates/starknet_http_server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ pub mod communication;
pub mod config;
pub mod errors;
pub mod http_server;
#[cfg(feature = "testing")]
pub mod test_utils;
55 changes: 55 additions & 0 deletions crates/starknet_http_server/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::net::SocketAddr;

use axum::body::Body;
use mempool_test_utils::starknet_api_test_utils::rpc_tx_to_json;
use reqwest::{Client, Response};
use starknet_api::rpc_transaction::RpcTransaction;
use starknet_api::transaction::TransactionHash;
use starknet_gateway_types::errors::GatewaySpecError;
use starknet_sequencer_infra::test_utils::get_available_socket;

use crate::config::HttpServerConfig;

/// A test utility client for interacting with an http server.
pub struct HttpTestClient {
socket: SocketAddr,
client: Client,
}

impl HttpTestClient {
pub fn new(socket: SocketAddr) -> Self {
let client = Client::new();
Self { socket, client }
}

pub async fn assert_add_tx_success(&self, rpc_tx: RpcTransaction) -> TransactionHash {
let response = self.add_tx(rpc_tx).await;
assert!(response.status().is_success());

response.json().await.unwrap()
}

// TODO: implement when usage eventually arises.
pub async fn assert_add_tx_error(&self, _tx: RpcTransaction) -> GatewaySpecError {
todo!()
}

// Prefer using assert_add_tx_success or other higher level methods of this client, to ensure
// tests are boilerplate and implementation-detail free.
pub async fn add_tx(&self, rpc_tx: RpcTransaction) -> Response {
let tx_json = rpc_tx_to_json(&rpc_tx);
self.client
.post(format!("http://{}/add_tx", self.socket))
.header("content-type", "application/json")
.body(Body::from(tx_json))
.send()
.await
.unwrap()
}
}

pub async fn create_http_server_config() -> HttpServerConfig {
// TODO(Tsabary): use ser_generated_param.
let socket = get_available_socket().await;
HttpServerConfig { ip: socket.ip(), port: socket.port() }
}
2 changes: 1 addition & 1 deletion crates/starknet_integration_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ starknet_client.workspace = true
starknet_consensus_manager.workspace = true
starknet_gateway = { workspace = true, features = ["testing"] }
starknet_gateway_types.workspace = true
starknet_http_server.workspace = true
starknet_http_server = { workspace = true, features = ["testing"] }
starknet_mempool_p2p.workspace = true
starknet_monitoring_endpoint = { workspace = true, features = ["testing"] }
starknet_sequencer_infra = { workspace = true, features = ["testing"] }
Expand Down
3 changes: 2 additions & 1 deletion crates/starknet_integration_tests/src/flow_test_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use starknet_api::rpc_transaction::RpcTransaction;
use starknet_api::transaction::TransactionHash;
use starknet_gateway_types::errors::GatewaySpecError;
use starknet_http_server::config::HttpServerConfig;
use starknet_http_server::test_utils::HttpTestClient;
use starknet_sequencer_infra::trace_util::configure_tracing;
use starknet_sequencer_node::servers::run_component_servers;
use starknet_sequencer_node::utils::create_node_modules;
Expand All @@ -16,7 +17,7 @@ use tokio::runtime::Handle;
use tokio::task::JoinHandle;

use crate::state_reader::{spawn_test_rpc_state_reader, StorageTestSetup};
use crate::utils::{create_chain_info, create_config, HttpTestClient};
use crate::utils::{create_chain_info, create_config};

pub struct FlowTestSetup {
pub task_executor: TokioExecutor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use std::path::PathBuf;
use mempool_test_utils::starknet_api_test_utils::MultiAccountTransactionGenerator;
use papyrus_storage::StorageConfig;
use starknet_http_server::config::HttpServerConfig;
use starknet_http_server::test_utils::HttpTestClient;
use starknet_monitoring_endpoint::config::MonitoringEndpointConfig;
use starknet_monitoring_endpoint::test_utils::IsAliveClient;
use tempfile::{tempdir, TempDir};

use crate::config_utils::dump_config_file_changes;
use crate::state_reader::{spawn_test_rpc_state_reader, StorageTestSetup};
use crate::utils::{create_chain_info, create_config, HttpTestClient};
use crate::utils::{create_chain_info, create_config};

pub struct IntegrationTestSetup {
// Client for adding transactions to the sequencer node.
Expand Down
38 changes: 0 additions & 38 deletions crates/starknet_integration_tests/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,44 +105,6 @@ pub fn test_rpc_state_reader_config(rpc_server_addr: SocketAddr) -> RpcStateRead
}
}

/// A test utility client for interacting with an http server.
pub struct HttpTestClient {
socket: SocketAddr,
client: Client,
}

impl HttpTestClient {
pub fn new(socket: SocketAddr) -> Self {
let client = Client::new();
Self { socket, client }
}

pub async fn assert_add_tx_success(&self, rpc_tx: RpcTransaction) -> TransactionHash {
let response = self.add_tx(rpc_tx).await;
assert!(response.status().is_success());

response.json().await.unwrap()
}

// TODO: implement when usage eventually arises.
pub async fn assert_add_tx_error(&self, _tx: RpcTransaction) -> GatewaySpecError {
todo!()
}

// Prefer using assert_add_tx_success or other higher level methods of this client, to ensure
// tests are boilerplate and implementation-detail free.
pub async fn add_tx(&self, rpc_tx: RpcTransaction) -> Response {
let tx_json = rpc_tx_to_json(&rpc_tx);
self.client
.post(format!("http://{}/add_tx", self.socket))
.header("content-type", "application/json")
.body(Body::from(tx_json))
.send()
.await
.unwrap()
}
}

/// Creates a multi-account transaction generator for integration tests.
pub fn create_integration_test_tx_generator() -> MultiAccountTransactionGenerator {
let mut tx_generator: MultiAccountTransactionGenerator =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use papyrus_protobuf::mempool::RpcTransactionWrapper;
use rstest::{fixture, rstest};
use starknet_api::rpc_transaction::RpcTransaction;
use starknet_http_server::config::HttpServerConfig;
use starknet_http_server::test_utils::HttpTestClient;
use starknet_integration_tests::state_reader::{spawn_test_rpc_state_reader, StorageTestSetup};
use starknet_integration_tests::utils::{
create_batcher_config,
Expand All @@ -18,7 +19,6 @@ use starknet_integration_tests::utils::{
create_integration_test_tx_generator,
run_integration_test_scenario,
test_rpc_state_reader_config,
HttpTestClient,
};
use starknet_mempool_p2p::config::MempoolP2pConfig;
use starknet_mempool_p2p::MEMPOOL_TOPIC;
Expand Down

0 comments on commit 0da3e11

Please sign in to comment.