Skip to content

Commit 9e70497

Browse files
committed
add integration tests
1 parent 20b1610 commit 9e70497

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

tests/client.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
extern crate reqwest;
2+
3+
#[macro_use] mod server;
4+
5+
use std::io::Read;
6+
7+
#[test]
8+
fn test_get() {
9+
let server = server! {
10+
request: b"\
11+
GET /1 HTTP/1.1\r\n\
12+
Host: $HOST\r\n\
13+
User-Agent: $USERAGENT\r\n\
14+
\r\n\
15+
",
16+
response: b"\
17+
HTTP/1.1 200 OK\r\n\
18+
Server: test\r\n\
19+
Content-Length: 0\r\n\
20+
\r\n\
21+
"
22+
};
23+
24+
let mut res = reqwest::get(&format!("http://{}/1", server.addr())).unwrap();
25+
assert_eq!(res.status(), &reqwest::StatusCode::Ok);
26+
assert_eq!(res.version(), &reqwest::HttpVersion::Http11);
27+
assert_eq!(res.headers().get(), Some(&reqwest::header::Server("test".to_string())));
28+
assert_eq!(res.headers().get(), Some(&reqwest::header::ContentLength(0)));
29+
30+
let mut buf = [0; 1024];
31+
let n = res.read(&mut buf).unwrap();
32+
assert_eq!(n, 0)
33+
}
34+
35+
#[test]
36+
fn test_redirect_302_changes_post_to_get() {
37+
38+
let redirect = server! {
39+
request: b"\
40+
POST /302 HTTP/1.1\r\n\
41+
Host: $HOST\r\n\
42+
User-Agent: $USERAGENT\r\n\
43+
Content-Length: 0\r\n\
44+
\r\n\
45+
",
46+
response: b"\
47+
HTTP/1.1 302 Found\r\n\
48+
Server: test-redirect\r\n\
49+
Content-Length: 0\r\n\
50+
Location: /dst\r\n\
51+
Connection: close\r\n\
52+
\r\n\
53+
",
54+
55+
request: b"\
56+
GET /dst HTTP/1.1\r\n\
57+
Host: $HOST\r\n\
58+
User-Agent: $USERAGENT\r\n\
59+
Referer: http://$HOST/302\r\n\
60+
\r\n\
61+
",
62+
response: b"\
63+
HTTP/1.1 200 OK\r\n\
64+
Server: test-dst\r\n\
65+
Content-Length: 0\r\n\
66+
\r\n\
67+
"
68+
};
69+
70+
let client = reqwest::Client::new().unwrap();
71+
let res = client.post(&format!("http://{}/302", redirect.addr()))
72+
.send()
73+
.unwrap();
74+
assert_eq!(res.status(), &reqwest::StatusCode::Ok);
75+
assert_eq!(res.headers().get(), Some(&reqwest::header::Server("test-dst".to_string())));
76+
77+
}

tests/server.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//! A server builder helper for the integration tests.
2+
3+
use std::io::{Read, Write};
4+
use std::net;
5+
use std::thread;
6+
7+
pub struct Server {
8+
addr: net::SocketAddr,
9+
}
10+
11+
impl Server {
12+
pub fn addr(&self) -> net::SocketAddr {
13+
self.addr
14+
}
15+
}
16+
17+
static DEFAULT_USER_AGENT: &'static str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
18+
19+
pub fn spawn(txns: Vec<(Vec<u8>, Vec<u8>)>) -> Server {
20+
let listener = net::TcpListener::bind("0.0.0.0:0").unwrap();
21+
let addr = listener.local_addr().unwrap();
22+
thread::spawn(move || {
23+
for (mut expected, reply) in txns {
24+
let (mut socket, _addr) = listener.accept().unwrap();
25+
replace_expected_vars(&mut expected, addr.to_string().as_ref(), DEFAULT_USER_AGENT.as_ref());
26+
let mut buf = [0; 4096];
27+
let n = socket.read(&mut buf).unwrap();
28+
29+
match (::std::str::from_utf8(&expected), ::std::str::from_utf8(&buf[..n])) {
30+
(Ok(expected), Ok(received)) => assert_eq!(expected, received),
31+
_ => assert_eq!(expected, &buf[..n])
32+
}
33+
socket.write_all(&reply).unwrap();
34+
}
35+
});
36+
37+
Server {
38+
addr: addr,
39+
}
40+
}
41+
42+
fn replace_expected_vars(bytes: &mut Vec<u8>, host: &[u8], ua: &[u8]) {
43+
// plenty horrible, but these are just tests, and gets the job done
44+
let mut index = 0;
45+
loop {
46+
if index == bytes.len() {
47+
return;
48+
}
49+
50+
for b in (&bytes[index..]).iter() {
51+
index += 1;
52+
if *b == b'$' {
53+
break;
54+
}
55+
}
56+
57+
let has_host = (&bytes[index..]).starts_with(b"HOST");
58+
if has_host {
59+
bytes.drain(index - 1..index + 4);
60+
for (i, b) in host.iter().enumerate() {
61+
bytes.insert(index - 1 + i, *b);
62+
}
63+
} else {
64+
let has_ua = (&bytes[index..]).starts_with(b"USERAGENT");
65+
if has_ua {
66+
bytes.drain(index - 1..index + 9);
67+
for (i, b) in ua.iter().enumerate() {
68+
bytes.insert(index - 1 + i, *b);
69+
}
70+
}
71+
}
72+
}
73+
}
74+
75+
#[macro_export]
76+
macro_rules! server {
77+
($(request: $req:expr, response: $res:expr),*) => ({
78+
let txns = vec![
79+
$(((&$req[..]).into(), (&$res[..]).into()),)*
80+
];
81+
::server::spawn(txns)
82+
})
83+
}

0 commit comments

Comments
 (0)