Skip to content

Commit

Permalink
feat: add routes test (semi-integration test)
Browse files Browse the repository at this point in the history
- tests that routes behave the way we expect, shouldn't have too much
  logic here, just test status codes and return values.
  Proper integration/flow tests will be in their own testing file.
- add `tower` dependency that `axum` uses in order to simulate an HTTP
  request via the `oneshot` call: this simulates the HTTP request
  without having to spin up an actual server and perform IO.
- Currently add_transaction test is identical to the one in
  gateway_test.rs, but they'll diverge once real logic kicks in. At that
  point the routes test will only test for success codes in basic
  scenarios.
- is_alive is currently just a dummy test, since it isn't implemented
  yet.
  • Loading branch information
Gilad Chase committed Apr 4, 2024
1 parent 9aa64ae commit 2499b1b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ serde_json = "1.0"
starknet_api = { git = "https://github.com/starkware-libs/starknet-api.git", branch = "mohammad/transaction/implement-external-transactions" }
thiserror = "1.0"
tokio = { version = "1", features = ["full"] }
tower = "0.4.13"
1 change: 1 addition & 0 deletions crates/gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ serde.workspace = true
serde_json.workspace = true
starknet_api.workspace = true
thiserror.workspace = true
tower.workspace = true

[dev-dependencies]
assert_matches.workspace = true
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()
}

0 comments on commit 2499b1b

Please sign in to comment.