Skip to content

Instantly share code, notes, and snippets.

@dbrgn
Created November 15, 2018 21:27
Show Gist options
  • Save dbrgn/412e774d0cb43aa6babb39f34f289b9a to your computer and use it in GitHub Desktop.
Save dbrgn/412e774d0cb43aa6babb39f34f289b9a to your computer and use it in GitHub Desktop.
extern crate hyper;
extern crate reqwest;
extern crate rustysafe;
use std::thread;
use hyper::Server;
use hyper::rt::Future;
use hyper::service::service_fn;
/// Create a new test server instance and return the bound port.
fn testserver() -> (thread::JoinHandle<()>, u16) {
let addr = ([127, 0, 0, 1], 0).into();
let service = || service_fn(rustysafe::handler);
let server = Server::bind(&addr).serve(service);
let port = server.local_addr().port();
let handle = thread::spawn(move || {
hyper::rt::run(server.map_err(|e| eprintln!("Server error: {}", e)));
});
(handle, port)
}
#[test]
fn get_index_1() {
let (_handle, port) = testserver();
let res = reqwest::get(&format!("http://127.0.0.1:{}/", port));
println!("Port {}: {:?}", port, res);
}
#[test]
fn get_index_2() {
let (_handle, port) = testserver();
let res = reqwest::get(&format!("http://127.0.0.1:{}/", port));
println!("Port {}: {:?}", port, res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment