Cleanup request tests; move into separate file.

This commit is contained in:
Sergio Benitez 2017-03-15 19:20:20 -07:00
parent 47fe659ebe
commit ca30e5e901
3 changed files with 52 additions and 101 deletions

View File

@ -6,6 +6,9 @@ mod form;
mod from_request;
mod state;
#[cfg(test)]
mod tests;
pub use self::request::Request;
pub use self::from_request::{FromRequest, Outcome};
pub use self::param::{FromParam, FromSegments};
@ -15,101 +18,3 @@ pub use self::state::State;
/// Type alias to retrieve [Flash](/rocket/response/struct.Flash.html) messages
/// from a request.
pub type FlashMessage = ::response::Flash<()>;
#[cfg(test)]
mod tests {
/// These tests are related to Issue#223
/// The way we were getting the headers from hyper
/// was causing a list to come back as a comma separated
/// list of entries.
use super::Request;
use super::super::http::hyper::header::Headers;
use hyper::method::Method;
use hyper::uri::RequestUri;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::collections::HashMap;
fn check_headers(test_headers: HashMap<String, Vec<String>>) {
let h_method: Method = Method::Get;
let h_uri: RequestUri = RequestUri::AbsolutePath("/test".to_string());
let h_addr: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8000);
let mut h_headers: Headers = Headers::new();
for (key, values) in &test_headers {
let raw_bytes: Vec<Vec<u8>> = values
.iter()
.map(|v| v.clone().into_bytes())
.collect();
h_headers.set_raw(key.clone(), raw_bytes);
}
let req = match Request::from_hyp(h_method, h_headers, h_uri, h_addr) {
Ok(req) => req,
Err(e) => panic!("Building Request failed: {:?}", e),
};
let r_headers = req.headers();
for (key, values) in &test_headers {
for (v1, v2) in values.iter().zip(r_headers.get(&key)) {
assert_eq!(v1, v2)
}
}
}
#[test]
fn test_single_header_single_entry() {
let mut test_headers = HashMap::new();
test_headers.insert("friends".to_string(), vec![
"alice".to_string(),
]);
check_headers(test_headers);
}
#[test]
fn test_single_header_multiple_entries() {
let mut test_headers = HashMap::new();
test_headers.insert("friends".to_string(), vec![
"alice".to_string(),
"bob".to_string()
]);
check_headers(test_headers);
}
#[test]
fn test_single_header_comma_entry() {
let mut test_headers = HashMap::new();
test_headers.insert("friends".to_string(), vec![
"alice".to_string(),
"bob, carol".to_string()
]);
check_headers(test_headers);
}
#[test]
fn test_multiple_headers_single_entry() {
let mut test_headers = HashMap::new();
test_headers.insert("friends".to_string(), vec![
"alice".to_string(),
]);
test_headers.insert("enemies".to_string(), vec![
"victor".to_string(),
]);
check_headers(test_headers);
}
#[test]
fn test_multiple_headers_multiple_entries() {
let mut test_headers = HashMap::new();
test_headers.insert("friends".to_string(), vec![
"alice".to_string(),
"bob".to_string(),
]);
test_headers.insert("enemies".to_string(), vec![
"david".to_string(),
"emily".to_string(),
]);
check_headers(test_headers);
}
}

View File

@ -483,9 +483,9 @@ impl<'r> Request<'r> {
for hyp in h_headers.iter() {
if let Some(header_values) = h_headers.get_raw(hyp.name()) {
for value in header_values {
let value_str = str::from_utf8(value)
.map_err(|_| format!("Bad Header: {:?}", hyp))?;
let header = Header::new(hyp.name().to_string(), value_str.to_string());
// This is not totally correct since values needn't be UTF8.
let value_str = String::from_utf8_lossy(value).into_owned();
let header = Header::new(hyp.name().to_string(), value_str);
request.add_header(header);
}
}

46
lib/src/request/tests.rs Normal file
View File

@ -0,0 +1,46 @@
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::collections::HashMap;
use {hyper, Request};
macro_rules! assert_headers {
($($key:expr => [$($value:expr),+]),+) => ({
// Set up the parameters to the hyper request object.
let h_method = hyper::method::Method::Get;
let h_uri = hyper::uri::RequestUri::AbsolutePath("/test".to_string());
let h_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8000);
let mut h_headers = hyper::header::Headers::new();
// Add all of the passed in headers to the request.
$($(h_headers.append_raw($key.to_string(), $value.as_bytes().into());)+)+
// Build up what we expect the headers to actually be.
let mut expected = HashMap::new();
$(expected.entry($key).or_insert(vec![]).append(&mut vec![$($value),+]);)+
// Dispatch the request and check that the headers are what we expect.
let req = Request::from_hyp(h_method, h_headers, h_uri, h_addr).unwrap();
let actual_headers = req.headers();
for (key, values) in expected.iter() {
let actual: Vec<_> = actual_headers.get(key).collect();
assert_eq!(*values, actual);
}
})
}
#[test]
fn test_multiple_headers_from_hyp() {
assert_headers!("friends" => ["alice"]);
assert_headers!("friends" => ["alice", "bob"]);
assert_headers!("friends" => ["alice", "bob, carol"]);
assert_headers!("friends" => ["alice, david", "bob, carol", "eric, frank"]);
assert_headers!("friends" => ["alice"], "enemies" => ["victor"]);
assert_headers!("friends" => ["alice", "bob"], "enemies" => ["david", "emily"]);
}
#[test]
fn test_multiple_headers_merge_into_one_from_hyp() {
assert_headers!("friend" => ["alice"], "friend" => ["bob"]);
assert_headers!("friend" => ["alice"], "friend" => ["bob"], "friend" => ["carol"]);
assert_headers!("friend" => ["alice"], "friend" => ["bob"], "enemy" => ["carol"]);
}