1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-25 06:39:22 +02:00

fix implicit chunked encoding

This commit is contained in:
Nikolay Kim
2018-01-11 15:26:46 -08:00
parent 728d4f1f57
commit 0648ad6f33
2 changed files with 133 additions and 56 deletions

View File

@ -16,7 +16,8 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use flate2::Compression;
use flate2::write::{GzEncoder, DeflateEncoder, DeflateDecoder};
use brotli2::write::{BrotliEncoder, BrotliDecoder};
use futures::Future;
use futures::{Future, Stream};
use futures::stream::once;
use h2::client;
use bytes::{Bytes, BytesMut, BufMut};
use http::Request;
@ -113,6 +114,41 @@ fn test_body_gzip() {
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
#[test]
fn test_body_streaming_implicit() {
let srv = test::TestServer::new(
|app| app.handler(|_| {
let body = once(Ok(Bytes::from_static(STR.as_ref())));
httpcodes::HTTPOk.build()
.content_encoding(headers::ContentEncoding::Gzip)
.body(Body::Streaming(Box::new(body)))}));
let mut res = reqwest::get(&srv.url("/")).unwrap();
assert!(res.status().is_success());
let mut bytes = BytesMut::with_capacity(2048).writer();
let _ = res.copy_to(&mut bytes);
let bytes = bytes.into_inner();
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
#[test]
fn test_body_streaming_explicit() {
let srv = test::TestServer::new(
|app| app.handler(|_| {
let body = once(Ok(Bytes::from_static(STR.as_ref())));
httpcodes::HTTPOk.build()
.chunked()
.content_encoding(headers::ContentEncoding::Gzip)
.body(Body::Streaming(Box::new(body)))}));
let mut res = reqwest::get(&srv.url("/")).unwrap();
assert!(res.status().is_success());
let mut bytes = BytesMut::with_capacity(2048).writer();
let _ = res.copy_to(&mut bytes);
let bytes = bytes.into_inner();
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
#[test]
fn test_body_deflate() {
let srv = test::TestServer::new(
@ -153,35 +189,6 @@ fn test_body_brotli() {
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
}
#[test]
fn test_h2() {
let srv = test::TestServer::new(|app| app.handler(httpcodes::HTTPOk));
let addr = srv.addr();
let mut core = Core::new().unwrap();
let handle = core.handle();
let tcp = TcpStream::connect(&addr, &handle);
let tcp = tcp.then(|res| {
client::handshake(res.unwrap())
}).then(move |res| {
let (mut client, h2) = res.unwrap();
let request = Request::builder()
.uri(format!("https://{}/", addr).as_str())
.body(())
.unwrap();
let (response, _) = client.send_request(request, false).unwrap();
// Spawn a task to run the conn...
handle.spawn(h2.map_err(|e| println!("GOT ERR={:?}", e)));
response
});
let resp = core.run(tcp).unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}
#[test]
fn test_gzip_encoding() {
let srv = test::TestServer::new(|app| app.handler(|req: HttpRequest| {
@ -260,6 +267,47 @@ fn test_brotli_encoding() {
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
#[test]
fn test_h2() {
let srv = test::TestServer::new(|app| app.handler(|_|{
httpcodes::HTTPOk.build().body(STR)
}));
let addr = srv.addr();
let mut core = Core::new().unwrap();
let handle = core.handle();
let tcp = TcpStream::connect(&addr, &handle);
let tcp = tcp.then(|res| {
client::handshake(res.unwrap())
}).then(move |res| {
let (mut client, h2) = res.unwrap();
let request = Request::builder()
.uri(format!("https://{}/", addr).as_str())
.body(())
.unwrap();
let (response, _) = client.send_request(request, false).unwrap();
// Spawn a task to run the conn...
handle.spawn(h2.map_err(|e| println!("GOT ERR={:?}", e)));
response.and_then(|response| {
assert_eq!(response.status(), StatusCode::OK);
let (_, body) = response.into_parts();
body.fold(BytesMut::new(), |mut b, c| -> Result<_, h2::Error> {
b.extend(c);
Ok(b)
})
})
});
let res = core.run(tcp).unwrap();
assert_eq!(res, Bytes::from_static(STR.as_ref()));
}
#[test]
fn test_application() {
let srv = test::TestServer::with_factory(