Add some tests for the list handlers
This commit is contained in:
parent
4b33969427
commit
810d087508
|
@ -7,10 +7,11 @@ extern crate inflector;
|
|||
extern crate rocket;
|
||||
extern crate rocket_contrib;
|
||||
#[macro_use] extern crate serde_derive;
|
||||
extern crate serde_json;
|
||||
#[macro_use] extern crate serde_json;
|
||||
|
||||
mod handlers;
|
||||
mod models;
|
||||
#[cfg(test)] mod tests;
|
||||
|
||||
use rocket::Rocket;
|
||||
use std::sync::RwLock;
|
||||
|
|
|
@ -0,0 +1,114 @@
|
|||
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);
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
This is a test list
|
||||
|
||||
* One
|
||||
* Two
|
||||
* Three
|
|
@ -0,0 +1 @@
|
|||
contentB
|
Loading…
Reference in New Issue