Move the tests for the list handlers to the right module

Also improve and expand the tests abit.  Use serde_json to parse the
results an interpret them instead on depending on response body string
matching (which fails for changing mtimes of the test files).
This commit is contained in:
Paul van Tilburg 2018-01-01 21:15:30 +01:00
parent d167c50918
commit e98324e3fe
3 changed files with 136 additions and 115 deletions

View File

@ -30,3 +30,139 @@ fn update(list_id: String, new_list: Json<List>, lists: State<ListStore>) -> Opt
list.update_data(&new_list.data);
Some(Json(list.clone()))
}
#[cfg(test)]
mod tests {
use rocket;
use rocket::http::{Accept, ContentType, Status};
use rocket::local::Client;
use serde_json;
use super::*;
#[test]
fn index() {
let client = Client::new(rocket(Some("test"))).unwrap();
// Try to get all the lists
let mut res = client.get("/lists").header(Accept::JSON).dispatch();
assert_eq!(res.status(), Status::Ok);
let body = res.body_string().unwrap();
let lists = serde_json::from_str::<Vec<List>>(body.as_str()).unwrap();
assert_eq!(lists[0].id, "test");
assert_eq!(lists[0].index, 0);
assert_eq!(lists[0].name, "Test");
assert_eq!(lists[0].data, "This is a test list\n\n* One\n* Two\n* Three\n");
// The mtime field can vary, don't test for it
// The path field is private, also don't test for it
// Cannot get the lists in HTML format
let res = client.get("/lists").header(Accept::HTML).dispatch();
assert_eq!(res.status(), Status::NotFound);
}
#[test]
fn show_html() {
let client = Client::new(rocket(Some("test"))).unwrap();
// Try to get the list and verify the body
let mut res = client.get("/lists/test").header(Accept::HTML).dispatch();
assert_eq!(res.status(), Status::Ok);
let body = res.body_string().unwrap();
assert_eq!(body,
r#"<p>This is a test list</p>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
"#);
}
#[test]
fn show_json() {
let client = Client::new(rocket(Some("test"))).unwrap();
// Try to get the list and verify the body
let mut res = client.get("/lists/test").header(Accept::JSON).dispatch();
assert_eq!(res.status(), Status::Ok);
let body = res.body_string().unwrap();
let list = serde_json::from_str::<List>(body.as_str()).unwrap();
assert_eq!(list.id, "test");
assert_eq!(list.index, 0);
assert_eq!(list.name, "Test");
assert_eq!(list.data, "This is a test list\n\n* One\n* Two\n* Three\n");
// The mtime field can vary, don't test for it
// The path field is private, also don't test for it
// Try to get a list that doesn't exist
let res = client.get("/lists/doesntexit").header(Accept::JSON).dispatch();
assert_eq!(res.status(), Status::NotFound);
// FIXME: Test that there is some kind of error in the JSON
}
#[test]
fn update() {
let client = Client::new(rocket(Some("test"))).unwrap();
// Try to get the list and determine what to change it to
let mut res = client.get("/lists/updatable").header(Accept::JSON).dispatch();
let body = res.body_string().unwrap();
let list = serde_json::from_str::<List>(body.as_str()).unwrap();
// Switch contentA with contentB and vice versa
let new_data = if list.data == "contentA" {
"contentB"
} else {
"contentA"
};
let new_json = json!({
"id": "updatable",
"index": 1,
"data": new_data,
"mtime": {
"secs_since_epoch": 0,
"nanos_since_epoch": 0
},
"name": "Updatable",
"path": "test/lists/updatablelist"
});
// Try to change the list data, then verify it was changed
let res = client.put("/lists/updatable")
.header(ContentType::JSON)
.body(new_json.to_string())
.dispatch();
assert_eq!(res.status(), Status::Ok);
let mut res = client.get("/lists/updatable")
.header(Accept::JSON)
.dispatch();
let body = res.body_string().unwrap();
let list = serde_json::from_str::<List>(body.as_str()).unwrap();
assert_eq!(list.data, new_data);
// Try to change a list that doesn't exist
let res = client.put("/lists/doesntexit")
.header(ContentType::JSON)
.body(new_json.to_string())
.dispatch();
assert_eq!(res.status(), Status::NotFound);
// Try to change a list without a proper body
let res = client.put("/lists/updatable")
.header(ContentType::JSON)
.body(r#"{}"#)
.dispatch();
assert_eq!(res.status(), Status::BadRequest);
// Try to change a list without a proper type (i.e. not JSON)
let res = client.put("/lists/updatable")
.header(ContentType::Plain)
.body("foo bar baz")
.dispatch();
assert_eq!(res.status(), Status::NotFound);
}
}

View File

@ -11,7 +11,6 @@ extern crate rocket_contrib;
mod handlers;
mod models;
#[cfg(test)] mod tests;
use rocket::Rocket;
use std::sync::RwLock;

View File

@ -1,114 +0,0 @@
use rocket;
use rocket::http::{Accept, ContentType, Status};
use rocket::local::Client;
#[test]
fn lists_index() {
let client = Client::new(rocket(Some("test"))).unwrap();
// Try to get all the lists
let mut res = client.get("/lists").header(Accept::JSON).dispatch();
assert_eq!(res.status(), Status::Ok);
let body = res.body_string().unwrap();
assert!(body.contains(r#"{"id":"test","#));
assert!(body.contains(r#"{"id":"updatable","#));
// Cannot get the lists in HTML format
let res = client.get("/lists").header(Accept::HTML).dispatch();
assert_eq!(res.status(), Status::NotFound);
}
#[test]
fn lists_show_html() {
let client = Client::new(rocket(Some("test"))).unwrap();
// Try to get the list and verify the body
let mut res = client.get("/lists/test").header(Accept::HTML).dispatch();
assert_eq!(res.status(), Status::Ok);
let body = res.body_string().unwrap();
println!("html body: {:?}", body);
assert_eq!(body,
r#"<p>This is a test list</p>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
"#);
}
#[test]
fn lists_show_json() {
let client = Client::new(rocket(Some("test"))).unwrap();
// Try to get the list and verify the body
let mut res = client.get("/lists/test").header(Accept::JSON).dispatch();
assert_eq!(res.status(), Status::Ok);
let body = res.body_string().unwrap();
assert_eq!(body,
r#"{"id":"test","index":0,"data":"This is a test list\n\n* One\n* Two\n* Three\n","mtime":{"secs_since_epoch":1514818496,"nanos_since_epoch":59595664},"name":"Test","path":"test/lists/test.list"}"#);
// Try to get a list that doesn't exist
let res = client.get("/lists/doesntexit").header(Accept::JSON).dispatch();
assert_eq!(res.status(), Status::NotFound);
// FIXME: Test that there is some kind of error in the JSON
}
#[test]
fn lists_update() {
let client = Client::new(rocket(Some("test"))).unwrap();
// Try to get the list and determine what to change it too
let mut res = client.get("/lists/updatable").header(Accept::JSON).dispatch();
let body = res.body_string().unwrap();
let new_data = if body.contains("contentA") {
"contentB"
} else {
"contentA"
};
let new_json = json!({
"id": "updatable",
"index": 1,
"data": new_data,
"mtime": {
"secs_since_epoch": 0,
"nanos_since_epoch": 0
},
"name": "Updatable",
"path": "test/lists/updatablelist"
});
let new_body = format!("{}", new_json);
// Try to change the list data, then verify it was changed
let res = client.put("/lists/updatable")
.header(ContentType::JSON)
.body(new_body.clone())
.dispatch();
assert_eq!(res.status(), Status::Ok);
let mut res = client.get("/lists/updatable")
.header(Accept::JSON)
.dispatch();
let body = res.body_string().unwrap();
assert!(body.contains(new_data));
// Try to change a list that doesn't exist
// Try to change a list without a proper body
let res = client.put("/lists/updatable")
.header(ContentType::JSON)
.body(r#"{}"#)
.dispatch();
assert_eq!(res.status(), Status::BadRequest);
// Try to change a list without a proper type
let res = client.put("/lists/doesntexit")
.header(ContentType::JSON)
.body(new_body.clone())
.dispatch();
assert_eq!(res.status(), Status::NotFound);
}