Add a simple test for the eventbus index route

This commit is contained in:
R Tyler Croy 2020-01-05 14:09:45 -08:00
parent 6a1f3636c8
commit 8d49aded94
No known key found for this signature in database
GPG Key ID: E5C92681BEF6CEA2
3 changed files with 52 additions and 0 deletions

2
Cargo.lock generated
View File

@ -1327,6 +1327,7 @@ name = "otto-eventbus"
version = "0.1.0"
dependencies = [
"actix 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"actix-http 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"actix-rt 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"actix-web 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"actix-web-actors 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1336,6 +1337,7 @@ dependencies = [
"handlebars 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"pretty_env_logger 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rust-embed 5.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustyline 5.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -18,6 +18,7 @@ actix = "~0.9.0"
actix-web = "~2.0.0"
actix-web-actors = "~2.0.0"
actix-rt = "~1.0.0"
actix-http = "~1.0.1"
handlebars = "~2.0.2"
# Need to embed static resources into the binary for serving at runtime
@ -44,3 +45,7 @@ tungstenite = "~0.9.2"
url = "~2.1.0"
# Needed for the nice bits in the cli
rustyline = "~5.0.6"
[dev-dependencies]
regex = "1"

View File

@ -4,6 +4,7 @@
*/
extern crate actix;
extern crate actix_web;
extern crate actix_http;
extern crate config;
extern crate log;
extern crate pretty_env_logger;
@ -157,3 +158,47 @@ async fn main() -> std::io::Result<()> {
.run()
.await
}
#[cfg(test)]
mod test {
use super::*;
use actix_web::{test, web, App};
use regex::Regex;
/**
* This test just ensures that the server can come online properly and render its index handler
* properly.
*
* It doesn't really test much useful, but does ensure that critical failures in the eventbus
* can sometimes be prevented
*/
#[actix_rt::test]
async fn test_basic_http() {
let events = eventbus::EventBus::with_channels(vec![], vec![]).start();
let state = AppState {
bus: events,
hb: Arc::new(Handlebars::new()),
};
let wd = web::Data::new(state);
let srv = test::start(move || {
App::new()
.app_data(wd.clone())
.route("/", web::get().to(index))
});
let req = srv.get("/");
let mut response = req.send().await.unwrap();
assert!(response.status().is_success());
let re = Regex::new(r"(v\d\.\d\.\d)").unwrap();
let body = response.body().await.unwrap();
let buffer = String::from_utf8(body.to_vec()).unwrap();
let matches = re.captures(&buffer).unwrap();
let version = matches.get(1).unwrap();
assert_eq!(version.as_str(), format!("v{}", option_env!("CARGO_PKG_VERSION").unwrap_or("unknown")));
}
}