Handle bad json.
This commit is contained in:
parent
e7c7d9f270
commit
80124af836
@ -58,10 +58,18 @@ impl Document {
|
||||
reply.add_data("error_type", ErrorType::DocumentAlreadyExists);
|
||||
self.queue.send(reply).unwrap();
|
||||
return;
|
||||
},
|
||||
None => {},
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
let doc: Value = serde_json::from_str(&msg.get_data("doc").unwrap().to_string()).unwrap();
|
||||
let doc: Value = match serde_json::from_str(&msg.get_data("doc").unwrap().to_string()) {
|
||||
Ok(value) => value,
|
||||
Err(_) => {
|
||||
let mut reply = msg.reply(MsgType::Error);
|
||||
reply.add_data("error_type", ErrorType::DocumentInvalidRequest);
|
||||
self.queue.send(reply).unwrap();
|
||||
return;
|
||||
}
|
||||
};
|
||||
self.data
|
||||
.insert(name, doc["template"].as_str().unwrap().to_string());
|
||||
self.queue.send(msg.reply(MsgType::ActionOk)).unwrap();
|
||||
@ -220,7 +228,7 @@ pub mod documents {
|
||||
let reply = rx.recv_timeout(TIMEOUT).unwrap();
|
||||
assert_eq!(reply.get_id(), msg.get_id());
|
||||
match reply.get_msg_type() {
|
||||
MsgType::Error=> {},
|
||||
MsgType::Error => {}
|
||||
_ => unreachable!(
|
||||
"got '{:?}': should have received document",
|
||||
reply.get_msg_type()
|
||||
@ -238,4 +246,30 @@ pub mod documents {
|
||||
let result = binding.get_data("doc").unwrap();
|
||||
assert_eq!(result.to_string(), expected.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_json() {
|
||||
let (queue, rx) = setup_document();
|
||||
let mut msg = Message::new(MsgType::DocumentRequest);
|
||||
msg.add_data("action", ActionType::Add);
|
||||
msg.add_data("name", "doc");
|
||||
msg.add_data("doc", "Invalid json request.");
|
||||
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 '{:?}': should have received document",
|
||||
reply.get_msg_type()
|
||||
),
|
||||
}
|
||||
match reply.get_data("error_type") {
|
||||
Some(err) => match err.to_error_type().unwrap() {
|
||||
ErrorType::DocumentInvalidRequest => {}
|
||||
_ => unreachable!("got {:?}: should have been bad request'", err),
|
||||
},
|
||||
None => unreachable!("should contain error type"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -233,7 +233,7 @@ mod fields {
|
||||
let field: Field = err.into();
|
||||
match field {
|
||||
Field::ErrorType(data) => match data {
|
||||
ErrorType::DocumentNotFound => {}
|
||||
ErrorType::DocumentNotFound => {}
|
||||
_ => unreachable!("got {:?}: should have been Document not found", data),
|
||||
},
|
||||
_ => unreachable!("should have been an error type"),
|
||||
@ -252,7 +252,7 @@ mod fields {
|
||||
let field: Field = err.into();
|
||||
let result = field.to_error_type().unwrap();
|
||||
match result {
|
||||
ErrorType::DocumentNotFound => {}
|
||||
ErrorType::DocumentNotFound => {}
|
||||
_ => unreachable!("got {:?}: should have been document not found", result),
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ pub enum ActionType {
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum ErrorType {
|
||||
DocumentAlreadyExists,
|
||||
DocumentInvalidRequest,
|
||||
DocumentNotFound,
|
||||
}
|
||||
|
||||
@ -75,7 +76,7 @@ mod mtt_replies {
|
||||
let reply = MTTReply::new(msg);
|
||||
match reply.get_error() {
|
||||
Some(err) => match err {
|
||||
ErrorType::DocumentNotFound => {},
|
||||
ErrorType::DocumentNotFound => {}
|
||||
_ => unreachable!("got {:?}: should have been document not found", err),
|
||||
},
|
||||
None => unreachable!("should return an error type"),
|
||||
|
23
src/main.rs
23
src/main.rs
@ -106,7 +106,9 @@ async fn mtt_conn(
|
||||
let status = match reply.get_error() {
|
||||
Some(err) => match err {
|
||||
ErrorType::DocumentAlreadyExists => StatusCode::CONFLICT,
|
||||
ErrorType::DocumentInvalidRequest => StatusCode::BAD_REQUEST,
|
||||
ErrorType::DocumentNotFound => StatusCode::NOT_FOUND,
|
||||
// _ => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
},
|
||||
None => StatusCode::OK,
|
||||
};
|
||||
@ -261,4 +263,25 @@ mod servers {
|
||||
"do not allow post to existing documents"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn invalid_json() {
|
||||
let app = create_app(MoreThanText::new()).await;
|
||||
let response = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method(Method::POST)
|
||||
.uri("/api/something")
|
||||
.body("Some invalid json.".to_string())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
response.status(),
|
||||
StatusCode::BAD_REQUEST,
|
||||
"do not allow post to existing documents"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user