Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Jan 10, 2024
1 parent 9e691a4 commit feb772b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
60 changes: 59 additions & 1 deletion tests/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(())
}

0 comments on commit feb772b

Please sign in to comment.