mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-27 17:22:57 +01:00
multipart tests
This commit is contained in:
parent
5f90d0bcd6
commit
3adddc591d
@ -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;
|
||||||
|
127
src/multipart.rs
127
src/multipart.rs
@ -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,38 +696,94 @@ impl Drop for Safety {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[cfg(test)]
|
||||||
fn test_boundary() {
|
mod tests {
|
||||||
let headers = HeaderMap::new();
|
use super::*;
|
||||||
match Multipart::boundary(&headers) {
|
use bytes::Bytes;
|
||||||
Err(MultipartError::NoContentType) => (),
|
use futures::future::{lazy, result};
|
||||||
_ => panic!("should not happen"),
|
use tokio_core::reactor::Core;
|
||||||
|
use payload::Payload;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_boundary() {
|
||||||
|
let headers = HeaderMap::new();
|
||||||
|
match Multipart::boundary(&headers) {
|
||||||
|
Err(MultipartError::NoContentType) => (),
|
||||||
|
_ => panic!("should not happen"),
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut headers = HeaderMap::new();
|
||||||
|
headers.insert(header::CONTENT_TYPE,
|
||||||
|
header::HeaderValue::from_static("test"));
|
||||||
|
|
||||||
|
match Multipart::boundary(&headers) {
|
||||||
|
Err(MultipartError::ParseContentType) => (),
|
||||||
|
_ => panic!("should not happen"),
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut headers = HeaderMap::new();
|
||||||
|
headers.insert(
|
||||||
|
header::CONTENT_TYPE,
|
||||||
|
header::HeaderValue::from_static("multipart/mixed"));
|
||||||
|
match Multipart::boundary(&headers) {
|
||||||
|
Err(MultipartError::Boundary) => (),
|
||||||
|
_ => panic!("should not happen"),
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut headers = HeaderMap::new();
|
||||||
|
headers.insert(
|
||||||
|
header::CONTENT_TYPE,
|
||||||
|
header::HeaderValue::from_static(
|
||||||
|
"multipart/mixed; boundary=\"5c02368e880e436dab70ed54e1c58209\""));
|
||||||
|
|
||||||
|
assert_eq!(Multipart::boundary(&headers).unwrap(),
|
||||||
|
"5c02368e880e436dab70ed54e1c58209");
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut headers = HeaderMap::new();
|
#[test]
|
||||||
headers.insert(header::CONTENT_TYPE,
|
fn test_multipart() {
|
||||||
header::HeaderValue::from_static("test"));
|
Core::new().unwrap().run(lazy(|| {
|
||||||
|
let (mut sender, payload) = Payload::new(false);
|
||||||
|
|
||||||
match Multipart::boundary(&headers) {
|
let bytes = Bytes::from(
|
||||||
Err(MultipartError::ParseContentType) => (),
|
"--abbc761f78ff4d7cb7573b5a23f96ef0\r\n\
|
||||||
_ => panic!("should not happen"),
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut headers = HeaderMap::new();
|
|
||||||
headers.insert(
|
|
||||||
header::CONTENT_TYPE,
|
|
||||||
header::HeaderValue::from_static("multipart/mixed"));
|
|
||||||
match Multipart::boundary(&headers) {
|
|
||||||
Err(MultipartError::Boundary) => (),
|
|
||||||
_ => panic!("should not happen"),
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut headers = HeaderMap::new();
|
|
||||||
headers.insert(
|
|
||||||
header::CONTENT_TYPE,
|
|
||||||
header::HeaderValue::from_static(
|
|
||||||
"multipart/mixed; boundary=\"5c02368e880e436dab70ed54e1c58209\""));
|
|
||||||
|
|
||||||
assert_eq!(Multipart::boundary(&headers).unwrap(),
|
|
||||||
"5c02368e880e436dab70ed54e1c58209");
|
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
Loading…
Reference in New Issue
Block a user