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 28, 2024
1 parent 5047301 commit 4431b53
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 54 deletions.
4 changes: 2 additions & 2 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() }
}
4 changes: 1 addition & 3 deletions crates/starknet_integration_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ workspace = true
[dependencies]
anyhow.workspace = true
assert_matches.workspace = true
axum.workspace = true
blockifier.workspace = true
cairo-lang-starknet-classes.workspace = true
futures.workspace = true
Expand All @@ -24,7 +23,6 @@ papyrus_network = { workspace = true, features = ["testing"] }
papyrus_protobuf.workspace = true
papyrus_rpc.workspace = true
papyrus_storage = { workspace = true, features = ["testing"] }
reqwest.workspace = true
serde_json.workspace = true
starknet-types-core.workspace = true
starknet_api.workspace = true
Expand All @@ -33,7 +31,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
47 changes: 1 addition & 46 deletions crates/starknet_integration_tests/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@ use std::future::Future;
use std::net::SocketAddr;
use std::time::Duration;

use axum::body::Body;
use blockifier::context::ChainInfo;
use blockifier::test_utils::contracts::FeatureContract;
use blockifier::test_utils::CairoVersion;
use mempool_test_utils::starknet_api_test_utils::{
rpc_tx_to_json,
AccountId,
MultiAccountTransactionGenerator,
};
use mempool_test_utils::starknet_api_test_utils::{AccountId, MultiAccountTransactionGenerator};
use papyrus_consensus::config::ConsensusConfig;
use papyrus_network::network_manager::test_utils::create_network_config_connected_to_broadcast_channels;
use papyrus_network::network_manager::BroadcastTopicChannels;
use papyrus_protobuf::consensus::ProposalPart;
use papyrus_storage::StorageConfig;
use reqwest::{Client, Response};
use starknet_api::block::BlockNumber;
use starknet_api::contract_address;
use starknet_api::core::ContractAddress;
Expand All @@ -31,7 +25,6 @@ use starknet_gateway::config::{
StatefulTransactionValidatorConfig,
StatelessTransactionValidatorConfig,
};
use starknet_gateway_types::errors::GatewaySpecError;
use starknet_http_server::config::HttpServerConfig;
use starknet_sequencer_infra::test_utils::get_available_socket;
use starknet_sequencer_node::config::node_config::SequencerNodeConfig;
Expand Down Expand Up @@ -105,44 +98,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 4431b53

Please sign in to comment.