Patch errors if page is missing.
This commit is contained in:
parent
cb7526dd45
commit
5502013b76
@ -41,25 +41,41 @@ impl Document {
|
|||||||
loop {
|
loop {
|
||||||
let msg = self.rx.recv().unwrap();
|
let msg = self.rx.recv().unwrap();
|
||||||
match msg.get_data("action") {
|
match msg.get_data("action") {
|
||||||
Some(action) => match action.to_action().unwrap() {
|
Some(action_field) => {
|
||||||
ActionType::Add => self.add(msg),
|
let action = action_field.to_action().unwrap();
|
||||||
_ => self.get(msg),
|
match action {
|
||||||
},
|
ActionType::Add | ActionType::Update => self.add(action, msg),
|
||||||
|
_ => self.get(msg),
|
||||||
|
}
|
||||||
|
}
|
||||||
None => self.get(msg),
|
None => self.get(msg),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add(&mut self, msg: Message) {
|
fn add(&mut self, action: ActionType, msg: Message) {
|
||||||
let name = msg.get_data("name").unwrap().to_string();
|
let name = msg.get_data("name").unwrap().to_string();
|
||||||
match self.data.get(&name) {
|
match self.data.get(&name) {
|
||||||
Some(_) => {
|
Some(_) => match action {
|
||||||
self.queue
|
ActionType::Add => {
|
||||||
.send(msg.reply_with_error(ErrorType::DocumentAlreadyExists))
|
self.queue
|
||||||
.unwrap();
|
.send(msg.reply_with_error(ErrorType::DocumentAlreadyExists))
|
||||||
return;
|
.unwrap();
|
||||||
}
|
return;
|
||||||
None => {}
|
}
|
||||||
|
ActionType::Update => {}
|
||||||
|
_ => unreachable!("listen should prevent anything else"),
|
||||||
|
},
|
||||||
|
None => match action {
|
||||||
|
ActionType::Add => {}
|
||||||
|
ActionType::Update => {
|
||||||
|
self.queue
|
||||||
|
.send(msg.reply_with_error(ErrorType::DocumentNotFound))
|
||||||
|
.unwrap();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_ => unreachable!("listen should prevent anything else"),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
let doc: Value = match serde_json::from_str(&msg.get_data("doc").unwrap().to_string()) {
|
let doc: Value = match serde_json::from_str(&msg.get_data("doc").unwrap().to_string()) {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
@ -251,8 +267,8 @@ pub mod documents {
|
|||||||
#[test]
|
#[test]
|
||||||
fn invalid_json() {
|
fn invalid_json() {
|
||||||
let inputs = ["Invalid json request.", "{}"];
|
let inputs = ["Invalid json request.", "{}"];
|
||||||
|
let (queue, rx) = setup_document();
|
||||||
for input in inputs.into_iter() {
|
for input in inputs.into_iter() {
|
||||||
let (queue, rx) = setup_document();
|
|
||||||
let mut msg = Message::new(MsgType::DocumentRequest);
|
let mut msg = Message::new(MsgType::DocumentRequest);
|
||||||
msg.add_data("action", ActionType::Add);
|
msg.add_data("action", ActionType::Add);
|
||||||
msg.add_data("name", "doc");
|
msg.add_data("name", "doc");
|
||||||
@ -282,4 +298,30 @@ pub mod documents {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn patch_nonexistant_page() {
|
||||||
|
let (queue, rx) = setup_document();
|
||||||
|
let input = json!({
|
||||||
|
"template": "Sothing here"
|
||||||
|
});
|
||||||
|
let mut msg = Message::new(MsgType::DocumentRequest);
|
||||||
|
msg.add_data("action", ActionType::Update);
|
||||||
|
msg.add_data("name", "something");
|
||||||
|
msg.add_data("doc", input.to_string());
|
||||||
|
queue.send(msg.clone()).unwrap();
|
||||||
|
let reply = rx.recv_timeout(TIMEOUT).unwrap();
|
||||||
|
assert_eq!(reply.get_id(), msg.get_id());
|
||||||
|
match reply.get_msg_type() {
|
||||||
|
MsgType::Error => {}
|
||||||
|
_ => unreachable!("got {:?}: shoud have been error", reply.get_msg_type()),
|
||||||
|
}
|
||||||
|
match reply.get_data("error_type") {
|
||||||
|
Some(err) => match err.to_error_type().unwrap() {
|
||||||
|
ErrorType::DocumentNotFound => {}
|
||||||
|
_ => unreachable!("got {:?}: should have been document not found'", err),
|
||||||
|
},
|
||||||
|
None => unreachable!("should contain error type"),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
69
src/main.rs
69
src/main.rs
@ -45,7 +45,7 @@ async fn create_app(state: MoreThanText) -> Router {
|
|||||||
Router::new()
|
Router::new()
|
||||||
.route("/", get(mtt_conn))
|
.route("/", get(mtt_conn))
|
||||||
.route("/{document}", get(mtt_conn))
|
.route("/{document}", get(mtt_conn))
|
||||||
.route("/api/{document}", post(mtt_conn))
|
.route("/api/{document}", post(mtt_conn).patch(mtt_conn))
|
||||||
.layer(CookieManagerLayer::new())
|
.layer(CookieManagerLayer::new())
|
||||||
.layer(Extension(state.clone()))
|
.layer(Extension(state.clone()))
|
||||||
.with_state(state)
|
.with_state(state)
|
||||||
@ -91,6 +91,7 @@ async fn mtt_conn(
|
|||||||
let action = match method {
|
let action = match method {
|
||||||
Method::GET => ActionType::Get,
|
Method::GET => ActionType::Get,
|
||||||
Method::POST => ActionType::Add,
|
Method::POST => ActionType::Add,
|
||||||
|
Method::PATCH => ActionType::Update,
|
||||||
_ => unreachable!("reouter should prevent this"),
|
_ => unreachable!("reouter should prevent this"),
|
||||||
};
|
};
|
||||||
let doc = match path.get("document") {
|
let doc = match path.get("document") {
|
||||||
@ -305,4 +306,70 @@ mod servers {
|
|||||||
"do not allow post to existing documents"
|
"do not allow post to existing documents"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn patch_root() {
|
||||||
|
let content = format!("content-{}", Uuid::new_v4());
|
||||||
|
let document = json!({
|
||||||
|
"template": content.clone()
|
||||||
|
});
|
||||||
|
let app = create_app(MoreThanText::new()).await;
|
||||||
|
let response = app
|
||||||
|
.clone()
|
||||||
|
.oneshot(
|
||||||
|
Request::builder()
|
||||||
|
.method(Method::PATCH)
|
||||||
|
.uri("/api/root".to_string())
|
||||||
|
.body(document.to_string())
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
response.status(),
|
||||||
|
StatusCode::OK,
|
||||||
|
"failed to patch /api/root",
|
||||||
|
);
|
||||||
|
let response = app
|
||||||
|
.oneshot(
|
||||||
|
Request::builder()
|
||||||
|
.uri("/".to_string())
|
||||||
|
.body(Body::empty())
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
response.status(),
|
||||||
|
StatusCode::OK,
|
||||||
|
"failed to get to home page",
|
||||||
|
);
|
||||||
|
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||||
|
assert_eq!(body, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn patch_missing_page() {
|
||||||
|
let content = format!("content-{}", Uuid::new_v4());
|
||||||
|
let document = json!({
|
||||||
|
"template": content.clone()
|
||||||
|
});
|
||||||
|
let app = create_app(MoreThanText::new()).await;
|
||||||
|
let response = app
|
||||||
|
.clone()
|
||||||
|
.oneshot(
|
||||||
|
Request::builder()
|
||||||
|
.method(Method::PATCH)
|
||||||
|
.uri("/api/something".to_string())
|
||||||
|
.body(document.to_string())
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
response.status(),
|
||||||
|
StatusCode::NOT_FOUND,
|
||||||
|
"failed to patch /api/somethingt",
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user