Skip to content

Commit 94f59e8

Browse files
authored
Merge pull request #15 from yoshuawuyts/tests
Add beginnings of tests for server
2 parents bef59cc + b9f391a commit 94f59e8

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ futures-core-preview = "0.3.0-alpha.18"
2020

2121
[dev-dependencies]
2222
futures-util = "0.3.0"
23+
pretty_assertions = "0.6.1"

tests/common/mod.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::{fs::File, path::PathBuf};
2+
3+
#[macro_export]
4+
macro_rules! assert {
5+
($actual:expr, $expected:expr, $block:expr) => {
6+
task::block_on(async {
7+
$block.await.unwrap();
8+
pretty_assertions::assert_eq!(
9+
std::str::from_utf8(&$actual).unwrap(),
10+
std::str::from_utf8(&$expected).unwrap()
11+
);
12+
})
13+
};
14+
}
15+
16+
pub fn read_fixture(name: &str) -> Vec<u8> {
17+
use std::io::Read;
18+
19+
let directory: PathBuf = env!("CARGO_MANIFEST_DIR").into();
20+
let path: PathBuf = format!("tests/fixtures/{}.txt", name).into();
21+
let mut file = File::open(directory.join(path)).expect("Reading fixture file didn't work");
22+
let mut contents = Vec::new();
23+
file.read_to_end(&mut contents)
24+
.expect("Couldn't read fixture files contents");
25+
26+
let mut result = Vec::<u8>::new();
27+
for byte in contents {
28+
if byte == 0x0A {
29+
result.push(0x0D);
30+
}
31+
result.push(byte);
32+
}
33+
result
34+
}

tests/fixtures/request1.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
POST / HTTP/1.1
2+
Host: localhost:8080
3+
User-Agent: curl/7.54.0
4+
Accept: */*
5+
Content-Length: 2
6+
Content-Type: application/x-www-form-urlencoded
7+

tests/fixtures/response1.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
HTTP/1.1 200 OK
2+
Content-Length: 0
3+

tests/server.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
mod common;
2+
use async_h1::{server, Body};
3+
use async_std::task;
4+
use common::read_fixture;
5+
use http::Response;
6+
7+
#[test]
8+
fn test_basic_request() {
9+
let request = read_fixture("request1");
10+
let expected = read_fixture("response1");
11+
let mut actual = Vec::new();
12+
13+
assert!(
14+
actual,
15+
expected,
16+
server::connect(&request[..], &mut actual, |_req| {
17+
async { Ok(Response::new(Body::empty("".as_bytes()))) }
18+
})
19+
);
20+
}

0 commit comments

Comments
 (0)