Skip to content

Commit

Permalink
test(server): add a compile-fail test to ensure reasonable error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryman committed Jul 6, 2015
1 parent 6a48868 commit 2cdac29
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tests/compile-fail/unmet_data_requirement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#[macro_use] extern crate nickel;
extern crate cookie;

use nickel::{Nickel, HttpRouter, Cookies, QueryString};
use nickel::cookies;
use cookie::Cookie;

struct Data {
secret_key: cookies::SecretKey
}

fn main() {
let data = Data { secret_key: cookies::SecretKey([0; 32]) };
let mut server = Nickel::with_data(data);

// Try curl -b MyCookie=bar localhost:6767
server.get("/", middleware! { |req|
let cookie = req.cookies().find("MyCookie");
//~^ ERROR: the trait `core::convert::AsRef<nickel::cookies::SecretKey>` is not implemented for the type `Data`
format!("MyCookie={:?}", cookie.map(|c| c.value))
});

// Note: Don't use get for login in real applications ;)
// Try http://localhost:6767/login?name=foo
server.get("/login", middleware! { |req, mut res|
let jar = res.cookies_mut()
//~^ ERROR: the trait `core::convert::AsRef<nickel::cookies::SecretKey>` is not implemented for the type `Data`
// long life cookies!
.permanent();

let name = req.query().get("name")
.unwrap_or("default_name");
let cookie = Cookie::new("MyCookie".to_owned(),
name.to_owned());
jar.add(cookie);

"Cookie set!"
});

server.listen("127.0.0.1:6767");
}

0 comments on commit 2cdac29

Please sign in to comment.