test: Fixed up issue with reading a Body and finished RequestBuilder tests

This commit is contained in:
Michael Bryan
2016-11-21 11:45:05 +08:00
parent 980488f918
commit 59ba7cf23b
2 changed files with 54 additions and 34 deletions

View File

@@ -1,4 +1,5 @@
use std::io::Read; use std::io::{Cursor, Read};
use std::io;
use std::fs::File; use std::fs::File;
use std::fmt; use std::fmt;
@@ -27,11 +28,39 @@ impl Body {
*/ */
} }
impl Read for Body {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.reader.read(buf)
}
}
enum Kind { enum Kind {
Reader(Box<Read>, Option<u64>), Reader(Box<Read>, Option<u64>),
Bytes(Vec<u8>), Bytes(Vec<u8>),
} }
impl Read for Kind {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match *self {
Kind::Reader(ref mut reader, _) => {
reader.read(buf)
}
Kind::Bytes(ref mut bytes) => {
// To make sure the bytes are removed properly when you read
// them, you need to use `drain()`
// FIXME: this will probably have poor performance for larger
// bodies due to allocating more than necessary.
let drained_bytes: Vec<u8> = bytes.drain(..).collect();
// then you need a Cursor because a standard Vec doesn't implement
// Read
let mut cursor = Cursor::new(drained_bytes);
cursor.read(buf)
}
}
}
}
impl From<Vec<u8>> for Body { impl From<Vec<u8>> for Body {
#[inline] #[inline]
fn from(v: Vec<u8>) -> Body { fn from(v: Vec<u8>) -> Body {
@@ -101,4 +130,3 @@ pub fn as_hyper_body<'a>(body: &'a mut Body) -> ::hyper::client::Body<'a> {
} }
} }
} }

View File

@@ -290,6 +290,9 @@ mod tests {
use hyper::Url; use hyper::Url;
use hyper::header::{Host, Headers, ContentType}; use hyper::header::{Host, Headers, ContentType};
use std::collections::HashMap; use std::collections::HashMap;
use std::io::Read;
use serde_urlencoded;
use serde_json;
#[test] #[test]
fn basic_get_request() { fn basic_get_request() {
@@ -361,30 +364,19 @@ mod tests {
} }
#[test] #[test]
#[ignore]
fn add_body() { fn add_body() {
// Currently Body doesn't have an implementation of PartialEq, so you let client = Client::new().unwrap();
// can't check whether the body is actually what you set. let some_url = "https://google.com/";
// let mut r = client.post(some_url);
// This is most probably because Body's reader attribute is anything
// which implements Read, meaning the act of checking the body will
// probably consume it. To get around this, you might want to consider
// restricting Body.reader to something a little more concrete like a
// String.
//
// A basic test is included below, but commented out so the compiler
// won't yell at you.
let body = "Some interesting content";
// let client = Client::new().unwrap(); r = r.body(body);
// let some_url = "https://google.com/";
// let mut r = client.post(some_url); let mut buf = String::new();
// r.body.unwrap().unwrap().read_to_string(&mut buf).unwrap();
// let body = "Some interesting content";
// assert_eq!(buf, body);
// r = r.body(body);
//
//assert_eq!(r.body.unwrap().unwrap(), body);
} }
#[test] #[test]
@@ -401,11 +393,11 @@ mod tests {
// Make sure the content type was set // Make sure the content type was set
assert_eq!(r.headers.get::<ContentType>(), Some(&ContentType::form_url_encoded())); assert_eq!(r.headers.get::<ContentType>(), Some(&ContentType::form_url_encoded()));
// need to check the body is set to the serialized hashmap. Can't let mut buf = String::new();
// currently do that because Body doesn't implement PartialEq. r.body.unwrap().unwrap().read_to_string(&mut buf).unwrap();
// let body_should_be: Body = serde_urlencoded::to_string(form).map(|b| b.into()); let body_should_be = serde_urlencoded::to_string(&form_data).unwrap();
// assert_eq!(r.body, Some(body_should_be)); assert_eq!(buf, body_should_be);
} }
#[test] #[test]
@@ -414,18 +406,18 @@ mod tests {
let some_url = "https://google.com/"; let some_url = "https://google.com/";
let mut r = client.post(some_url); let mut r = client.post(some_url);
let mut form_data = HashMap::new(); let mut json_data = HashMap::new();
form_data.insert("foo", "bar"); json_data.insert("foo", "bar");
r = r.json(&form_data); r = r.json(&json_data);
// Make sure the content type was set // Make sure the content type was set
assert_eq!(r.headers.get::<ContentType>(), Some(&ContentType::json())); assert_eq!(r.headers.get::<ContentType>(), Some(&ContentType::json()));
// need to check the body is set to the serialized hashmap. Can't let mut buf = String::new();
// currently do that because Body doesn't implement PartialEq. r.body.unwrap().unwrap().read_to_string(&mut buf).unwrap();
// let body_should_be: Body = serde_urlencoded::to_string(form).map(|b| b.into()); let body_should_be = serde_json::to_string(&json_data).unwrap();
// assert_eq!(r.body, Some(body_should_be)); assert_eq!(buf, body_should_be);
} }
} }