Skip to content

Commit 652f50b

Browse files
committed
refactor: [#109] extract structs and functions
1 parent 80ad41e commit 652f50b

File tree

8 files changed

+54
-19
lines changed

8 files changed

+54
-19
lines changed

tests/e2e/asserts.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ pub fn assert_response_title(response: &Response, title: &str) {
99
);
1010
}
1111

12-
pub fn assert_ok(response: &Response) {
12+
pub fn assert_text_ok(response: &Response) {
1313
assert_eq!(response.status, 200);
1414
assert_eq!(response.content_type, "text/html; charset=utf-8");
1515
}
16+
17+
pub fn assert_json_ok(response: &Response) {
18+
assert_eq!(response.status, 200);
19+
assert_eq!(response.content_type, "application/json");
20+
}

tests/e2e/client.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ impl Client {
1818
}
1919
}
2020

21+
pub async fn root(&self) -> Response {
22+
self.get("", Query::empty()).await
23+
}
24+
25+
pub async fn about(&self) -> Response {
26+
self.get("about", Query::empty()).await
27+
}
28+
29+
pub async fn license(&self) -> Response {
30+
self.get("about/license", Query::empty()).await
31+
}
32+
2133
pub async fn get(&self, path: &str, params: Query) -> Response {
2234
self.get_request_with_query(path, params).await
2335
}

tests/e2e/connection_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub fn connection_with_no_token(bind_address: &str) -> ConnectionInfo {
1+
pub fn anonymous_connection(bind_address: &str) -> ConnectionInfo {
22
ConnectionInfo::anonymous(bind_address)
33
}
44

tests/e2e/env.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crate::e2e::client::Client;
2+
use crate::e2e::connection_info::anonymous_connection;
3+
4+
pub struct TestEnv {
5+
pub authority: String,
6+
}
7+
8+
impl TestEnv {
9+
pub fn guess_client(&self) -> Client {
10+
Client::new(anonymous_connection(&self.authority))
11+
}
12+
}
13+
14+
impl Default for TestEnv {
15+
fn default() -> Self {
16+
Self {
17+
authority: "localhost:3000".to_string(),
18+
}
19+
}
20+
}

tests/e2e/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
mod asserts;
2020
mod client;
2121
mod connection_info;
22+
pub mod env;
2223
mod http;
2324
mod response;
2425
mod routes;

tests/e2e/routes/about.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
use crate::e2e::asserts::{assert_ok, assert_response_title};
2-
use crate::e2e::client::Client;
3-
use crate::e2e::connection_info::connection_with_no_token;
4-
use crate::e2e::http::Query;
1+
use crate::e2e::asserts::{assert_response_title, assert_text_ok};
2+
use crate::e2e::env::TestEnv;
53

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

11-
let response = client.get("about", Query::empty()).await;
9+
let response = client.about().await;
1210

13-
assert_ok(&response);
11+
assert_text_ok(&response);
1412
assert_response_title(&response, "About");
1513
}
1614

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

22-
let response = client.get("about/license", Query::empty()).await;
20+
let response = client.license().await;
2321

24-
assert_ok(&response);
22+
assert_text_ok(&response);
2523
assert_response_title(&response, "Licensing");
2624
}

tests/e2e/routes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod about;
2+
23
pub mod root;

tests/e2e/routes/root.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
use crate::e2e::asserts::{assert_ok, assert_response_title};
2-
use crate::e2e::client::Client;
3-
use crate::e2e::connection_info::connection_with_no_token;
4-
use crate::e2e::http::Query;
1+
use crate::e2e::asserts::{assert_response_title, assert_text_ok};
2+
use crate::e2e::env::TestEnv;
53

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

11-
let response = client.get("", Query::empty()).await;
9+
let response = client.root().await;
1210

13-
assert_ok(&response);
11+
assert_text_ok(&response);
1412
assert_response_title(&response, "About");
1513
}

0 commit comments

Comments
 (0)