feat: add tests for invalid/failure cases (#6951)
This commit is contained in:
parent
dbbd9663b2
commit
42af8b199f
|
@ -22,6 +22,8 @@ fn test_rpc_client() {
|
|||
solana_clap_utils::version!()
|
||||
);
|
||||
|
||||
assert!(client.get_account(&bob_pubkey).is_err());
|
||||
|
||||
assert_eq!(client.get_balance(&bob_pubkey).unwrap(), 0);
|
||||
|
||||
assert_eq!(client.get_balance(&alice.pubkey()).unwrap(), 1_000_000);
|
||||
|
|
|
@ -95,3 +95,74 @@ fn test_rpc_send_tx() {
|
|||
server.close().unwrap();
|
||||
remove_dir_all(ledger_path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rpc_invalid_requests() {
|
||||
solana_logger::setup();
|
||||
|
||||
let (server, leader_data, _alice, ledger_path) = new_validator_for_tests();
|
||||
let bob_pubkey = Pubkey::new_rand();
|
||||
|
||||
// test invalid get_balance request
|
||||
let client = reqwest::Client::new();
|
||||
let request = json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "getBalance",
|
||||
"params": json!(["invalid9999"])
|
||||
});
|
||||
let rpc_addr = leader_data.rpc;
|
||||
let rpc_string = get_rpc_request_str(rpc_addr, false);
|
||||
let mut response = client
|
||||
.post(&rpc_string)
|
||||
.header(CONTENT_TYPE, "application/json")
|
||||
.body(request.to_string())
|
||||
.send()
|
||||
.unwrap();
|
||||
let json: Value = serde_json::from_str(&response.text().unwrap()).unwrap();
|
||||
let the_error = json["error"]["message"].as_str().unwrap();
|
||||
assert_eq!(the_error, "Invalid request");
|
||||
|
||||
// test invalid get_account_info request
|
||||
let client = reqwest::Client::new();
|
||||
let request = json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "getAccountInfo",
|
||||
"params": json!(["invalid9999"])
|
||||
});
|
||||
let rpc_addr = leader_data.rpc;
|
||||
let rpc_string = get_rpc_request_str(rpc_addr, false);
|
||||
let mut response = client
|
||||
.post(&rpc_string)
|
||||
.header(CONTENT_TYPE, "application/json")
|
||||
.body(request.to_string())
|
||||
.send()
|
||||
.unwrap();
|
||||
let json: Value = serde_json::from_str(&response.text().unwrap()).unwrap();
|
||||
let the_error = json["error"]["message"].as_str().unwrap();
|
||||
assert_eq!(the_error, "Invalid request");
|
||||
|
||||
// test invalid get_account_info request
|
||||
let client = reqwest::Client::new();
|
||||
let request = json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "getAccountInfo",
|
||||
"params": json!([bob_pubkey.to_string()])
|
||||
});
|
||||
let rpc_addr = leader_data.rpc;
|
||||
let rpc_string = get_rpc_request_str(rpc_addr, false);
|
||||
let mut response = client
|
||||
.post(&rpc_string)
|
||||
.header(CONTENT_TYPE, "application/json")
|
||||
.body(request.to_string())
|
||||
.send()
|
||||
.unwrap();
|
||||
let json: Value = serde_json::from_str(&response.text().unwrap()).unwrap();
|
||||
let the_value = &json["result"]["value"];
|
||||
assert!(the_value.is_null());
|
||||
|
||||
server.close().unwrap();
|
||||
remove_dir_all(ledger_path).unwrap();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue