2019-08-20 23:53:00 +00:00
|
|
|
#![feature(proc_macro_hygiene)]
|
2016-09-04 20:51:16 +00:00
|
|
|
|
2018-06-28 15:55:15 +00:00
|
|
|
#[macro_use] extern crate rocket;
|
2016-09-04 20:51:16 +00:00
|
|
|
|
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);
|
|
|
|
|
2020-01-31 09:34:15 +00:00
|
|
|
#[rocket::async_trait]
|
2016-12-16 11:07:23 +00:00
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for HeaderCount {
|
2019-04-09 03:57:47 +00:00
|
|
|
type Error = std::convert::Infallible;
|
2018-08-10 11:42:30 +00:00
|
|
|
|
2020-01-31 09:34:15 +00:00
|
|
|
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
2016-10-25 09:17:49 +00:00
|
|
|
Success(HeaderCount(request.headers().len()))
|
2016-09-04 20:51:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/")]
|
|
|
|
fn header_count(header_count: HeaderCount) -> String {
|
2018-08-10 11:42:30 +00:00
|
|
|
format!("Your request contained {} headers!", header_count.0)
|
2016-09-04 20:51:16 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 20:41:04 +00:00
|
|
|
fn rocket() -> rocket::Rocket {
|
|
|
|
rocket::ignite().mount("/", routes![header_count])
|
|
|
|
}
|
|
|
|
|
2016-09-04 20:51:16 +00:00
|
|
|
fn main() {
|
2019-08-25 02:19:11 +00:00
|
|
|
let _ = rocket().launch();
|
2016-09-04 20:51:16 +00:00
|
|
|
}
|
2016-10-16 10:16:16 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2017-06-06 20:41:04 +00:00
|
|
|
use rocket::local::Client;
|
2016-12-16 00:34:19 +00:00
|
|
|
use rocket::http::Header;
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
async fn test_header_count<'h>(headers: Vec<Header<'static>>) {
|
2020-06-14 15:57:53 +00:00
|
|
|
let client = Client::new(super::rocket()).await.unwrap();
|
2017-06-06 20:41:04 +00:00
|
|
|
let mut req = client.get("/");
|
2017-01-31 10:43:19 +00:00
|
|
|
for header in headers.iter().cloned() {
|
2017-06-06 20:41:04 +00:00
|
|
|
req.add_header(header);
|
2016-12-16 00:34:19 +00:00
|
|
|
}
|
2016-10-16 10:16:16 +00:00
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
let mut response = req.dispatch().await;
|
2017-01-31 10:43:19 +00:00
|
|
|
let expect = format!("Your request contained {} headers!", headers.len());
|
2019-08-24 17:27:10 +00:00
|
|
|
assert_eq!(response.body_string().await, Some(expect));
|
2016-10-16 10:16:16 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn test_n_headers() {
|
2016-10-16 10:16:16 +00:00
|
|
|
for i in 0..50 {
|
2017-06-06 20:41:04 +00:00
|
|
|
let headers = (0..i)
|
|
|
|
.map(|n| Header::new(n.to_string(), n.to_string()))
|
2017-01-31 10:43:19 +00:00
|
|
|
.collect();
|
2016-10-16 10:16:16 +00:00
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
test_header_count(headers).await;
|
2016-10-16 10:16:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|