Skip to content

Commit d5ef065

Browse files
committed
Have tests run independently of project structure
1 parent 39af37f commit d5ef065

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/conduit-static.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,40 @@ impl Handler for Static {
5252
#[cfg(test)]
5353
mod tests {
5454
extern crate test = "conduit-test";
55+
56+
use std::io::{fs, File, TempDir, UserRWX};
57+
5558
use conduit;
5659
use conduit::Handler;
5760
use Static;
5861

5962
#[test]
6063
fn test_static() {
61-
let root = Path::new(file!()).dir_path().dir_path();
62-
let handler = Static::new(root);
64+
let td = TempDir::new("conduit-static").unwrap();
65+
let root = td.path();
66+
let handler = Static::new(root.clone());
67+
File::create(&root.join("Cargo.toml")).write(b"[package]").unwrap();
6368
let mut req = test::MockRequest::new(conduit::Get, "/Cargo.toml");
6469
let mut res = handler.call(&mut req).ok().expect("No response");
6570
let body = res.body.read_to_str().ok().expect("No body");
66-
assert!(body.as_slice().contains("[package]"), "The Cargo.toml was provided");
67-
assert_eq!(res.headers.find_equiv(&"Content-Type").expect("No content-type"), &vec!("text/plain".to_str()));
71+
assert_eq!(body.as_slice(), "[package]");
72+
assert_eq!(res.headers.find_equiv(&"Content-Type")
73+
.expect("No content-type"),
74+
&vec!("text/plain".to_str()));
6875
}
6976

7077
#[test]
7178
fn test_mime_types() {
72-
let root = Path::new(file!()).dir_path().dir_path();
73-
let handler = Static::new(root);
79+
let td = TempDir::new("conduit-static").unwrap();
80+
let root = td.path();
81+
fs::mkdir(&root.join("src"), UserRWX).unwrap();
82+
File::create(&root.join("src/fixture.css")).unwrap();
83+
84+
let handler = Static::new(root.clone());
7485
let mut req = test::MockRequest::new(conduit::Get, "/src/fixture.css");
7586
let res = handler.call(&mut req).ok().expect("No response");
76-
assert_eq!(res.headers.find_equiv(&"Content-Type").expect("No content-type"), &vec!("text/css".to_str()));
87+
assert_eq!(res.headers.find_equiv(&"Content-Type")
88+
.expect("No content-type"),
89+
&vec!("text/css".to_str()));
7790
}
7891
}

0 commit comments

Comments
 (0)