Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add routes test (semi-integration test) #28

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ starknet_api = { git = "https://github.com/starkware-libs/starknet-api.git", rev
papyrus_config = "0.3.0"
thiserror = "1.0"
tokio = { version = "1", features = ["full"] }
tower = "0.4.13"
validator = "0.12"

3 changes: 2 additions & 1 deletion crates/gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ workspace = true
axum.workspace = true
clap.workspace = true
hyper.workspace = true
papyrus_config.workspace = true
serde.workspace = true
serde_json.workspace = true
starknet_api.workspace = true
thiserror.workspace = true
papyrus_config.workspace = true
tower.workspace = true
validator.workspace = true

[dev-dependencies]
Expand Down
45 changes: 45 additions & 0 deletions crates/gateway/tests/routing_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use axum::body::{Body, Bytes, HttpBody};
use axum::http::{Request, StatusCode};
use pretty_assertions::assert_str_eq;
use rstest::rstest;
use starknet_gateway::gateway::app;
use std::fs;
use tower::ServiceExt;

// TODO(Ayelet): Replace the use of the JSON files with generated instances, then serialize these
// into JSON for testing.
#[rstest]
#[case("./src/json_files_for_testing/declare_v3.json", "DECLARE")]
#[case(
"./src/json_files_for_testing/deploy_account_v3.json",
"DEPLOY_ACCOUNT"
)]
#[case("./src/json_files_for_testing/invoke_v3.json", "INVOKE")]
#[tokio::test]
async fn test_routes(#[case] json_file_path: &str, #[case] expected_response: &str) {
let tx_json = fs::read_to_string(json_file_path).unwrap();
let request = Request::post("/add_transaction")
.header("content-type", "application/json")
.body(Body::from(tx_json))
.unwrap();

let response = check_request(request, StatusCode::OK).await;

assert_str_eq!(expected_response, String::from_utf8_lossy(&response));
}

#[tokio::test]
#[should_panic]
// FIXME: Currently is_alive is not implemented, fix this once it is implemented.
async fn test_is_alive() {
let request = Request::get("/is_alive").body(Body::empty()).unwrap();
// Status code doesn't matter, this panics ATM.
check_request(request, StatusCode::default()).await;
}

async fn check_request(request: Request<Body>, status_code: StatusCode) -> Bytes {
let response = app().oneshot(request).await.unwrap();
assert_eq!(response.status(), status_code);

response.into_body().collect().await.unwrap().to_bytes()
}
Loading