Commit f0a4e036 authored by Robert Czechowski's avatar Robert Czechowski
Browse files

Add test to check for server start and site response

parent 3c8fb5be
all: all:
RUSTFLAGS=-Awarnings RUST_BACKTRACE=1 cargo run --features watch RUSTFLAGS=-Awarnings RUST_BACKTRACE=1 cargo run --features watch
test:
RUSTFLAGS=-Awarnings RUST_BACKTRACE=1 cargo test
...@@ -46,7 +46,7 @@ use std::fs; ...@@ -46,7 +46,7 @@ use std::fs;
use std::path::{Path,PathBuf}; use std::path::{Path,PathBuf};
use structopt::StructOpt; use structopt::StructOpt;
#[derive(Serialize, Deserialize, Clone)] #[derive(Serialize, Deserialize, Clone, Default)]
pub struct Config { pub struct Config {
host: Option<String>, host: Option<String>,
port: Option<u16>, port: Option<u16>,
...@@ -64,23 +64,13 @@ fn read_config_from_file(file: &Path) -> Config { ...@@ -64,23 +64,13 @@ fn read_config_from_file(file: &Path) -> Config {
println!("Reading Config file '{}'", file.to_str().unwrap_or("<Encoding error>")); println!("Reading Config file '{}'", file.to_str().unwrap_or("<Encoding error>"));
let mut config = if let Ok(mut file) = fs::File::open(file) { let mut config : Config = if let Ok(mut file) = fs::File::open(file) {
let mut contents = String::new(); let mut contents = String::new();
file.read_to_string(&mut contents).unwrap(); file.read_to_string(&mut contents).unwrap();
serde_json::from_str(&contents).unwrap() serde_json::from_str(&contents).unwrap()
} else { } else {
println!("Configuration file '{}' not found.", file.to_str().unwrap_or("<Encoding error>")); println!("Configuration file '{}' not found.", file.to_str().unwrap_or("<Encoding error>"));
Config { Default::default()
host: None,
port: None,
self_url: None,
oauth_url: None,
oauth_client_id: None,
oauth_client_secret: None,
oauth_access_token_url: None,
oauth_user_data_url: None,
database_file: None,
}
}; };
if config.host.is_none() {config.host = Some("[::]".to_string())} if config.host.is_none() {config.host = Some("[::]".to_string())}
...@@ -196,3 +186,61 @@ fn main() { ...@@ -196,3 +186,61 @@ fn main() {
println!("Could not run server. Is the port already in use?"); println!("Could not run server. Is the port already in use?");
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn start_server_and_check_request() {
use std::{thread, time};
let mut conn = Connection::open_in_memory().unwrap();
db_apply_migrations::test(&mut conn);
// add contests / tasks here
use std::sync::{Arc, Mutex, Condvar};
let pair = Arc::new((Mutex::new(false), Condvar::new()));
let pair_ = pair.clone();
let mut config = read_config_from_file(Path::new("thisfileshoudnotexist"));
let srvr = start_server(conn, config);
thread::spawn(move || {
// wait for server to start:
thread::sleep(time::Duration::from_millis(100));
use std::io::Read;
let mut resp = reqwest::get("http://localhost:8080").unwrap();
assert!(resp.status().is_success());
let mut content = String::new();
resp.read_to_string(&mut content);
assert!(content.contains("<h1>Jugendwettbewerb Informatik</h1>"));
assert!(!content.contains("Error"));
let &(ref lock, ref cvar) = &*pair_;
let mut should_exit = lock.lock().unwrap();
*should_exit = true;
cvar.notify_one();
//fs::copy("foo.txt", "bar.txt").unwrap();
});
// Copied from docs
let &(ref lock, ref cvar) = &*pair;
let mut should_exit = lock.lock().unwrap();
while !*should_exit {
should_exit = cvar.wait(should_exit).unwrap();
}
srvr.unwrap().close().unwrap();
assert!(true);
}
}
...@@ -773,7 +773,7 @@ fn cookie_warning(req: &mut Request) -> IronResult<Response> { ...@@ -773,7 +773,7 @@ fn cookie_warning(req: &mut Request) -> IronResult<Response> {
} }
pub fn start_server(conn: Connection, config: ::Config) { pub fn start_server(conn: Connection, config: ::Config) -> iron::error::HttpResult<iron::Listening> {
let router = router!( let router = router!(
greet: get "/" => greet_personal, greet: get "/" => greet_personal,
contests: get "/contest/" => contests, contests: get "/contest/" => contests,
...@@ -824,8 +824,9 @@ pub fn start_server(conn: Connection, config: ::Config) { ...@@ -824,8 +824,9 @@ pub fn start_server(conn: Connection, config: ::Config) {
let socket_addr = format!("{}:{}", config.host.unwrap(), config.port.unwrap()); let socket_addr = format!("{}:{}", config.host.unwrap(), config.port.unwrap());
let _res = Iron::new(ch).http(&socket_addr); let srvr = Iron::new(ch).http(&socket_addr);
println!("Listening on {}.", &socket_addr); println!("Listening on {}.", &socket_addr);
srvr
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment