Skip to content

Commit be0bafa

Browse files
committed
create a long chunked test
1 parent 0d1ed82 commit be0bafa

File tree

5 files changed

+181
-2
lines changed

5 files changed

+181
-2
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ log = "0.4"
2626
pretty_assertions = "0.6.1"
2727
async-std = { version = "1.4.0", features = ["unstable", "attributes"] }
2828
tempfile = "3.1.0"
29+
duplexify = "1.1.0"
30+
io-arc = "1.0.0"

src/server/encode.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,14 @@ impl Read for Encoder {
217217
// we keep track how many bytes of the head and body we've read
218218
// in this call of `poll_read`
219219
self.bytes_read = 0;
220-
match self.state {
220+
let res = match self.state {
221221
EncoderState::Start => self.encode_start(cx, buf),
222222
EncoderState::Head => self.encode_head(cx, buf),
223223
EncoderState::FixedBody => self.encode_fixed_body(cx, buf),
224224
EncoderState::ChunkedBody => self.encode_chunked_body(cx, buf),
225225
EncoderState::Done => Poll::Ready(Ok(0)),
226-
}
226+
};
227+
// dbg!(String::from_utf8(buf[..self.bytes_read].to_vec()).unwrap());
228+
res
227229
}
228230
}

tests/common/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ use async_std::task::{Context, Poll};
66
use std::pin::Pin;
77
use std::sync::Mutex;
88

9+
mod test3;
10+
11+
pub use test3::TestCase as TestCase2;
12+
913
#[derive(Debug, Copy, Clone)]
1014
#[allow(dead_code)]
1115
enum Direction {

tests/common/test3.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
use async_std::fs::File;
2+
use async_std::io::prelude::*;
3+
use async_std::io::{self, SeekFrom};
4+
use async_std::sync::Arc;
5+
use async_std::task::{Context, Poll};
6+
use std::pin::Pin;
7+
use std::sync::Mutex;
8+
9+
#[derive(Clone)]
10+
pub struct TestCase {
11+
reader: Arc<Mutex<File>>,
12+
writer: Arc<Mutex<File>>,
13+
}
14+
15+
impl TestCase {
16+
/// Create a new instance.
17+
pub async fn new(reader: &str, writer: &str) -> TestCase {
18+
use std::io::Write;
19+
20+
let mut temp = tempfile::tempfile().expect("Failed writer create tempfile");
21+
temp.write(reader.as_bytes())
22+
.expect("Could not write writer dest file");
23+
let mut file: File = temp.into();
24+
file.seek(SeekFrom::Start(0)).await.unwrap();
25+
let reader = Arc::new(Mutex::new(file.into()));
26+
27+
let mut temp = tempfile::tempfile().expect("Failed writer create tempfile");
28+
temp.write(writer.as_bytes())
29+
.expect("Could not write writer dest file");
30+
let mut file: File = temp.into();
31+
file.seek(SeekFrom::Start(0)).await.unwrap();
32+
let writer = Arc::new(Mutex::new(file.into()));
33+
34+
TestCase { reader, writer }
35+
}
36+
37+
/// Get the value of the "writer" string.
38+
pub async fn writer(&self) -> String {
39+
let mut writer = String::new();
40+
let mut file = self.writer.lock().unwrap();
41+
file.seek(SeekFrom::Start(0)).await.unwrap();
42+
file.read_to_string(&mut writer).await.unwrap();
43+
writer
44+
}
45+
46+
/// Assert the reader.
47+
pub async fn assert_reader(&self, rhs: &str) {
48+
let mut lhs = self.reader().await;
49+
pretty_assertions::assert_eq!(lhs, rhs);
50+
}
51+
52+
/// Assert the reader but pass it through a closure first..
53+
pub async fn assert_reader_with(&self, mut rhs: &str, f: impl Fn(&mut String, &mut String)) {
54+
let mut lhs = self.reader().await;
55+
let mut rhs = String::from(rhs);
56+
f(&mut lhs, &mut rhs);
57+
pretty_assertions::assert_eq!(lhs, rhs);
58+
}
59+
60+
/// Assert the writer.
61+
pub async fn assert_writer(&self, rhs: &str) {
62+
let mut lhs = self.writer().await;
63+
pretty_assertions::assert_eq!(lhs, rhs);
64+
}
65+
66+
/// Assert the reader but pass it through a closure first..
67+
pub async fn assert_writer_with(&self, mut rhs: &str, f: impl Fn(&mut String, &mut String)) {
68+
let mut lhs = self.writer().await;
69+
let mut rhs = String::from(rhs);
70+
f(&mut lhs, &mut rhs);
71+
pretty_assertions::assert_eq!(lhs, rhs);
72+
}
73+
74+
/// Get the value of the "reader" string.
75+
pub async fn reader(&self) -> String {
76+
let mut reader = String::new();
77+
let mut file = self.reader.lock().unwrap();
78+
file.seek(SeekFrom::Start(0)).await.unwrap();
79+
file.read_to_string(&mut reader).await.unwrap();
80+
reader
81+
}
82+
}
83+
84+
impl Read for TestCase {
85+
fn poll_read(
86+
self: Pin<&mut Self>,
87+
cx: &mut Context,
88+
buf: &mut [u8],
89+
) -> Poll<io::Result<usize>> {
90+
Pin::new(&mut &*self.reader.lock().unwrap()).poll_read(cx, buf)
91+
}
92+
}
93+
94+
impl Write for TestCase {
95+
fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<io::Result<usize>> {
96+
Pin::new(&mut &*self.writer.lock().unwrap()).poll_write(cx, buf)
97+
}
98+
99+
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
100+
Pin::new(&mut &*self.writer.lock().unwrap()).poll_flush(cx)
101+
}
102+
103+
fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
104+
Pin::new(&mut &*self.writer.lock().unwrap()).poll_close(cx)
105+
}
106+
}

tests/server-chunked-large.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use http_types::{Body, Response, StatusCode};
2+
3+
mod common;
4+
5+
const REQUEST: &'static str = concat![
6+
"GET / HTTP/1.1\r\n",
7+
"host: example.com\r\n",
8+
"user-agent: curl/7.54.0\r\n",
9+
"content-type: text/plain\r\n",
10+
"transfer-encoding: chunked\r\n",
11+
"\r\n",
12+
"453\r\n",
13+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
14+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
15+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
16+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
17+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
18+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
19+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
20+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
21+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
22+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
23+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
24+
"\r\n",
25+
"0",
26+
"\r\n",
27+
"\r\n",
28+
];
29+
30+
const RESPONSE: &'static str = concat![
31+
"HTTP/1.1 200 OK\r\n",
32+
"transfer-encoding: chunked\r\n",
33+
"date: {DATE}\r\n",
34+
"content-type: application/octet-stream\r\n",
35+
"\r\n",
36+
"453\r\n",
37+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
38+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
39+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
40+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
41+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
42+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
43+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
44+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
45+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
46+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
47+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
48+
"\r\n",
49+
"0",
50+
"\r\n",
51+
"\r\n",
52+
];
53+
54+
#[async_std::test]
55+
async fn server_chunked_large() {
56+
let case = common::TestCase2::new(REQUEST, "").await;
57+
async_h1::accept("http://example.com", case.clone(), |req| async {
58+
let mut res = Response::new(StatusCode::Ok);
59+
res.set_body(Body::from_reader(req, None));
60+
Ok(res)
61+
})
62+
.await
63+
.unwrap();
64+
case.assert_writer_with(RESPONSE, common::munge_date).await;
65+
}

0 commit comments

Comments
 (0)