Skip to content

Commit

Permalink
corrected tests in service.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
aaravm committed Jun 18, 2024
1 parent 395b00a commit b1c12fe
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 66 deletions.
19 changes: 13 additions & 6 deletions lib/src/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


#[derive(Debug, Clone)]
pub struct ServiceConfiguration {
pub struct Configuration {
pub base_path: String,
pub user_agent: Option<String>,
pub basic_auth: Option<BasicAuth>,
Expand All @@ -20,9 +20,16 @@ pub struct ApiKey {
}


impl ServiceConfiguration {
pub fn new() -> ServiceConfiguration {
ServiceConfiguration::default()
impl Configuration {
pub fn new(base_path: String, user_agent: Option<String>, oauth_access_token: Option<String>) -> Self {
Configuration {
base_path,
user_agent,
basic_auth: None,
oauth_access_token,
bearer_access_token: None,
api_key: None,
}
}

pub fn set_base_path(&mut self, base_path: &str) -> &mut Self {
Expand All @@ -31,9 +38,9 @@ impl ServiceConfiguration {
}
}

impl Default for ServiceConfiguration {
impl Default for Configuration {
fn default() -> Self {
ServiceConfiguration {
Configuration {
base_path: "localhost".to_owned(),
user_agent: Some("GA4GH SDK".to_owned()),
basic_auth: None,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/serviceinfo/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::transport::Transport;
use crate::configuration::ServiceConfiguration;
use crate::configuration::Configuration;

pub struct ServiceInfo {
transport: Transport,
}

impl ServiceInfo {
pub fn new(config: &ServiceConfiguration) -> Self {
pub fn new(config: &Configuration) -> Self {
// todo: read service info from the server
ServiceInfo {
transport: Transport::new(&config.clone()),
Expand Down
10 changes: 5 additions & 5 deletions lib/src/tes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod models;
use crate::transport::Transport;
use serde_json::json;
use crate::serviceinfo::ServiceInfo;
use crate::configuration::ServiceConfiguration;
use crate::configuration::Configuration;
use crate::tes::models::TesTask;
use crate::tes::models::TesCreateTaskResponse;
use crate::tes::models::TesState;
Expand All @@ -27,14 +27,14 @@ impl Task {
}

pub struct TES {
config: ServiceConfiguration,
config: Configuration,
service_info: ServiceInfo,
transport: Transport,
}
// *** see question above

impl TES {
pub fn new(config: &ServiceConfiguration) -> Self {
pub fn new(config: &Configuration) -> Self {
// todo double check that it's really a TES using serviceinfo
TES {
config: config.clone(),
Expand Down Expand Up @@ -95,12 +95,12 @@ impl TES {
#[cfg(test)]
mod tests {
use crate::tes::TES;
use crate::configuration::ServiceConfiguration;
use crate::configuration::Configuration;
use crate::tes::models::TesTask;
use crate::tes::models::TesCreateTaskResponse;

async fn create_task() -> Result<String, Box<dyn std::error::Error>> {
let mut config = ServiceConfiguration::default();
let mut config = Configuration::default();
config.set_base_path("http://localhost:8080"); // expecting TES/Funnel, TODO autorun
let tes = TES::new(&config);

Expand Down
110 changes: 57 additions & 53 deletions lib/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ use serde::Serialize;
use serde_json::Value;
use std::error::Error;
use std::fmt;
use crate::configuration::ServiceConfiguration;
use crate::configuration::Configuration;

// note: could implement custom certs handling, such as in-TEE generated ephemerial certs

pub struct Transport {
config: ServiceConfiguration,
config: Configuration,
client: reqwest::Client,
}

impl Transport {
pub fn new(config: &ServiceConfiguration) -> Self {
pub fn new(config: &Configuration) -> Self {
Transport {
config: config.clone(),
client: Client::new(),
Expand Down Expand Up @@ -84,54 +84,58 @@ impl Transport {

#[cfg(test)]
mod tests {
// use crate::transport::Transport;
// use reqwest::Method;
// use serde_json::json;
// use mockito::{mock, Matcher};

// #[tokio::test]
// async fn test_request_success() {
// let base_url = &mockito::server_url();
// let _m = mock("GET", "/test")
// .with_status(200)
// .with_header("content-type", "application/json")
// .with_body(r#"{"message": "success"}"#)
// .create();

// let transport = Transport::new(base_url.clone(), None, None, None);

// let response = transport.request(
// Method::GET,
// &format!("{}/test", base_url),
// None,
// None,
// ).await;

// assert!(response.is_ok());
// let body = response.unwrap();
// assert_eq!(body, r#"{"message": "success"}"#);
// }

// #[tokio::test]
// async fn test_request_failure() {
// let base_url = &mockito::server_url();
// let _m = mock("GET", "/test")
// .with_status(404)
// .with_header("content-type", "application/json")
// .with_body(r#"{"message": "not found"}"#)
// .create();

// let transport = Transport::new(base_url.clone(), None, None, None);

// let response = transport.request(
// Method::GET,
// &format!("{}/test", base_url),
// None,
// None,
// ).await;

// assert!(response.is_err());
// let error = response.err().unwrap();
// assert_eq!(error.to_string(), r#"{"message": "not found"}"#);
// }
use crate::{configuration::Configuration, transport::Transport};
use reqwest::Method;
use serde_json::json;
use mockito::{mock, Matcher};

#[tokio::test]
async fn test_request_success() {
let base_url = &mockito::server_url();
let _m = mock("GET", "/test")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(r#"{"message": "success"}"#)
.create();

let config= Configuration::new(base_url.clone(), None, None);

let transport = Transport::new(&config.clone());

let response = transport.request(
Method::GET,
&format!("{}/test", base_url),
None,
None,
).await;

assert!(response.is_ok());
let body = response.unwrap();
assert_eq!(body, r#"{"message": "success"}"#);
}

#[tokio::test]
async fn test_request_failure() {
let base_url = &mockito::server_url();
let _m = mock("GET", "/test")
.with_status(404)
.with_header("content-type", "application/json")
.with_body(r#"{"message": "not found"}"#)
.create();

let config= Configuration::new(base_url.clone(), None, None);

let transport = Transport::new(&config.clone());

let response = transport.request(
Method::GET,
&format!("{}/test", base_url),
None,
None,
).await;

assert!(response.is_err());
let error = response.err().unwrap();
assert_eq!(error.to_string(), r#"{"message": "not found"}"#);
}
}

0 comments on commit b1c12fe

Please sign in to comment.