Skip to content

Commit b01d8f4

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

File tree

2 files changed

+75
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)