Add Response::body_string(). Use it in all tests.

This commit is contained in:
Sergio Benitez 2017-04-14 01:59:28 -07:00
parent 0d674c57fd
commit 3bebdcc53d
31 changed files with 88 additions and 104 deletions

View File

@ -49,6 +49,5 @@ pub fn test_hello() {
let mut request = MockRequest::new(Method::Get, "/hello");
let mut response = request.dispatch_with(&rocket);
assert_eq!(response.body().and_then(|b| b.into_string()),
Some("Hello, world!".to_string()));
assert_eq!(response.body_string(), Some("Hello, world!".into()));
}

View File

@ -14,7 +14,7 @@ fn test<H>(method: Method, uri: &str, header: H, status: Status, body: String)
let mut response = request.dispatch_with(&rocket);
assert_eq!(response.status(), status);
assert_eq!(response.body().and_then(|b| b.into_string()), Some(body));
assert_eq!(response.body_string(), Some(body));
}
#[test]

View File

@ -31,7 +31,7 @@ fn test_body(optional_cookie: Option<Cookie<'static>>, expected_body: String) {
let mut response = request.dispatch_with(&rocket);
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.body().and_then(|b| b.into_string()), Some(expected_body));
assert_eq!(response.body_string(), Some(expected_body));
}
#[test]

View File

@ -10,7 +10,7 @@ fn test(uri: &str, status: Status, body: String) {
let mut response = req.dispatch_with(&rocket);
assert_eq!(response.status(), status);
assert_eq!(response.body().and_then(|b| b.into_string()), Some(body));
assert_eq!(response.body_string(), Some(body));
}
#[test]

View File

@ -17,7 +17,7 @@ fn test_login<T>(user: &str, pass: &str, age: &str, status: Status, body: T)
let mut response = req.dispatch_with(&rocket);
assert_eq!(response.status(), status);
let body_str = response.body().and_then(|body| body.into_string());
let body_str = response.body_string();
if let Some(expected_str) = body.into() {
assert!(body_str.map_or(false, |s| s.contains(expected_str)));
}

View File

@ -12,7 +12,7 @@ fn test_login(username: &str, password: &str, age: isize, status: Status,
.body(&format!("username={}&password={}&age={}", username, password, age));
let mut response = req.dispatch_with(&rocket);
let body_str = response.body().and_then(|body| body.into_string());
let body_str = response.body_string();
println!("Checking: {:?}/{:?}/{:?}/{:?}", username, password, age, body_str);
assert_eq!(response.status(), status);

View File

@ -50,7 +50,7 @@ mod test {
let mut response = req.dispatch_with(&rocket);
let expect = format!("Your request contained {} headers!", headers.len());
assert_eq!(response.body().and_then(|b| b.into_string()), Some(expect));
assert_eq!(response.body_string(), Some(expect));
}
#[test]

View File

@ -34,14 +34,12 @@ fn test_root() {
for method in &[Post, Put, Delete, Options, Trace, Connect, Patch] {
let req = MockRequest::new(*method, "/");
run_test!(req, |mut response: Response| {
assert_eq!(response.status(), Status::NotFound);
let mut map = ::std::collections::HashMap::new();
map.insert("path", "/");
let expected = Template::render("error/404", &map).to_string();
let expected_body = Template::render("error/404", &map).to_string();
let body_string = response.body().and_then(|body| body.into_string());
assert_eq!(body_string, Some(expected));
assert_eq!(response.status(), Status::NotFound);
assert_eq!(response.body_string(), Some(expected_body));
});
}
}
@ -59,8 +57,7 @@ fn test_name() {
};
let expected = Template::render("index", &context).to_string();
let body_string = response.body().and_then(|body| body.into_string());
assert_eq!(body_string, Some(expected));
assert_eq!(response.body_string(), Some(expected));
});
}
@ -74,8 +71,6 @@ fn test_404() {
let mut map = ::std::collections::HashMap::new();
map.insert("path", "/hello/");
let expected = Template::render("error/404", &map).to_string();
let body_string = response.body().and_then(|body| body.into_string());
assert_eq!(body_string, Some(expected));
assert_eq!(response.body_string(), Some(expected));
});
}

View File

@ -12,7 +12,7 @@ fn test(method: Method, status: Status, body_prefix: Option<&str>) {
assert_eq!(response.status(), status);
if let Some(expected_body_string) = body_prefix {
let body_str = response.body().and_then(|body| body.into_string()).unwrap();
let body_str = response.body_string().unwrap();
assert!(body_str.starts_with(expected_body_string));
}
}

View File

@ -7,8 +7,7 @@ fn test(uri: &str, expected: String) {
let rocket = rocket::ignite().mount("/", routes![super::hello, super::hi]);
let mut req = MockRequest::new(Get, uri);
let mut response = req.dispatch_with(&rocket);
assert_eq!(response.body().and_then(|b| b.into_string()), Some(expected));
assert_eq!(response.body_string(), Some(expected));
}
fn test_404(uri: &str) {

View File

@ -5,10 +5,8 @@ use rocket::http::Method::*;
fn test(uri: &str, expected: String) {
let rocket = rocket::ignite().mount("/", routes![super::hello, super::hi]);
let mut req = MockRequest::new(Get, uri);
let mut response = req.dispatch_with(&rocket);
let body_str = response.body().and_then(|body| body.into_string());
assert_eq!(body_str, Some(expected));
assert_eq!(response.body_string(), Some(expected));
}
#[test]

View File

@ -8,6 +8,5 @@ fn hello_world() {
let mut req = MockRequest::new(Get, "/");
let mut response = req.dispatch_with(&rocket);
let body_str = response.body().and_then(|body| body.into_string());
assert_eq!(body_str, Some("Hello, world!".to_string()));
assert_eq!(response.body_string(), Some("Hello, world!".into()));
}

View File

@ -8,6 +8,5 @@ fn hello_world() {
let mut req = MockRequest::new(Get, "/");
let mut response = req.dispatch_with(&rocket);
let body_str = response.body().and_then(|body| body.into_string());
assert_eq!(body_str, Some("Hello, world!".to_string()));
assert_eq!(response.body_string(), Some("Hello, world!".into()));
}

View File

@ -9,7 +9,7 @@ fn test(uri: &str, content_type: ContentType, status: Status, body: String) {
let mut response = request.dispatch_with(&rocket);
assert_eq!(response.status(), status);
assert_eq!(response.body().and_then(|b| b.into_string()), Some(body));
assert_eq!(response.body_string(), Some(body));
}
#[test]
@ -50,7 +50,7 @@ fn test_upload() {
let mut request = MockRequest::new(Get, "/upload");
let mut response = request.dispatch_with(&rocket);
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.body().and_then(|b| b.into_string()), Some(expected_body));
assert_eq!(response.body_string(), Some(expected_body));
}
#[test]

View File

@ -9,8 +9,7 @@ fn test_200(uri: &str, expected_body: &str) {
let mut response = request.dispatch_with(&rocket);
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.body().and_then(|b| b.into_string()),
Some(expected_body.to_string()));
assert_eq!(response.body_string(), Some(expected_body.to_string()));
}
fn test_303(uri: &str, expected_location: &str) {

View File

@ -9,8 +9,7 @@ fn test_200() {
let mut response = request.dispatch_with(&rocket);
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.body().and_then(|b| b.into_string()),
Some("Hello, Sergio!".to_string()));
assert_eq!(response.body_string(), Some("Hello, Sergio!".into()));
}
#[test]

View File

@ -16,45 +16,44 @@ macro_rules! run_test {
#[test]
fn age_and_name_params() {
run_test!("?age=10&name=john", |mut response: Response| {
let body_str = response.body().and_then(|body| body.into_string());
assert_eq!(body_str, Some("Hello, 10 year old named john!".to_string()));
});
run_test!("?age=10&name=john", |mut response: Response| {
assert_eq!(response.body_string(),
Some("Hello, 10 year old named john!".into()));
});
}
#[test]
fn age_param_only() {
run_test!("?age=10", |response: Response| {
assert_eq!(response.status(), Status::NotFound);
});
run_test!("?age=10", |response: Response| {
assert_eq!(response.status(), Status::NotFound);
});
}
#[test]
fn name_param_only() {
run_test!("?name=John", |mut response: Response| {
let body_str = response.body().and_then(|body| body.into_string());
assert_eq!(body_str, Some("Hello John!".to_string()));
});
run_test!("?name=John", |mut response: Response| {
assert_eq!(response.body_string(), Some("Hello John!".into()));
});
}
#[test]
fn no_params() {
run_test!("", |response: Response| {
assert_eq!(response.status(), Status::NotFound);
});
run_test!("", |response: Response| {
assert_eq!(response.status(), Status::NotFound);
});
run_test!("?", |response: Response| {
assert_eq!(response.status(), Status::NotFound);
});
run_test!("?", |response: Response| {
assert_eq!(response.status(), Status::NotFound);
});
}
#[test]
fn non_existent_params() {
run_test!("?x=y", |response: Response| {
assert_eq!(response.status(), Status::NotFound);
});
run_test!("?x=y", |response: Response| {
assert_eq!(response.status(), Status::NotFound);
});
run_test!("?age=10&name=john&complete=true", |response: Response| {
assert_eq!(response.status(), Status::NotFound);
});
run_test!("?age=10&name=john&complete=true", |response: Response| {
assert_eq!(response.status(), Status::NotFound);
});
}

View File

@ -8,6 +8,5 @@ fn hello() {
let mut req = MockRequest::new(Get, "/");
let mut response = req.dispatch_with(&rocket);
let body_str = response.body().and_then(|body| body.into_string());
assert_eq!(body_str, Some("Rocketeer".to_string()));
assert_eq!(response.body_string(), Some("Rocketeer".into()));
}

View File

@ -31,8 +31,8 @@ fn test_root() {
#[test]
fn test_login() {
run_test!("/login", |mut response: Response| {
let body_string = response.body().and_then(|body| body.into_string());
assert_eq!(body_string, Some("Hi! Please log in before continuing.".to_string()));
assert_eq!(response.body_string(),
Some("Hi! Please log in before continuing.".to_string()));
assert_eq!(response.status(), Status::Ok);
});
}

View File

@ -12,8 +12,7 @@ fn register_hit(rocket: &Rocket) {
fn get_count(rocket: &Rocket) -> usize {
let mut req = MockRequest::new(Get, "/count");
let mut response = req.dispatch_with(&rocket);
let body_string = response.body().and_then(|b| b.into_string()).unwrap();
body_string.parse().unwrap()
response.body_string().and_then(|s| s.parse().ok()).unwrap()
}
#[test]

View File

@ -25,8 +25,6 @@ mod test {
let mut req = MockRequest::new(Get, "/");
let mut response = req.dispatch_with(&rocket);
assert_eq!(response.status(), Status::Ok);
let body_string = response.body().and_then(|b| b.into_string());
assert_eq!(body_string, Some("Hello, world!".to_string()));
assert_eq!(response.body_string(), Some("Hello, world!".into()));
}
}

View File

@ -9,7 +9,7 @@ fn test(uri: &str, expected: &str) {
let mut req = MockRequest::new(Get, uri);
let mut res = req.dispatch_with(&rocket);
assert_eq!(res.body().and_then(|b| b.into_string()), Some(expected.into()));
assert_eq!(res.body_string(), Some(expected.into()));
}
fn test_404(uri: &str) {

View File

@ -173,9 +173,7 @@ pub trait Responder<'r> {
/// use rocket::http::ContentType;
///
/// let mut response = "Hello".respond().unwrap();
///
/// let body_string = response.body().and_then(|b| b.into_string());
/// assert_eq!(body_string, Some("Hello".to_string()));
/// assert_eq!(response.body_string(), Some("Hello".into()));
///
/// let content_type: Vec<_> = response.headers().get("Content-Type").collect();
/// assert_eq!(content_type.len(), 1);

View File

@ -829,10 +829,7 @@ impl<'r> Response<'r> {
/// assert!(response.body().is_none());
///
/// response.set_sized_body(Cursor::new("Hello, world!"));
///
/// let body_string = response.body().and_then(|b| b.into_string());
/// assert_eq!(body_string, Some("Hello, world!".to_string()));
/// assert!(response.body().is_some());
/// assert_eq!(response.body_string(), Some("Hello, world!".to_string()));
/// ```
#[inline(always)]
pub fn body(&mut self) -> Option<Body<&mut io::Read>> {
@ -846,6 +843,29 @@ impl<'r> Response<'r> {
}
}
/// Consumes `self's` body and reads it into a string. If `self` doesn't
/// have a body, reading fails, or string conversion (for non-UTF-8 bodies)
/// fails, returns `None`. Note that `self`'s `body` is consumed after a
/// call to this method.
///
/// # Example
///
/// ```rust
/// use std::io::Cursor;
/// use rocket::Response;
///
/// let mut response = Response::new();
/// assert!(response.body().is_none());
///
/// response.set_sized_body(Cursor::new("Hello, world!"));
/// assert_eq!(response.body_string(), Some("Hello, world!".to_string()));
/// assert!(response.body().is_none());
/// ```
#[inline(always)]
pub fn body_string(&mut self) -> Option<String> {
self.take_body().and_then(|b| b.into_string())
}
/// Moves the body of `self` out and returns it, if there is one, leaving no
/// body in its place.
///
@ -901,9 +921,7 @@ impl<'r> Response<'r> {
///
/// let mut response = Response::new();
/// response.set_sized_body(Cursor::new("Hello, world!"));
///
/// let body_string = response.body().and_then(|b| b.into_string());
/// assert_eq!(body_string, Some("Hello, world!".to_string()));
/// assert_eq!(response.body_string(), Some("Hello, world!".to_string()));
/// ```
#[inline]
pub fn set_sized_body<B>(&mut self, mut body: B)
@ -929,9 +947,7 @@ impl<'r> Response<'r> {
///
/// let mut response = Response::new();
/// response.set_streamed_body(repeat(97).take(5));
///
/// let body_string = response.body().and_then(|b| b.into_string());
/// assert_eq!(body_string, Some("aaaaa".to_string()));
/// assert_eq!(response.body_string(), Some("aaaaa".to_string()));
/// ```
#[inline(always)]
pub fn set_streamed_body<B>(&mut self, body: B) where B: io::Read + 'r {
@ -949,9 +965,7 @@ impl<'r> Response<'r> {
///
/// let mut response = Response::new();
/// response.set_chunked_body(repeat(97).take(5), 10);
///
/// let body_string = response.body().and_then(|b| b.into_string());
/// assert_eq!(body_string, Some("aaaaa".to_string()));
/// assert_eq!(response.body_string(), Some("aaaaa".to_string()));
/// ```
#[inline(always)]
pub fn set_chunked_body<B>(&mut self, body: B, chunk_size: u64)
@ -974,8 +988,7 @@ impl<'r> Response<'r> {
/// let mut response = Response::new();
/// response.set_raw_body(body);
///
/// let body_string = response.body().and_then(|b| b.into_string());
/// assert_eq!(body_string, Some("Hello!".to_string()));
/// assert_eq!(response.body_string(), Some("Hello!".to_string()));
/// ```
#[inline(always)]
pub fn set_raw_body<T: io::Read + 'r>(&mut self, body: Body<T>) {

View File

@ -96,11 +96,8 @@
//! let mut req = MockRequest::new(Get, "/");
//! let mut response = req.dispatch_with(&rocket);
//!
//! // Write the body out as a string.
//! let body_str = response.body().and_then(|b| b.into_string());
//!
//! // Check that the body contains what we expect.
//! assert_eq!(body_str, Some("Hello, world!".to_string()));
//! // Check that the body contains the string we expect.
//! assert_eq!(response.body_string(), Some("Hello, world!".into()));
//! }
//! }
//! ```
@ -260,8 +257,7 @@ impl<'r> MockRequest<'r> {
/// let mut req = MockRequest::new(Get, "/");
/// let mut response = req.dispatch_with(&rocket);
///
/// let body_str = response.body().and_then(|b| b.into_string());
/// assert_eq!(body_str, Some("Hello, world!".to_string()));
/// assert_eq!(response.body_string(), Some("Hello, world!".into()));
/// # }
/// ```
pub fn dispatch_with<'s>(&'s mut self, rocket: &'r Rocket) -> Response<'s> {

View File

@ -32,8 +32,7 @@ mod tests {
.body("_method=patch&form_data=Form+data");
let mut response = req.dispatch_with(&rocket);
let body_str = response.body().and_then(|b| b.into_string());
assert_eq!(body_str, Some("OK".to_string()));
assert_eq!(response.body_string(), Some("OK".into()));
}
#[test]

View File

@ -30,9 +30,8 @@ mod tests {
.body(format!("form_data={}", raw));
let mut response = req.dispatch_with(&rocket);
let body_string = response.body().and_then(|b| b.into_string());
assert_eq!(response.status(), Status::Ok);
assert_eq!(Some(decoded.to_string()), body_string);
assert_eq!(Some(decoded.to_string()), response.body_string());
}
#[test]

View File

@ -48,7 +48,7 @@ mod tests {
}
let mut response = req.dispatch_with(&rocket);
let body_str = response.body().and_then(|b| b.into_string());
let body_str = response.body_string();
let body: Option<&'static str> = $body;
match body {
Some(string) => assert_eq!(body_str, Some(string.to_string())),

View File

@ -29,13 +29,11 @@ mod tests {
fn assert_no_collision(rocket: &Rocket) {
let mut req = MockRequest::new(Get, "/?field=query");
let mut response = req.dispatch_with(&rocket);
let body_str = response.body().and_then(|b| b.into_string());
assert_eq!(body_str, Some("query".to_string()));
assert_eq!(response.body_string(), Some("query".into()));
let mut req = MockRequest::new(Get, "/");
let mut response = req.dispatch_with(&rocket);
let body_str = response.body().and_then(|b| b.into_string());
assert_eq!(body_str, Some("no query".to_string()));
assert_eq!(response.body_string(), Some("no query".into()));
}
#[test]

View File

@ -33,7 +33,7 @@ mod remote_rewrite_tests {
let mut response = req.dispatch_with(&rocket);
assert_eq!(response.status(), Status::Ok);
let body_str = response.body().and_then(|b| b.into_string());
let body_str = response.body_string();
match ip {
Some(ip) => assert_eq!(body_str, Some(format!("{}:{}", ip, port))),
None => assert_eq!(body_str, Some(KNOWN_IP.into()))

View File

@ -52,8 +52,7 @@ mod tests {
let mut req = MockRequest::new(Get, format!("{}/{}", prefix, path));
let mut response = req.dispatch_with(&rocket);
let body_str = response.body().and_then(|b| b.into_string());
assert_eq!(body_str, Some(path.into()));
assert_eq!(response.body_string(), Some(path.into()));
}
}
}