Skip to content

Commit 9253f33

Browse files
authored
Merge pull request #16 from Bibimbap-Team/15-change-project-structure-for-easier-testing
Change project structure for easier testing
2 parents 1a30ef7 + 7ea2aa8 commit 9253f33

File tree

4 files changed

+46
-26
lines changed

4 files changed

+46
-26
lines changed

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@ name = "bibimbap-backend"
33
version = "0.1.0"
44
edition = "2021"
55

6+
[lib]
7+
path = "src/lib.rs"
8+
9+
[[bin]]
10+
path = "src/main.rs"
11+
name = "bibimbap-backend"
12+
613
[dependencies]
714
rocket = "0.5.1"

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[macro_use]
2+
extern crate rocket;
3+
4+
use rocket::{Build, Rocket};
5+
6+
#[get("/health_check")]
7+
fn health_check() {
8+
()
9+
}
10+
11+
pub fn run() -> Rocket<Build> {
12+
rocket::build().mount("/", routes![health_check])
13+
}

src/main.rs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,9 @@
1-
#[macro_use]
2-
extern crate rocket;
1+
use bibimbap_backend::run;
32

4-
#[get("/health_check")]
5-
fn health_check() {
6-
()
7-
}
8-
9-
#[launch]
10-
fn rocket() -> _ {
11-
rocket::build().mount("/", routes![health_check])
12-
}
13-
14-
#[cfg(test)]
15-
mod test {
16-
use super::rocket;
17-
use rocket::http::Status;
18-
use rocket::local::blocking::Client;
19-
use std::io::Read;
20-
21-
#[test]
22-
fn health_check_works() {
23-
let client = Client::tracked(rocket()).expect("Failed to create client");
24-
let response = client.get(uri!(super::health_check)).dispatch();
25-
26-
assert_eq!(response.status(), Status::Ok);
27-
assert_eq!(response.bytes().count(), 0);
3+
#[rocket::main]
4+
async fn main() {
5+
if let Err(e) = run().launch().await {
6+
println!("Failed to launch Rocket: {e}");
7+
drop(e);
288
}
299
}

tests/health_check.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#[macro_use]
2+
extern crate rocket;
3+
4+
use rocket::http::Status;
5+
use rocket::local::blocking::Client;
6+
use rocket::{Build, Rocket};
7+
use std::io::Read;
8+
9+
fn spawn_app() -> Rocket<Build> {
10+
bibimbap_backend::run()
11+
}
12+
13+
#[test]
14+
fn health_check_works() {
15+
let client = Client::tracked(spawn_app()).expect("Failed to create client");
16+
let response = client.get(uri!("/health_check")).dispatch();
17+
18+
assert_eq!(response.status(), Status::Ok);
19+
assert_eq!(response.bytes().count(), 0);
20+
}

0 commit comments

Comments
 (0)