use rocket::Config;
use rocket::http::Status;
use rocket::error::LaunchError;
use rocket::config::Environment;
use rocket_contrib::json::Json;
use crate::submission::Submission;
use crate::ResultsFile;
#[get("/")]
fn return_ok() -> Status {
Status::Ok
}
#[post("/submit", format = "application/json", data = "<submission>")]
fn accept_submission(submission: Json<Submission>) -> Status {
let sub = submission.into_inner();
let mut rf = ResultsFile::for_item(&sub).expect("Could not open results file");
if rf.write_csv(&sub).is_ok() {
return Status::Accepted;
} else {
eprintln!("Error: Could not write following submission");
eprintln!("{:#?}", sub);
return Status::InternalServerError;
}
}
pub fn run(port: u16) -> LaunchError {
#[cfg(debug_assertions)]
let builder = Config::build(Environment::Development);
#[cfg(not(debug_assertions))]
let builder = Config::build(Environment::Production);
let config = builder
.address("0.0.0.0")
.port(port)
.finalize()
.expect("Could not build submission server");
return rocket::custom(config)
.mount("/", routes![return_ok, accept_submission])
.launch();
}
#[cfg(test)]
mod tests {
use super::*;
use rocket::local::Client;
use rocket::http::Header;
fn client() -> Client {
let rocket = rocket::ignite().mount("/", routes![return_ok, accept_submission]);
Client::new(rocket).expect("valid rocket instance")
}
#[test]
fn test_server_runs() {
let client = client();
let resp = client.get("/").dispatch();
assert_eq!(resp.status(), Status::Ok);
}
#[test]
fn test_404() {
let client = client();
let resp = client.get("/not_a_route").dispatch();
assert_eq!(resp.status(), Status::NotFound);
}
#[test]
fn test_422_unprocessable_entity() {
let client = client();
let resp = client.post("/submit")
.body(r#"{"key":"value"}"#)
.header(Header::new("Content-Type", "application/json"))
.dispatch();
assert_eq!(resp.status(), Status::UnprocessableEntity)
}
#[test]
fn test_accept_submission() {
let client = client();
let sub = Submission::new();
let req = client.post("/submit")
.body(serde_json::to_string(&sub).unwrap())
.header(Header::new("Content-Type", "application/json"))
.dispatch();
assert_eq!(req.status(), Status::Accepted);
}
}