From e8936fe081afdbe4856a80f6bc5ef3c8d84d62cf Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 22 Oct 2017 22:23:38 -0700 Subject: [PATCH] update multipart tests --- src/multipart.rs | 39 ++++++++++++++++++++++++++++++++++++--- tests/test_server.rs | 15 ++++++++++----- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/multipart.rs b/src/multipart.rs index c815f623..b3317b37 100644 --- a/src/multipart.rs +++ b/src/multipart.rs @@ -239,12 +239,17 @@ impl InnerMultipart { //ValueError("Could not find starting boundary %r" //% (self._boundary)) } + if chunk.len() < boundary.len() { + continue + } if &chunk[..2] == b"--" && &chunk[2..chunk.len()-2] == boundary.as_bytes() { break; } else { + if chunk.len() < boundary.len() + 2{ + continue + } let b: &[u8] = boundary.as_ref(); - if chunk.len() <= boundary.len() + 2 && - &chunk[..boundary.len()] == b && + if &chunk[..boundary.len()] == b && &chunk[boundary.len()..boundary.len()+2] == b"--" { eof = true; break; @@ -746,9 +751,13 @@ mod tests { let (mut sender, payload) = Payload::new(false); let bytes = Bytes::from( - "--abbc761f78ff4d7cb7573b5a23f96ef0\r\n\ + "testasdadsad\r\n\ + --abbc761f78ff4d7cb7573b5a23f96ef0\r\n\ Content-Type: text/plain; charset=utf-8\r\nContent-Length: 4\r\n\r\n\ test\r\n\ + --abbc761f78ff4d7cb7573b5a23f96ef0\r\n\ + Content-Type: text/plain; charset=utf-8\r\nContent-Length: 4\r\n\r\n\ + data\r\n\ --abbc761f78ff4d7cb7573b5a23f96ef0--\r\n"); sender.feed_data(bytes); @@ -777,6 +786,30 @@ mod tests { } _ => unreachable!() } + + match multipart.poll() { + Ok(Async::Ready(Some(item))) => { + match item { + MultipartItem::Field(mut field) => { + assert_eq!(field.content_type().type_(), mime::TEXT); + assert_eq!(field.content_type().subtype(), mime::PLAIN); + + match field.poll() { + Ok(Async::Ready(Some(chunk))) => + assert_eq!(chunk.0, "data"), + _ => unreachable!() + } + match field.poll() { + Ok(Async::Ready(None)) => (), + _ => unreachable!() + } + }, + _ => unreachable!() + } + } + _ => unreachable!() + } + match multipart.poll() { Ok(Async::Ready(None)) => (), _ => unreachable!() diff --git a/tests/test_server.rs b/tests/test_server.rs index 1a742081..66042b21 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -4,7 +4,6 @@ extern crate tokio_core; extern crate reqwest; use std::{net, thread}; -use std::str::FromStr; use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; use tokio_core::net::TcpListener; @@ -35,18 +34,24 @@ fn test_serve() { #[test] fn test_serve_incoming() { - thread::spawn(|| { + let loopback = net::Ipv4Addr::new(127, 0, 0, 1); + let socket = net::SocketAddrV4::new(loopback, 0); + let tcp = net::TcpListener::bind(socket).unwrap(); + let addr1 = tcp.local_addr().unwrap(); + let addr2 = tcp.local_addr().unwrap(); + + thread::spawn(move || { let sys = System::new("test"); let srv = create_server(); - let addr = net::SocketAddr::from_str("127.0.0.1:58903").unwrap(); - let tcp = TcpListener::bind(&addr, Arbiter::handle()).unwrap(); + let tcp = TcpListener::from_listener(tcp, &addr2, Arbiter::handle()).unwrap(); srv.serve_incoming::<_, ()>(tcp.incoming()).unwrap(); sys.run(); }); - assert!(reqwest::get("http://localhost:58903/").unwrap().status().is_success()); + assert!(reqwest::get(&format!("http://{}/", addr1)) + .unwrap().status().is_success()); } struct MiddlewareTest {