First half of serve request

This commit is contained in:
Antoine Gersant 2020-01-17 02:37:33 -08:00
parent 3c45150651
commit 7f7da0050b
4 changed files with 70 additions and 75 deletions

30
Cargo.lock generated
View file

@ -1497,11 +1497,6 @@ name = "percent-encoding"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "pin-project-lite"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "pkg-config"
version = "0.3.17"
@ -1541,6 +1536,7 @@ dependencies = [
"metaflac 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"mp3-duration 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"pbkdf2 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"reqwest 0.9.24 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1555,7 +1551,6 @@ dependencies = [
"simplelog 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)",
"thiserror 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
"unix-daemonize 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"url 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2367,17 +2362,6 @@ dependencies = [
"tokio-timer 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "tokio"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bytes 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)",
"pin-project-lite 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-macros 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "tokio-buf"
version = "0.1.1"
@ -2416,15 +2400,6 @@ dependencies = [
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "tokio-macros"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "tokio-reactor"
version = "0.1.11"
@ -2922,7 +2897,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum pear_codegen 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "336db4a192cc7f54efeb0c4e11a9245394824cc3bcbd37ba3ff51240c35d7a6e"
"checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831"
"checksum percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
"checksum pin-project-lite 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e8822eb8bb72452f038ebf6048efa02c3fe22bf83f76519c9583e47fc194a422"
"checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677"
"checksum png 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)" = "247cb804bd7fc86d0c2b153d1374265e67945875720136ca8fe451f11c6aed52"
"checksum ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b"
@ -3011,12 +2985,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum tiff 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d7b7c2cfc4742bd8a32f2e614339dd8ce30dbcf676bb262bd63a2327bc5df57d"
"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f"
"checksum tokio 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)" = "5a09c0b5bb588872ab2f09afa13ee6e9dac11e10a0ec9e8e3ba39a5a5d530af6"
"checksum tokio 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "ffa2fdcfa937b20cb3c822a635ceecd5fc1a27a6a474527e5516aa24b8c8820a"
"checksum tokio-buf 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8fb220f46c53859a4b7ec083e41dec9778ff0b1851c0942b211edb89e0ccdc46"
"checksum tokio-current-thread 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "d16217cad7f1b840c5a97dfb3c43b0c871fef423a6e8d2118c604e843662a443"
"checksum tokio-executor 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "ca6df436c42b0c3330a82d855d2ef017cd793090ad550a6bc2184f4b933532ab"
"checksum tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "5090db468dad16e1a7a54c8c67280c5e4b544f3d3e018f0b913b400261f85926"
"checksum tokio-macros 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "50a61f268a3db2acee8dcab514efc813dc6dbe8a00e86076f935f94304b59a7a"
"checksum tokio-reactor 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "6732fe6b53c8d11178dcb77ac6d9682af27fc6d4cb87789449152e5377377146"
"checksum tokio-sync 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "d06554cce1ae4a50f42fba8023918afa931413aded705b560e29600ccf7c6d76"
"checksum tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1d14b10654be682ac43efee27401d792507e30fd8d26389e1da3b185de2e4119"

View file

@ -51,10 +51,6 @@ default_features = false
features = ["json", "serve"]
optional = true
[dependencies.tokio]
version = "0.2"
features = ["macros", "rt-threaded"]
[target.'cfg(windows)'.dependencies]
uuid = "0.8"
@ -65,3 +61,6 @@ features = ["winuser", "libloaderapi", "shellapi", "errhandlingapi"]
[target.'cfg(unix)'.dependencies]
sd-notify = "0.1.0"
unix-daemonize = "0.1.2"
[dev-dependencies]
percent-encoding = "2.1"

View file

@ -1,11 +1,11 @@
use http::Response;
use http::response::{Builder, Response};
use rocket;
use rocket::http::Status;
use rocket::local::Client;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::fs;
use std::ops::Deref;
use std::ops::{Deref, DerefMut};
use std::path::PathBuf;
use std::sync::Arc;
@ -14,13 +14,27 @@ use crate::db::DB;
use crate::index;
use crate::service::test::TestService;
pub struct RocketResponse<'r>(&'r rocket::Response<'r>);
impl<'r> Into<Response<()>> for RocketResponse<'r> {
pub struct RocketResponse<'r, 's>(&'r mut rocket::Response<'s>);
impl<'r, 's> Into<Builder> for RocketResponse<'r, 's> {
fn into(self) -> Builder {
Response::builder().status(self.0.status().code)
}
}
impl<'r, 's> Into<Response<()>> for RocketResponse<'r, 's> {
fn into(self) -> Response<()> {
Response::builder()
.status(self.0.status().code)
.body(())
.unwrap()
let builder: Builder = self.into();
builder.body(()).unwrap()
}
}
impl<'r, 's> Into<Response<Vec<u8>>> for RocketResponse<'r, 's> {
fn into(self) -> Response<Vec<u8>> {
let body = self.0.body().unwrap();
let body = body.into_bytes().unwrap();
let builder: Builder = self.into();
builder.body(body).unwrap()
}
}
@ -68,18 +82,23 @@ impl TestService for RocketTestService {
}
fn get(&mut self, url: &str) -> Response<()> {
let response = self.client.get(url).dispatch();
RocketResponse(response.deref()).into()
let mut response = self.client.get(url).dispatch();
RocketResponse(response.deref_mut()).into()
}
fn get_bytes(&mut self, url: &str) -> Response<Vec<u8>> {
let mut response = self.client.get(url).dispatch();
RocketResponse(response.deref_mut()).into()
}
fn post(&mut self, url: &str) -> Response<()> {
let response = self.client.post(url).dispatch();
RocketResponse(response.deref()).into()
let mut response = self.client.post(url).dispatch();
RocketResponse(response.deref_mut()).into()
}
fn delete(&mut self, url: &str) -> Response<()> {
let response = self.client.delete(url).dispatch();
RocketResponse(response.deref()).into()
let mut response = self.client.delete(url).dispatch();
RocketResponse(response.deref_mut()).into()
}
fn get_json<T: DeserializeOwned>(&mut self, url: &str) -> T {
@ -87,7 +106,6 @@ impl TestService for RocketTestService {
let mut response = client.get(url).dispatch();
assert_eq!(response.status(), Status::Ok);
let response_body = response.body_string().unwrap();
dbg!(&response_body);
serde_json::from_str(&response_body).unwrap()
}
@ -100,8 +118,8 @@ impl TestService for RocketTestService {
fn post_json<T: Serialize>(&mut self, url: &str, payload: &T) -> Response<()> {
let body = serde_json::to_string(payload).unwrap();
let response = self.client.post(url).body(&body).dispatch();
RocketResponse(response.deref()).into()
let mut response = self.client.post(url).body(&body).dispatch();
RocketResponse(response.deref_mut()).into()
}
}

View file

@ -1,10 +1,10 @@
use function_name::named;
use http::{Response, StatusCode};
use percent_encoding::{percent_encode, NON_ALPHANUMERIC};
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::path::PathBuf;
use std::time::Duration;
use url::form_urlencoded::byte_serialize;
use crate::service::dto;
use crate::{config, ddns, index, vfs};
@ -20,6 +20,7 @@ const TEST_MOUNT_SOURCE: &str = "test/collection";
pub trait TestService {
fn new(db_name: &str) -> Self;
fn get(&mut self, url: &str) -> Response<()>;
fn get_bytes(&mut self, url: &str) -> Response<Vec<u8>>;
fn post(&mut self, url: &str) -> Response<()>;
fn delete(&mut self, url: &str) -> Response<()>;
fn get_json<T: DeserializeOwned>(&mut self, url: &str) -> T;
@ -274,8 +275,10 @@ fn test_service_browse() {
path.push("collection");
path.push("Khemmis");
path.push("Hunted");
let encoded_path: String = byte_serialize(path.to_string_lossy().as_ref().as_bytes()).collect();
let uri = format!("/api/browse/{}", encoded_path);
let uri = format!(
"/api/browse/{}",
percent_encode(path.to_string_lossy().as_ref().as_bytes(), NON_ALPHANUMERIC)
);
let entries: Vec<index::CollectionFile> = service.get_json(&uri);
assert_eq!(entries.len(), 5);
@ -336,7 +339,6 @@ fn test_service_search() {
}
}
/* TODO
#[named]
#[test]
fn test_service_serve() {
@ -345,29 +347,33 @@ fn test_service_serve() {
service.login();
service.index();
{
let mut response = client
.get("/api/serve/collection%2FKhemmis%2FHunted%2F02%20-%20Candlelight.mp3")
.dispatch();
assert_eq!(response.status(), Status::Ok);
let body = response.body().unwrap();
let body = body.into_bytes().unwrap();
assert_eq!(body.len(), 24_142);
}
let mut path = PathBuf::new();
path.push("collection");
path.push("Khemmis");
path.push("Hunted");
path.push("02 - Candlelight.mp3");
let uri = format!(
"/api/serve/{}",
percent_encode(path.to_string_lossy().as_ref().as_bytes(), NON_ALPHANUMERIC)
);
{
let mut response = client
.get("/api/serve/collection%2FKhemmis%2FHunted%2F02%20-%20Candlelight.mp3")
.header(Range::bytes(100, 299))
.dispatch();
assert_eq!(response.status(), Status::PartialContent);
let body = response.body().unwrap();
let body = body.into_bytes().unwrap();
assert_eq!(body.len(), 200);
assert_eq!(response.headers().get_one("Content-Length").unwrap(), "200");
}
let response = service.get_bytes(&uri);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.body().len(), 24_142);
// TODO
// {
// let mut response = client
// .get("/api/serve/collection%2FKhemmis%2FHunted%2F02%20-%20Candlelight.mp3")
// .header(Range::bytes(100, 299))
// .dispatch();
// assert_eq!(response.status(), Status::PartialContent);
// let body = response.body().unwrap();
// let body = body.into_bytes().unwrap();
// assert_eq!(body.len(), 200);
// assert_eq!(response.headers().get_one("Content-Length").unwrap(), "200");
// }
}
*/
#[named]
#[test]