1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-30 18:34:36 +01:00

multipart tests

This commit is contained in:
Nikolay Kim 2017-10-22 21:40:41 -07:00
parent 5f90d0bcd6
commit 3adddc591d
3 changed files with 96 additions and 35 deletions

View File

@ -1,4 +1,4 @@
//! Web framework for [Actix](https://github.com/fafhrd91/actix) //! Web framework for [Actix](https://github.com/actix/actix)
#[macro_use] #[macro_use]
extern crate log; extern crate log;

View File

@ -269,8 +269,9 @@ impl InnerMultipart {
let stop = match self.item { let stop = match self.item {
InnerMultipartItem::Field(ref mut field) => { InnerMultipartItem::Field(ref mut field) => {
match field.borrow_mut().poll(safety)? { match field.borrow_mut().poll(safety)? {
Async::NotReady => Async::NotReady => {
return Ok(Async::NotReady), return Ok(Async::NotReady)
}
Async::Ready(Some(_)) => Async::Ready(Some(_)) =>
continue, continue,
Async::Ready(None) => Async::Ready(None) =>
@ -318,7 +319,9 @@ impl InnerMultipart {
// read boundary // read boundary
InnerState::Boundary => { InnerState::Boundary => {
match InnerMultipart::read_boundary(payload, &self.boundary)? { match InnerMultipart::read_boundary(payload, &self.boundary)? {
Async::NotReady => return Ok(Async::NotReady), Async::NotReady => {
return Ok(Async::NotReady)
}
Async::Ready(eof) => { Async::Ready(eof) => {
if eof { if eof {
self.state = InnerState::Eof; self.state = InnerState::Eof;
@ -358,6 +361,8 @@ impl InnerMultipart {
} }
} }
self.state = InnerState::Boundary;
// nested multipart stream // nested multipart stream
if mt.type_() == mime::MULTIPART { if mt.type_() == mime::MULTIPART {
let inner = if let Some(boundary) = mt.get_param(mime::BOUNDARY) { let inner = if let Some(boundary) = mt.get_param(mime::BOUNDARY) {
@ -691,8 +696,16 @@ impl Drop for Safety {
} }
} }
#[test] #[cfg(test)]
fn test_boundary() { mod tests {
use super::*;
use bytes::Bytes;
use futures::future::{lazy, result};
use tokio_core::reactor::Core;
use payload::Payload;
#[test]
fn test_boundary() {
let headers = HeaderMap::new(); let headers = HeaderMap::new();
match Multipart::boundary(&headers) { match Multipart::boundary(&headers) {
Err(MultipartError::NoContentType) => (), Err(MultipartError::NoContentType) => (),
@ -725,4 +738,52 @@ fn test_boundary() {
assert_eq!(Multipart::boundary(&headers).unwrap(), assert_eq!(Multipart::boundary(&headers).unwrap(),
"5c02368e880e436dab70ed54e1c58209"); "5c02368e880e436dab70ed54e1c58209");
}
#[test]
fn test_multipart() {
Core::new().unwrap().run(lazy(|| {
let (mut sender, payload) = Payload::new(false);
let bytes = Bytes::from(
"--abbc761f78ff4d7cb7573b5a23f96ef0\r\n\
Content-Type: text/plain; charset=utf-8\r\nContent-Length: 4\r\n\r\n\
test\r\n\
--abbc761f78ff4d7cb7573b5a23f96ef0--\r\n");
sender.feed_data(bytes);
let mut multipart = Multipart::new(
"abbc761f78ff4d7cb7573b5a23f96ef0".to_owned(), payload);
match multipart.poll() {
Ok(Async::Ready(Some(item))) => {
println!("{:?}", 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, "test"),
_ => unreachable!()
}
match field.poll() {
Ok(Async::Ready(None)) => (),
_ => unreachable!()
}
},
_ => unreachable!()
}
}
_ => unreachable!()
}
match multipart.poll() {
Ok(Async::Ready(None)) => (),
_ => unreachable!()
}
let res: Result<(), ()> = Ok(());
result(res)
})).unwrap();
}
} }

View File

@ -363,7 +363,7 @@ impl Inner {
} }
#[cfg(test)] #[cfg(test)]
mod test { mod tests {
use super::*; use super::*;
use futures::future::{lazy, result}; use futures::future::{lazy, result};
use tokio_core::reactor::Core; use tokio_core::reactor::Core;