1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-20 08:31:09 +00: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]
extern crate log;

View file

@ -269,8 +269,9 @@ impl InnerMultipart {
let stop = match self.item {
InnerMultipartItem::Field(ref mut field) => {
match field.borrow_mut().poll(safety)? {
Async::NotReady =>
return Ok(Async::NotReady),
Async::NotReady => {
return Ok(Async::NotReady)
}
Async::Ready(Some(_)) =>
continue,
Async::Ready(None) =>
@ -318,7 +319,9 @@ impl InnerMultipart {
// read boundary
InnerState::Boundary => {
match InnerMultipart::read_boundary(payload, &self.boundary)? {
Async::NotReady => return Ok(Async::NotReady),
Async::NotReady => {
return Ok(Async::NotReady)
}
Async::Ready(eof) => {
if eof {
self.state = InnerState::Eof;
@ -358,6 +361,8 @@ impl InnerMultipart {
}
}
self.state = InnerState::Boundary;
// nested multipart stream
if mt.type_() == mime::MULTIPART {
let inner = if let Some(boundary) = mt.get_param(mime::BOUNDARY) {
@ -691,8 +696,16 @@ impl Drop for Safety {
}
}
#[test]
fn test_boundary() {
#[cfg(test)]
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();
match Multipart::boundary(&headers) {
Err(MultipartError::NoContentType) => (),
@ -725,4 +738,52 @@ fn test_boundary() {
assert_eq!(Multipart::boundary(&headers).unwrap(),
"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)]
mod test {
mod tests {
use super::*;
use futures::future::{lazy, result};
use tokio_core::reactor::Core;