-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix and Fish Request Handler Implementation and Tests
- Loading branch information
Showing
3 changed files
with
75 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use helius_sdk::error::{HeliusError, Result}; | ||
use helius_sdk::request_handler::RequestHandler; | ||
|
||
use mockito::{self, Server}; | ||
use reqwest::{Client, Method}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::sync::Arc; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Default)] | ||
struct MockResponse { | ||
message: String, | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_successful_request() { | ||
let mut server: Server = Server::new_with_opts_async(mockito::ServerOpts::default()).await; | ||
let url: String = server.url(); | ||
|
||
server | ||
.mock("GET", "/") | ||
.with_status(200) | ||
.with_header("content-type", "application/json") | ||
.with_body(r#"{"message": "success"}"#) | ||
.create(); | ||
|
||
let client: Arc<Client> = Arc::new(Client::new()); | ||
let handler: RequestHandler = RequestHandler::new(client).unwrap(); | ||
|
||
let response: Result<MockResponse> = handler | ||
.send::<(), MockResponse>(Method::GET, url.parse().unwrap(), None) | ||
.await; | ||
|
||
assert!(response.is_ok()); | ||
assert_eq!(response.unwrap().message, "success"); | ||
|
||
server.reset(); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_bad_request_error() { | ||
let mut server: Server = Server::new_with_opts_async(mockito::ServerOpts::default()).await; | ||
let url: String = server.url(); | ||
|
||
server | ||
.mock("GET", "/") | ||
.with_status(400) | ||
.with_header("content-type", "application/json") | ||
.with_body(r#"{"message": "bad request"}"#) | ||
.create(); | ||
|
||
let client: Arc<Client> = Arc::new(Client::new()); | ||
let handler: RequestHandler = RequestHandler::new(client).unwrap(); | ||
|
||
let response: Result<MockResponse> = handler | ||
.send::<(), MockResponse>(Method::GET, url.parse().unwrap(), None) | ||
.await; | ||
|
||
assert!(response.is_err()); | ||
match response { | ||
Err(HeliusError::BadRequest { text, .. }) => assert_eq!(text, "bad request"), | ||
_ => panic!("Expected BadRequest error"), | ||
} | ||
|
||
server.reset(); | ||
} |