Skip to content

Commit

Permalink
feat(common): multibytereader single byte test
Browse files Browse the repository at this point in the history
It shows that we actually don't handle our state correctly.
The first test which reads to string obviously uses a big-enough buffer.
  • Loading branch information
Byron committed Mar 18, 2015
1 parent 8db346b commit b127df1
Showing 1 changed file with 42 additions and 17 deletions.
59 changes: 42 additions & 17 deletions src/rust/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![feature(core,io)]
#![allow(dead_code)]
#![feature(core,io,old_path)]
#![allow(dead_code, deprecated, unused_features)]
//! library with code shared by all generated implementations
extern crate hyper;
extern crate mime;
Expand All @@ -17,7 +17,22 @@ mod tests {
use self::hyper_mock::*;
use std::io::Read;
use std::default::Default;
use std::old_path::BytesContainer;

const EXPECTED: &'static str =
"\r\n--MDuXWGyeE33QFXGchb2VFWc4Z7945d\r\n\
Content-Length: 50\r\n\
Content-Type: application/json\r\n\
\r\n\
foo\r\n\
--MDuXWGyeE33QFXGchb2VFWc4Z7945d\r\n\
Content-Length: 25\r\n\
Content-Type: application/plain\r\n\
\r\n\
bar\r\n\
--MDuXWGyeE33QFXGchb2VFWc4Z7945d";

const EXPECTED_LEN: usize= 221;

#[test]
fn multi_part_reader() {
Expand All @@ -31,25 +46,35 @@ mod tests {
let mut res = String::new();
let r = mpr.read_to_string(&mut res);
assert_eq!(res.len(), r.clone().unwrap());
println!("{}", res);

let expected =
"\r\n--MDuXWGyeE33QFXGchb2VFWc4Z7945d\r\n\
Content-Length: 50\r\n\
Content-Type: application/json\r\n\
\r\n\
foo\r\n\
--MDuXWGyeE33QFXGchb2VFWc4Z7945d\r\n\
Content-Length: 25\r\n\
Content-Type: application/plain\r\n\
\r\n\
bar\r\n\
--MDuXWGyeE33QFXGchb2VFWc4Z7945d";
// NOTE: This CAN fail, as the underlying header hashmap is not sorted
// As the test is just for dev, and doesn't run on travis, we are fine,
// for now. Possible solution would be to omit the size field (make it
// optional)
assert_eq!(res, expected);
assert_eq!(r, Ok(221));
// assert_eq!(res, EXPECTED);
assert_eq!(r, Ok(EXPECTED_LEN));
}

#[test]
fn multi_part_reader_single_byte_read() {
let mut r1 = MockStream::with_input(b"foo");
let mut r2 = MockStream::with_input(b"bar");
let mut mpr: MultiPartReader = Default::default();

mpr.add_part(&mut r1, 50, &"application/json".parse().unwrap())
.add_part(&mut r2, 25, &"application/plain".parse().unwrap());

let mut buf = &mut [0u8];
let mut v = Vec::<u8>::new();
while let Ok(br) = mpr.read(buf) {
if br == 0 {
break;
}
v.push(buf[0]);
}
println!("{:?}", v.len());
println!("{:?}", v.container_as_str().unwrap());
assert_eq!(v.len(), EXPECTED_LEN);
assert_eq!(v.container_as_str().unwrap(), EXPECTED);
}
}

0 comments on commit b127df1

Please sign in to comment.