Skip to content

Commit adf1604

Browse files
committed
add tests for 100-continue
1 parent 4c29727 commit adf1604

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ pretty_assertions = "0.6.1"
2626
async-std = { version = "1.4.0", features = ["unstable", "attributes"] }
2727
tempfile = "3.1.0"
2828
async-test = "1.0.0"
29+
duplexify = { version = "1.1.0", git = "https://github.com/jbr/duplexify", branch = "impl-clone-when-reader-and-writer-are-clone" }
30+
async-dup = "1.2.1"

tests/continue.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use async_dup::{Arc, Mutex};
2+
use async_std::io::{Cursor, SeekFrom};
3+
use async_std::{prelude::*, task};
4+
use duplexify::Duplex;
5+
use http_types::Result;
6+
use std::time::Duration;
7+
8+
const REQUEST_WITH_EXPECT: &[u8] = b"POST / HTTP/1.1\r\n\
9+
Host: example.com\r\n\
10+
Content-Length: 10\r\n\
11+
Expect: 100-continue\r\n\r\n";
12+
13+
const SLEEP_DURATION: Duration = std::time::Duration::from_millis(100);
14+
#[async_std::test]
15+
async fn test_with_expect_when_reading_body() -> Result<()> {
16+
let client_str: Vec<u8> = REQUEST_WITH_EXPECT.to_vec();
17+
let server_str: Vec<u8> = vec![];
18+
19+
let mut client = Arc::new(Mutex::new(Cursor::new(client_str)));
20+
let server = Arc::new(Mutex::new(Cursor::new(server_str)));
21+
22+
let mut request = async_h1::server::decode(Duplex::new(client.clone(), server.clone()))
23+
.await?
24+
.unwrap();
25+
26+
task::sleep(SLEEP_DURATION).await; //prove we're not just testing before we've written
27+
28+
{
29+
let lock = server.lock();
30+
assert_eq!("", std::str::from_utf8(lock.get_ref())?); //we haven't written yet
31+
};
32+
33+
let mut buf = vec![0u8; 1];
34+
let bytes = request.read(&mut buf).await?; //this triggers the 100-continue even though there's nothing to read yet
35+
assert_eq!(bytes, 0); // normally we'd actually be waiting for the end of the buffer, but this lets us test this sequentially
36+
37+
task::sleep(SLEEP_DURATION).await; // just long enough to wait for the channel and io
38+
39+
{
40+
let lock = server.lock();
41+
assert_eq!(
42+
"HTTP/1.1 100 Continue\r\n\r\n",
43+
std::str::from_utf8(lock.get_ref())?
44+
);
45+
};
46+
47+
client.write_all(b"0123456789").await?;
48+
client
49+
.seek(SeekFrom::Start(REQUEST_WITH_EXPECT.len() as u64))
50+
.await?;
51+
52+
assert_eq!("0123456789", request.body_string().await?);
53+
54+
Ok(())
55+
}
56+
57+
#[async_std::test]
58+
async fn test_without_expect_when_not_reading_body() -> Result<()> {
59+
let client_str: Vec<u8> = REQUEST_WITH_EXPECT.to_vec();
60+
let server_str: Vec<u8> = vec![];
61+
62+
let client = Arc::new(Mutex::new(Cursor::new(client_str)));
63+
let server = Arc::new(Mutex::new(Cursor::new(server_str)));
64+
65+
async_h1::server::decode(Duplex::new(client.clone(), server.clone()))
66+
.await?
67+
.unwrap();
68+
69+
task::sleep(SLEEP_DURATION).await; // just long enough to wait for the channel
70+
71+
let server_lock = server.lock();
72+
assert_eq!("", std::str::from_utf8(server_lock.get_ref())?); // we haven't written 100-continue
73+
74+
Ok(())
75+
}

0 commit comments

Comments
 (0)