-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.rs
44 lines (38 loc) · 1.38 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use std::env;
use std::time::Duration;
use hreq::prelude::*;
fn main() {
match smoke_test() {
Ok(()) => println!("Did not hang! Success"),
Err(err) => {
println!("Did not hang! Error: {}", err);
std::process::exit(1);
}
}
}
fn smoke_test() -> Result<(), Box<dyn std::error::Error>> {
let url = format!(
"http://{}",
env::args().skip(1).next().expect("No URL provided")
);
println!("Fetching {}", url);
let response = Request::builder()
.uri(&url)
.timeout(Duration::from_secs(40))
.call().block()?;
println!("HTTP status code: {}", response.status());
// Print headers
for header_name in response.headers().keys() {
for value in response.headers().get_all(header_name).iter() {
println!("Header: {}: {:?}", header_name, value);
}
}
// This retains the whole body in memory, but tests show that RAM is plentiful, so I didn't bother optimizing.
// Converting to a string lets us exercise encoding conversion routines.
let mut body = response.into_body();
let text = body.read_to_string().block()?;
// Print the first 8k chars of the body to get an idea of what we've downloaded, ignore the rest.
let first_8k_chars_of_body: String = text.chars().take(8192).collect();
println!("{}", first_8k_chars_of_body);
Ok(())
}