2016-09-04 20:51:16 +00:00
|
|
|
#![feature(plugin)]
|
2016-09-09 03:38:58 +00:00
|
|
|
#![plugin(rocket_codegen)]
|
2016-09-04 20:51:16 +00:00
|
|
|
|
|
|
|
extern crate rocket;
|
|
|
|
|
|
|
|
use std::fmt;
|
2016-10-25 09:17:49 +00:00
|
|
|
use rocket::request::{self, Request, FromRequest};
|
|
|
|
use rocket::outcome::Outcome::*;
|
2016-09-04 20:51:16 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct HeaderCount(usize);
|
|
|
|
|
|
|
|
impl fmt::Display for HeaderCount {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 06:20:49 +00:00
|
|
|
impl<'r> FromRequest<'r> for HeaderCount {
|
2016-09-04 20:51:16 +00:00
|
|
|
type Error = ();
|
2016-10-25 09:17:49 +00:00
|
|
|
fn from_request(request: &'r Request) -> request::Outcome<Self, ()> {
|
|
|
|
Success(HeaderCount(request.headers().len()))
|
2016-09-04 20:51:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/")]
|
|
|
|
fn header_count(header_count: HeaderCount) -> String {
|
|
|
|
format!("Your request contained {} headers!", header_count)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2016-10-04 02:48:33 +00:00
|
|
|
rocket::ignite().mount("/", routes![header_count]).launch()
|
2016-09-04 20:51:16 +00:00
|
|
|
}
|
2016-10-16 10:16:16 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::rocket;
|
|
|
|
use rocket::testing::MockRequest;
|
|
|
|
use rocket::http::Method::*;
|
|
|
|
|
|
|
|
fn test_header_count<'h>(headers: &[(&'h str, &'h str)]) {
|
2016-12-15 08:47:31 +00:00
|
|
|
// FIXME: Should be able to count headers directly!
|
2016-10-16 10:16:16 +00:00
|
|
|
let rocket = rocket::ignite().mount("/", routes![super::header_count]);
|
2016-12-15 08:47:31 +00:00
|
|
|
let mut req = MockRequest::new(Get, "/").headers(headers);
|
2016-10-16 10:16:16 +00:00
|
|
|
let result = req.dispatch_with(&rocket);
|
|
|
|
assert_eq!(result.unwrap(),
|
|
|
|
format!("Your request contained {} headers!", headers.len()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_n_headers() {
|
|
|
|
for i in 0..50 {
|
|
|
|
let mut headers = vec![];
|
|
|
|
for j in 0..i {
|
|
|
|
let string = format!("{}", j);
|
|
|
|
headers.push((string.clone(), string));
|
|
|
|
}
|
|
|
|
|
|
|
|
let h_strs: Vec<_> = headers.iter()
|
|
|
|
.map(|&(ref a, ref b)| (a.as_str(), b.as_str()))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
test_header_count(&h_strs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|