Skip to content

Commit

Permalink
refactor: [#109] extract structs and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Apr 21, 2023
1 parent 80ad41e commit 652f50b
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 19 deletions.
7 changes: 6 additions & 1 deletion tests/e2e/asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ pub fn assert_response_title(response: &Response, title: &str) {
);
}

pub fn assert_ok(response: &Response) {
pub fn assert_text_ok(response: &Response) {
assert_eq!(response.status, 200);
assert_eq!(response.content_type, "text/html; charset=utf-8");
}

pub fn assert_json_ok(response: &Response) {
assert_eq!(response.status, 200);
assert_eq!(response.content_type, "application/json");
}
12 changes: 12 additions & 0 deletions tests/e2e/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ impl Client {
}
}

pub async fn root(&self) -> Response {
self.get("", Query::empty()).await
}

pub async fn about(&self) -> Response {
self.get("about", Query::empty()).await
}

pub async fn license(&self) -> Response {
self.get("about/license", Query::empty()).await
}

pub async fn get(&self, path: &str, params: Query) -> Response {
self.get_request_with_query(path, params).await
}
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/connection_info.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub fn connection_with_no_token(bind_address: &str) -> ConnectionInfo {
pub fn anonymous_connection(bind_address: &str) -> ConnectionInfo {
ConnectionInfo::anonymous(bind_address)
}

Expand Down
20 changes: 20 additions & 0 deletions tests/e2e/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::e2e::client::Client;
use crate::e2e::connection_info::anonymous_connection;

pub struct TestEnv {
pub authority: String,
}

impl TestEnv {
pub fn guess_client(&self) -> Client {
Client::new(anonymous_connection(&self.authority))
}
}

impl Default for TestEnv {
fn default() -> Self {
Self {
authority: "localhost:3000".to_string(),
}
}
}
1 change: 1 addition & 0 deletions tests/e2e/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
mod asserts;
mod client;
mod connection_info;
pub mod env;
mod http;
mod response;
mod routes;
18 changes: 8 additions & 10 deletions tests/e2e/routes/about.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
use crate::e2e::asserts::{assert_ok, assert_response_title};
use crate::e2e::client::Client;
use crate::e2e::connection_info::connection_with_no_token;
use crate::e2e::http::Query;
use crate::e2e::asserts::{assert_response_title, assert_text_ok};
use crate::e2e::env::TestEnv;

#[tokio::test]
#[cfg_attr(not(feature = "e2e-tests"), ignore)]
async fn it_should_load_the_about_page_with_information_about_the_api() {
let client = Client::new(connection_with_no_token("localhost:3000"));
let client = TestEnv::default().guess_client();

let response = client.get("about", Query::empty()).await;
let response = client.about().await;

assert_ok(&response);
assert_text_ok(&response);
assert_response_title(&response, "About");
}

#[tokio::test]
#[cfg_attr(not(feature = "e2e-tests"), ignore)]
async fn it_should_load_the_license_page_at_the_api_entrypoint() {
let client = Client::new(connection_with_no_token("localhost:3000"));
let client = TestEnv::default().guess_client();

let response = client.get("about/license", Query::empty()).await;
let response = client.license().await;

assert_ok(&response);
assert_text_ok(&response);
assert_response_title(&response, "Licensing");
}
1 change: 1 addition & 0 deletions tests/e2e/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod about;

pub mod root;
12 changes: 5 additions & 7 deletions tests/e2e/routes/root.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use crate::e2e::asserts::{assert_ok, assert_response_title};
use crate::e2e::client::Client;
use crate::e2e::connection_info::connection_with_no_token;
use crate::e2e::http::Query;
use crate::e2e::asserts::{assert_response_title, assert_text_ok};
use crate::e2e::env::TestEnv;

#[tokio::test]
#[cfg_attr(not(feature = "e2e-tests"), ignore)]
async fn it_should_load_the_about_page_at_the_api_entrypoint() {
let client = Client::new(connection_with_no_token("localhost:3000"));
let client = TestEnv::default().guess_client();

let response = client.get("", Query::empty()).await;
let response = client.root().await;

assert_ok(&response);
assert_text_ok(&response);
assert_response_title(&response, "About");
}

0 comments on commit 652f50b

Please sign in to comment.