Rocket/examples/config/tests/common/mod.rs

55 lines
2.2 KiB
Rust
Raw Normal View History

2016-12-27 22:35:14 +00:00
extern crate rocket;
extern crate config as lib;
2016-12-29 00:24:54 +00:00
2016-12-27 22:35:14 +00:00
use rocket::config::{self, Environment};
use rocket::http::Method;
use rocket::LoggingLevel;
use rocket::testing::MockRequest;
pub fn test_config(environment: Environment) {
2016-12-29 00:24:54 +00:00
// Manually set the config environment variable. Rocket will initialize the
// environment in `ignite()`.
::std::env::set_var("ROCKET_ENV", environment.to_string());
2016-12-27 22:35:14 +00:00
rocket::ignite().mount("/hello", routes![lib::hello]);
2016-12-29 00:24:54 +00:00
// Get the active environment and ensure that it matches our expectations.
2016-12-27 22:35:14 +00:00
let config = config::active().unwrap();
match environment {
Environment::Development => {
assert_eq!(config.address, "localhost".to_string());
assert_eq!(config.port, 8000);
assert_eq!(config.workers, 1);
2016-12-27 22:35:14 +00:00
assert_eq!(config.log_level, LoggingLevel::Normal);
assert_eq!(config.environment, config::Environment::Development);
2016-12-27 22:35:14 +00:00
assert_eq!(config.extras().count(), 2);
assert_eq!(config.get_str("hi"), Ok("Hello!"));
assert_eq!(config.get_bool("is_extra"), Ok(true));
}
Environment::Staging => {
assert_eq!(config.address, "0.0.0.0".to_string());
assert_eq!(config.port, 80);
assert_eq!(config.workers, 8);
2016-12-27 22:35:14 +00:00
assert_eq!(config.log_level, LoggingLevel::Normal);
assert_eq!(config.environment, config::Environment::Staging);
2016-12-27 22:35:14 +00:00
assert_eq!(config.extras().count(), 0);
}
Environment::Production => {
assert_eq!(config.address, "0.0.0.0".to_string());
assert_eq!(config.port, 80);
assert_eq!(config.workers, 12);
2016-12-27 22:35:14 +00:00
assert_eq!(config.log_level, LoggingLevel::Critical);
assert_eq!(config.environment, config::Environment::Production);
2016-12-27 22:35:14 +00:00
assert_eq!(config.extras().count(), 0);
}
}
}
pub fn test_hello() {
let rocket = rocket::ignite().mount("/hello", routes![lib::hello]);
let mut request = MockRequest::new(Method::Get, "/hello");
let mut response = request.dispatch_with(&rocket);
assert_eq!(response.body().and_then(|b| b.into_string()),
Some("Hello, world!".to_string()));
}