diff --git a/Cargo.lock b/Cargo.lock index ecd67e4..0317cb3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -811,6 +811,16 @@ dependencies = [ "itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "http" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bytes 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "http-body" version = "0.1.0" @@ -1522,6 +1532,7 @@ dependencies = [ "flamer 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "function_name 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", + "http 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "id3 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "image 0.22.3 (registry+https://github.com/rust-lang/crates.io-index)", "lewton 0.9.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2839,6 +2850,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum hex 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "023b39be39e3a2da62a94feb433e91e8bcd37676fbc8bea371daf52b7a769a3e" "checksum hmac 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5dcb5e64cda4c23119ab41ba960d1e170a774c8e4b9d9e6a9bc18aabf5e59695" "checksum http 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)" = "d6ccf5ede3a895d8856620237b2f02972c1bbc78d2965ad7fe8838d4a0ed41f0" +"checksum http 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b708cc7f06493459026f53b9a61a7a121a5d1ec6238dee58ea4941132b30156b" "checksum http-body 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6741c859c1b2463a423a1dbce98d418e6c3c3fc720fb0d45528657320920292d" "checksum httparse 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" "checksum hyper 0.10.16 (registry+https://github.com/rust-lang/crates.io-index)" = "0a0652d9a2609a968c14be1a9ea00bf4b1d64e2e1f53a1b51b6fff3a6e829273" diff --git a/Cargo.toml b/Cargo.toml index 55bca09..026cf64 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ flame = { version = "0.2.2", optional = true } flamer = { version = "0.4", optional = true } function_name = "0.2.0" getopts = "0.2.15" +http = "0.2" id3 = "0.3" image = "0.22" libsqlite3-sys = { version = "0.16", features = ["bundled-windows"] } diff --git a/src/service/rocket/test.rs b/src/service/rocket/test.rs index 1e673df..9dcdc81 100644 --- a/src/service/rocket/test.rs +++ b/src/service/rocket/test.rs @@ -1,3 +1,4 @@ +use http::Response; use rocket; use rocket::http::Status; use rocket::local::Client; @@ -11,7 +12,17 @@ use std::sync::Arc; use super::server; use crate::db::DB; use crate::index; -use crate::service::test::{HttpStatus, TestService}; +use crate::service::test::TestService; + +pub struct RocketResponse<'r>(&'r rocket::Response<'r>); +impl<'r> Into> for RocketResponse<'r> { + fn into(self) -> Response<()> { + Response::builder() + .status(self.0.status().code) + .body(()) + .unwrap() + } +} pub struct RocketTestService { client: Client, @@ -20,19 +31,7 @@ pub struct RocketTestService { pub type ServiceType = RocketTestService; -impl HttpStatus for Status { - fn is_ok(&self) -> bool { - *self == Status::Ok - } - - fn is_unauthorized(&self) -> bool { - *self == Status::Unauthorized - } -} - impl TestService for RocketTestService { - type Status = Status; - fn new(db_name: &str) -> Self { let mut db_path = PathBuf::new(); db_path.push("test"); @@ -68,22 +67,19 @@ impl TestService for RocketTestService { } } - fn get(&mut self, url: &str) -> Status { - let client = &self.client; - let response = client.get(url).dispatch(); - response.status() + fn get(&mut self, url: &str) -> Response<()> { + let response = self.client.get(url).dispatch(); + RocketResponse(response.deref()).into() } - fn post(&mut self, url: &str) -> Status { - let client = &self.client; - let response = client.post(url).dispatch(); - response.status() + fn post(&mut self, url: &str) -> Response<()> { + let response = self.client.post(url).dispatch(); + RocketResponse(response.deref()).into() } - fn delete(&mut self, url: &str) -> Status { - let client = &self.client; - let response = client.delete(url).dispatch(); - response.status() + fn delete(&mut self, url: &str) -> Response<()> { + let response = self.client.delete(url).dispatch(); + RocketResponse(response.deref()).into() } fn get_json(&mut self, url: &str) -> T { @@ -102,11 +98,10 @@ impl TestService for RocketTestService { assert_eq!(response.status(), Status::Ok); } - fn post_json(&mut self, url: &str, payload: &T) -> Status { - let client = &self.client; + fn post_json(&mut self, url: &str, payload: &T) -> Response<()> { let body = serde_json::to_string(payload).unwrap(); - let response = client.post(url).body(&body).dispatch(); - response.status() + let response = self.client.post(url).body(&body).dispatch(); + RocketResponse(response.deref()).into() } } diff --git a/src/service/test.rs b/src/service/test.rs index a592036..6385733 100644 --- a/src/service/test.rs +++ b/src/service/test.rs @@ -1,4 +1,5 @@ use function_name::named; +use http::{Response, StatusCode}; use serde::de::DeserializeOwned; use serde::Serialize; use std::path::PathBuf; @@ -16,21 +17,14 @@ const TEST_PASSWORD: &str = "test_password"; const TEST_MOUNT_NAME: &str = "collection"; const TEST_MOUNT_SOURCE: &str = "test/collection"; -pub trait HttpStatus { - fn is_ok(&self) -> bool; - fn is_unauthorized(&self) -> bool; -} - pub trait TestService { - type Status: HttpStatus; - fn new(db_name: &str) -> Self; - fn get(&mut self, url: &str) -> Self::Status; - fn post(&mut self, url: &str) -> Self::Status; - fn delete(&mut self, url: &str) -> Self::Status; + fn get(&mut self, url: &str) -> Response<()>; + fn post(&mut self, url: &str) -> Response<()>; + fn delete(&mut self, url: &str) -> Response<()>; fn get_json(&mut self, url: &str) -> T; fn put_json(&mut self, url: &str, payload: &T); - fn post_json(&mut self, url: &str, payload: &T) -> Self::Status; + fn post_json(&mut self, url: &str, payload: &T) -> Response<()>; fn complete_initial_setup(&mut self) { let configuration = config::Config { @@ -60,7 +54,7 @@ pub trait TestService { } fn index(&mut self) { - assert!(self.post("/api/trigger_index").is_ok()); + assert!(self.post("/api/trigger_index").status() == StatusCode::OK); for _ in 1..20 { let entries: Vec = self.get_json("/api/browse"); if entries.len() > 0 { @@ -83,14 +77,14 @@ fn test_service_index() { #[test] fn test_service_swagger_index() { let mut service = ServiceType::new(function_name!()); - assert!(service.get("/swagger").is_ok()); + assert!(service.get("/swagger").status() == StatusCode::OK); } #[named] #[test] fn test_service_swagger_index_with_trailing_slash() { let mut service = ServiceType::new(function_name!()); - assert!(service.get("/swagger/").is_ok()); + assert!(service.get("/swagger/").status() == StatusCode::OK); } #[named] @@ -132,7 +126,7 @@ fn test_service_settings() { let mut service = ServiceType::new(function_name!()); service.complete_initial_setup(); - assert!(service.get("/api/settings").is_unauthorized()); + assert!(service.get("/api/settings").status() == StatusCode::UNAUTHORIZED); service.login(); { @@ -246,25 +240,21 @@ fn test_service_auth() { username: "garbage".into(), password: "garbage".into(), }; - assert!(service - .post_json("/api/auth", &credentials) - .is_unauthorized()); + assert!(service.post_json("/api/auth", &credentials).status() == StatusCode::UNAUTHORIZED); } { let credentials = dto::AuthCredentials { username: TEST_USERNAME.into(), password: "garbage".into(), }; - assert!(service - .post_json("/api/auth", &credentials) - .is_unauthorized()); + assert!(service.post_json("/api/auth", &credentials).status() == StatusCode::UNAUTHORIZED); } { let credentials = dto::AuthCredentials { username: TEST_USERNAME.into(), password: TEST_PASSWORD.into(), }; - assert!(service.post_json("/api/auth", &credentials).is_ok()); + assert!(service.post_json("/api/auth", &credentials).status() == StatusCode::OK); // TODO validate cookies } }