Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(server): use more specific HTTP status codes #262

Merged
merged 3 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions fixtures/test-file-upload-same-filename/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions fixtures/test-server-mime-blacklist/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ setup() {
run_test() {
test "this file type is not permitted" = "$(curl -s -F "[email protected]" localhost:8000)"
test "this file type is not permitted" = "$(curl -s -F "[email protected]" localhost:8000)"
test "415" = "$(curl -s -F "[email protected]" -w "%{response_code}" -o /dev/null localhost:8000)"
file_url=$(curl -s -F "[email protected]" localhost:8000)
test "$content" = "$(curl -s $file_url)"
}
Expand Down
14 changes: 5 additions & 9 deletions src/paste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,13 @@ impl Paste {
expiry_date: Option<u128>,
header_filename: Option<String>,
config: &Config,
) -> IoResult<String> {
) -> Result<String, Error> {
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",
));
}
}
Expand Down Expand Up @@ -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}"));
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 1 addition & 6 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,15 +951,10 @@ mod tests {
.to_request(),
)
.await;
assert_eq!(StatusCode::INTERNAL_SERVER_ERROR, response.status());
assert_eq!(StatusCode::CONFLICT, response.status());
orhun marked this conversation as resolved.
Show resolved Hide resolved
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());
orhun marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}
Expand Down
Loading