Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests can now run in any order #22

Merged
merged 2 commits into from
Jun 26, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 48 additions & 20 deletions tests/tungsten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,73 @@ extern crate hyper;
extern crate tungsten;

use std::thread;
use std::sync::{Once, ONCE_INIT};
use std::io::Read;
use hyper::status::StatusCode;
use hyper::client::Client;
use hyper::client::response::Response as HyperResponse;
use tungsten::server::Tungsten;
use tungsten::request::Request;
use tungsten::response::Response;
use tungsten::router::HttpMethods;

fn serve() {
let _ = thread::Builder::new().name(String::from("test-server")).spawn(move || {
let mut app = Tungsten::new();
app.get("/".to_string(), |req: Request, res: Response| {
res.send(b"Hello World!");
});
app.get("/some/random/path".to_string(), |req: Request, res: Response| {
res.send(b"You are at /some/random/path");
});
app.listen("0.0.0.0:4040");
});
static TEST_INIT: Once = ONCE_INIT;

struct TestContext {
req_client: Client
}

impl TestContext {
fn new() -> TestContext {
let ctx = TestContext {
req_client: Client::new()
};
TEST_INIT.call_once(|| {
let _ = thread::spawn(move || {
let mut app = Tungsten::new();
app.get("/".to_string(), |req: Request, res: Response| {
res.send(b"Hello World!");
});
app.get("/some/random/path".to_string(), |req: Request, res: Response| {
res.send(b"You are at /some/random/path");
});
app.listen("0.0.0.0:4040");
});
loop {
if ctx.req_client.get("http://0.0.0.0:4040").send().is_ok() {
break;
}
}
});
ctx
}

fn request(&self, url: &str) -> HyperResponse {
self.req_client.get(url).send().unwrap()
}

fn body_from_response(&self, res: &mut HyperResponse) -> String {
let mut body = String::new();
let _ = res.read_to_string(&mut body);
body
}
}

#[test]
fn test_hello_world() {
serve();
let client = Client::new();
let mut res = client.get("http://0.0.0.0:4040").send().unwrap();
let mut body = String::new();
let _ = res.read_to_string(&mut body);
let ctx = TestContext::new();
let mut res = ctx.request("http://0.0.0.0:4040");
let body = ctx.body_from_response(&mut res);

assert_eq!(res.status, StatusCode::Ok);
assert_eq!(body, "Hello World!");
}

#[test]
fn test_some_path() {
let client = Client::new();
let mut res = client.get("http://0.0.0.0:4040/some/random/path").send().unwrap();
let mut body = String::new();
let _ = res.read_to_string(&mut body);
let ctx = TestContext::new();
let mut res = ctx.request("http://0.0.0.0:4040/some/random/path");
let body = ctx.body_from_response(&mut res);

assert_eq!(res.status, StatusCode::Ok);
assert_eq!(body, "You are at /some/random/path");
Expand Down