From feb772bee8f888eef6261a97e507ccafdd19a3a1 Mon Sep 17 00:00:00 2001 From: sigoden Date: Wed, 10 Jan 2024 08:59:30 +0000 Subject: [PATCH] add more tests --- src/server.rs | 4 +++- tests/http.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/server.rs b/src/server.rs index 87afaefa8..dd64895e3 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1723,11 +1723,13 @@ fn guard_upload( if start > end || start >= range_size || end >= range_size - || (!allow_delete && start != size) || (allow_delete && start > size) { status_bad_request(res, message); (true, None) + } else if !allow_delete && start != size { + status_forbid(res); + (true, None) } else { (false, Some(start)) } diff --git a/tests/http.rs b/tests/http.rs index b396073f8..53be95b38 100644 --- a/tests/http.rs +++ b/tests/http.rs @@ -332,7 +332,7 @@ fn get_file_content_type(server: TestServer) -> Result<(), Error> { } #[rstest] -fn resumable_upload(#[with(&["-A"])] server: TestServer) -> Result<(), Error> { +fn resumable_upload(#[with(&["--allow-upload"])] server: TestServer) -> Result<(), Error> { let url = format!("{}file1", server.url()); let resp = fetch!(b"PUT", &url).body(b"abc".to_vec()).send()?; assert_eq!(resp.status(), 201); @@ -352,3 +352,61 @@ fn resumable_upload(#[with(&["-A"])] server: TestServer) -> Result<(), Error> { assert_eq!(resp.text().unwrap(), "abcabc"); Ok(()) } + +#[rstest] +fn partial_upload( + #[with(&["--allow-upload", "--allow-delete"])] server: TestServer, +) -> Result<(), Error> { + let url = format!("{}file1", server.url()); + let resp = fetch!(b"PUT", &url).body(b"abc".to_vec()).send()?; + assert_eq!(resp.status(), 201); + let resp = fetch!(b"PUT", &url) + .header("content-range", "bytes 0-0/6") + .body(b"n".to_vec()) + .send()?; + assert_eq!(resp.status(), 200); + let resp = reqwest::blocking::get(url)?; + assert_eq!(resp.status(), 200); + assert_eq!(resp.text().unwrap(), "nbc"); + Ok(()) +} + +#[rstest] +fn upload_content_range(#[with(&["--allow-upload"])] server: TestServer) -> Result<(), Error> { + let url = format!("{}file1", server.url()); + let resp = fetch!(b"PUT", &url).body(b"abc".to_vec()).send()?; + assert_eq!(resp.status(), 201); + let resp = fetch!(b"PUT", &url) + .body(b"abc".to_vec()) + .header("content-range", "bytes */3") + .send()?; + assert_eq!(resp.status(), 400); + let resp = fetch!(b"PUT", &url) + .body(b"bc".to_vec()) + .header("content-range", "bytes 1-2/3") + .send()?; + assert_eq!(resp.status(), 403); + let resp = fetch!(b"PUT", &url).body(b"abc".to_vec()).send()?; + assert_eq!(resp.status(), 403); + Ok(()) +} + +#[rstest] +fn upload_content_range_allow_delete( + #[with(&["--allow-upload", "--allow-delete"])] server: TestServer, +) -> Result<(), Error> { + let url = format!("{}file1", server.url()); + let resp = fetch!(b"PUT", &url).body(b"abc".to_vec()).send()?; + assert_eq!(resp.status(), 201); + let resp = fetch!(b"PUT", &url) + .body(b"abc".to_vec()) + .header("content-range", "bytes 4-6/7") + .send()?; + assert_eq!(resp.status(), 400); + let resp = fetch!(b"PUT", &url) + .body(b"abc".to_vec()) + .header("content-range", "bytes 3-5/6") + .send()?; + assert_eq!(resp.status(), 200); + Ok(()) +}