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::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 {
Reader(Box<Read>, Option<u64>),
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 {
#[inline]
fn from(v: Vec<u8>) -> Body {
@@ -101,4 +130,3 @@ pub fn as_hyper_body<'a>(body: &'a mut Body) -> ::hyper::client::Body<'a> {
}
}
}