From 4774de665239abee7f45adc6d81471dd8e253152 Mon Sep 17 00:00:00 2001 From: "Helmut K. C. Tessarek" Date: Mon, 11 Mar 2024 09:00:45 -0400 Subject: [PATCH] refactor(server): use more specific HTTP status codes (#262) * refactor(server): use more specific http status codes * fix: clippy error - oops missed that one * test(fixtures): add check for status code --- fixtures/test-file-upload-same-filename/test.sh | 2 ++ fixtures/test-server-mime-blacklist/test.sh | 1 + src/paste.rs | 14 +++++--------- src/server.rs | 7 +------ 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/fixtures/test-file-upload-same-filename/test.sh b/fixtures/test-file-upload-same-filename/test.sh index 1bef175..ac88454 100755 --- a/fixtures/test-file-upload-same-filename/test.sh +++ b/fixtures/test-file-upload-same-filename/test.sh @@ -13,6 +13,8 @@ run_test() { test "$content" = "$(curl -s $file_url)" file_url=$(curl -s -F "file=@file" -H "filename:fn_from_header.txt" localhost:8000) test "$file_url" = "file already exists" + status_code=$(curl -s -F "file=@file" -H "filename:fn_from_header.txt" -w "%{response_code}" -o /dev/null localhost:8000) + test "$status_code" = "409" } teardown() { diff --git a/fixtures/test-server-mime-blacklist/test.sh b/fixtures/test-server-mime-blacklist/test.sh index bdd9269..c93402f 100755 --- a/fixtures/test-server-mime-blacklist/test.sh +++ b/fixtures/test-server-mime-blacklist/test.sh @@ -11,6 +11,7 @@ setup() { run_test() { test "this file type is not permitted" = "$(curl -s -F "file=@file.html" localhost:8000)" test "this file type is not permitted" = "$(curl -s -F "file=@file.xml" localhost:8000)" + test "415" = "$(curl -s -F "file=@file.xml" -w "%{response_code}" -o /dev/null localhost:8000)" file_url=$(curl -s -F "file=@file.txt" localhost:8000) test "$content" = "$(curl -s $file_url)" } diff --git a/src/paste.rs b/src/paste.rs index e39c827..0b6261d 100644 --- a/src/paste.rs +++ b/src/paste.rs @@ -98,14 +98,13 @@ impl Paste { expiry_date: Option, header_filename: Option, config: &Config, - ) -> IoResult { + ) -> Result { let file_type = infer::get(&self.data); if let Some(file_type) = file_type { for mime_type in &config.paste.mime_blacklist { if mime_type == file_type.mime_type() { - return Err(IoError::new( - IoErrorKind::Other, - String::from("this file type is not permitted"), + return Err(error::ErrorUnsupportedMediaType( + "this file type is not permitted", )); } } @@ -179,10 +178,7 @@ impl Paste { let file_path = util::glob_match_file(path.clone()) .map_err(|_| IoError::new(IoErrorKind::Other, String::from("path is not valid")))?; if file_path.is_file() && file_path.exists() { - return Err(IoError::new( - IoErrorKind::AlreadyExists, - String::from("file already exists\n"), - )); + return Err(error::ErrorConflict("file already exists\n")); } if let Some(timestamp) = expiry_date { path.set_file_name(format!("{file_name}.{timestamp}")); @@ -248,7 +244,7 @@ impl Paste { .to_string()); } } - Ok(self.store_file(file_name, expiry_date, None, &config)?) + self.store_file(file_name, expiry_date, None, &config) } /// Writes an URL to a file in upload directory. diff --git a/src/server.rs b/src/server.rs index dbd9cbf..6fe1e41 100644 --- a/src/server.rs +++ b/src/server.rs @@ -951,15 +951,10 @@ mod tests { .to_request(), ) .await; - assert_eq!(StatusCode::INTERNAL_SERVER_ERROR, response.status()); + assert_eq!(StatusCode::CONFLICT, response.status()); assert_body(response.into_body(), "file already exists\n").await?; fs::remove_file(header_filename)?; - let serve_request = TestRequest::get() - .uri(&format!("/{header_filename}")) - .to_request(); - let response = test::call_service(&app, serve_request).await; - assert_eq!(StatusCode::NOT_FOUND, response.status()); Ok(()) }