diff --git a/examples/config/Cargo.toml b/examples/config/Cargo.toml index 413d7cdc..2a83e1c5 100644 --- a/examples/config/Cargo.toml +++ b/examples/config/Cargo.toml @@ -7,3 +7,6 @@ workspace = "../../" [dependencies] rocket = { path = "../../lib" } rocket_codegen = { path = "../../codegen" } + +[dev-dependencies] +rocket = { path = "../../lib", features = ["testing"] } diff --git a/examples/config/src/lib.rs b/examples/config/src/lib.rs new file mode 100644 index 00000000..c61740e3 --- /dev/null +++ b/examples/config/src/lib.rs @@ -0,0 +1,9 @@ +#![feature(plugin)] +#![plugin(rocket_codegen)] + +extern crate rocket; + +#[get("/")] +pub fn hello() -> &'static str { + "Hello, world!" +} diff --git a/examples/config/src/main.rs b/examples/config/src/main.rs index 1198442d..30fb2b83 100644 --- a/examples/config/src/main.rs +++ b/examples/config/src/main.rs @@ -2,14 +2,12 @@ #![plugin(rocket_codegen)] extern crate rocket; +extern crate config; + +#[cfg(test)] +mod tests; // This example's illustration is the Rocket.toml file. - -#[get("/")] -fn hello() -> &'static str { - "Hello, world!" -} - fn main() { - rocket::ignite().mount("/hello", routes![hello]).launch() + rocket::ignite().mount("/hello", routes![config::hello]).launch() } diff --git a/examples/config/src/tests.rs b/examples/config/src/tests.rs new file mode 100644 index 00000000..7f8bc55a --- /dev/null +++ b/examples/config/src/tests.rs @@ -0,0 +1,13 @@ +use super::rocket; +use rocket::testing::MockRequest; +use rocket::http::Method; + +#[test] +fn test_hello_world() { + let rocket = rocket::ignite().mount("/hello", routes![super::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())); +} diff --git a/examples/config/tests/common/mod.rs b/examples/config/tests/common/mod.rs new file mode 100644 index 00000000..375b22a1 --- /dev/null +++ b/examples/config/tests/common/mod.rs @@ -0,0 +1,53 @@ +extern crate rocket; +extern crate config as lib; +use std; +use rocket::config::{self, Environment}; +use rocket::http::Method; +use rocket::LoggingLevel; +use rocket::testing::MockRequest; + + +pub fn test_config(environment: Environment) { + // Manually set the config environment variable so that Rocket initializes it in `init()`. + std::env::set_var("ROCKET_ENV", environment.to_string()); + rocket::ignite().mount("/hello", routes![lib::hello]); + + let config = config::active().unwrap(); + match environment { + Environment::Development => { + assert_eq!(config.address, "localhost".to_string()); + assert_eq!(config.port, 8000); + assert_eq!(config.log_level, LoggingLevel::Normal); + assert_eq!(config.env, config::Environment::Development); + 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.log_level, LoggingLevel::Normal); + assert_eq!(config.env, config::Environment::Staging); + 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.log_level, LoggingLevel::Critical); + assert_eq!(config.env, config::Environment::Production); + assert_eq!(config.extras().count(), 0); + } + } + + // Rocket `take`s the key, so this should always be `None` + assert_eq!(config.take_session_key(), None); +} + +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())); +} diff --git a/examples/config/tests/development.rs b/examples/config/tests/development.rs new file mode 100644 index 00000000..df35d228 --- /dev/null +++ b/examples/config/tests/development.rs @@ -0,0 +1,13 @@ +#![feature(plugin)] +#![plugin(rocket_codegen)] + +extern crate rocket; +use rocket::config::Environment; + +mod common; + +#[test] +fn test() { + common::test_config(Environment::Development); + common::test_hello(); +} diff --git a/examples/config/tests/production.rs b/examples/config/tests/production.rs new file mode 100644 index 00000000..08397303 --- /dev/null +++ b/examples/config/tests/production.rs @@ -0,0 +1,13 @@ +#![feature(plugin)] +#![plugin(rocket_codegen)] + +extern crate rocket; +use rocket::config::Environment; + +mod common; + +#[test] +fn test() { + common::test_config(Environment::Production); + common::test_hello(); +} diff --git a/examples/config/tests/staging.rs b/examples/config/tests/staging.rs new file mode 100644 index 00000000..fcb1c519 --- /dev/null +++ b/examples/config/tests/staging.rs @@ -0,0 +1,13 @@ +#![feature(plugin)] +#![plugin(rocket_codegen)] + +extern crate rocket; +use rocket::config::Environment; + +mod common; + +#[test] +fn test() { + common::test_config(Environment::Staging); + common::test_hello(); +}