use util::*;
use hyper::status::StatusCode;
use hyper::client::Response;
use std::io::{Read, Write};
use std::net::TcpStream;
fn with_path<F>(path: &str, f: F) where F: FnOnce(&mut Response) {
run_example("static_files", |port| {
let url = format!("http://localhost:{}{}", port, path);
let ref mut res = response_for(&url);
f(res)
})
}
#[test]
fn returns_expected_files() {
with_path("/thoughtram_logo_brain.png", |res| {
assert_eq!(res.status, StatusCode::Ok);
});
}
#[test]
fn nested_files() {
with_path("/nested/foo.js", |res| {
let s = read_body_to_string(res);
assert!(s.starts_with("function foo"), "unexpected response: {:?}", s);
});
}
#[test]
fn rejects_parent_folder_access() {
run_example("static_files", |port| {
let address = format!("localhost:{}", port);
let mut stream = TcpStream::connect(&*address).unwrap();
stream.write_all(b"GET /../../Cargo.toml HTTP/1.1\n\
Connection: close\n\n").unwrap();
let mut response = String::new();
stream.read_to_string(&mut response).unwrap();
assert!(response.contains("400 Bad Request"), "Response was {:?}", response);
})
}
#[test]
fn fallthroughs_with_same_prefix() {
with_path("/static/files/a", |res| {
let s = read_body_to_string(res);
assert_eq!(s, "Not Found");
});
}