Skip to content

Implement DELETE method #4

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,4 @@ base URL to prepend to the `POST` responses will be configured using a config fi
After implementing a very basic API with `GET` and `POST` requests, I'm planning to add the following features.
- [x] HTTP basic authentication for posting/deleting pastes.
- [x] Syntax highlighting with [syntect](https://github.com/trishume/syntect).
- [ ] `DELETE` method to delete pastes.
- [ ] Configuration via a config file.
48 changes: 48 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
.service(
web::resource("/")
.route(web::post().to_async(new_paste))
.wrap(basic_auth.clone()),
)
.service(
web::resource("/{paste_id}")
.route(web::delete().to_async(delete_paste))
.wrap(basic_auth),
)
.service(send_paste)
Expand Down Expand Up @@ -107,6 +112,20 @@ fn new_paste(
})
}

fn delete_paste(
config: web::Data<Config>,
paste_id: web::Path<String>,
) -> impl Future<Item = HttpResponse, Error = actix_web::Error> {
web::block(move || {
let file_path = format!("{}/{}", config.paste_dir, paste_id);
fs::remove_file(file_path)
})
.then(|res| match res {
Ok(_) => Ok(HttpResponse::Ok().body("")),
Err(_) => Ok(HttpResponse::NotFound().into()),
})
}

#[get("/{paste_id}")]
fn send_paste(
config: web::Data<Config>,
Expand Down Expand Up @@ -285,6 +304,35 @@ mod tests {
assert_eq!(paste_content, file_content);
}

#[test]
fn delete_paste_file() {
let test_dir = TempDir::new().unwrap();
let config = make_test_config(test_dir.path().to_str().unwrap());

let data = web::Data::new(config.clone());
let mut app = test::init_service(
App::new()
.register_data(data)
.route("/{paste_id}", web::delete().to_async(delete_paste)),
);

let paste_id = "/testpaste";
let paste_path = config.paste_dir + paste_id;
{
let paste_content = b"nonsense";
let mut file = File::create(&paste_path).unwrap();
file.write_all(paste_content).unwrap();
}

let req = test::TestRequest::delete().uri(paste_id).to_request();
let resp = test::call_service(&mut app, req);
assert_eq!(resp.status(), http::StatusCode::OK);
assert!(
File::open(paste_path).is_err(),
"Deleted paste file should not exist"
);
}

#[test]
fn auth_valid_creds() {
let config = make_test_config("unused path");
Expand Down