Clean up config example.

This commit is contained in:
Sergio Benitez 2016-12-28 18:24:54 -06:00
parent e650587159
commit 55a2535896
6 changed files with 12 additions and 30 deletions

View File

@ -4,9 +4,6 @@
extern crate rocket; extern crate rocket;
extern crate config; extern crate config;
#[cfg(test)]
mod tests;
// This example's illustration is the Rocket.toml file. // This example's illustration is the Rocket.toml file.
fn main() { fn main() {
rocket::ignite().mount("/hello", routes![config::hello]).launch() rocket::ignite().mount("/hello", routes![config::hello]).launch()

View File

@ -1,13 +0,0 @@
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()));
}

View File

@ -1,17 +1,18 @@
extern crate rocket; extern crate rocket;
extern crate config as lib; extern crate config as lib;
use std;
use rocket::config::{self, Environment}; use rocket::config::{self, Environment};
use rocket::http::Method; use rocket::http::Method;
use rocket::LoggingLevel; use rocket::LoggingLevel;
use rocket::testing::MockRequest; use rocket::testing::MockRequest;
pub fn test_config(environment: Environment) { pub fn test_config(environment: Environment) {
// Manually set the config environment variable so that Rocket initializes it in `init()`. // Manually set the config environment variable. Rocket will initialize the
std::env::set_var("ROCKET_ENV", environment.to_string()); // environment in `ignite()`.
::std::env::set_var("ROCKET_ENV", environment.to_string());
rocket::ignite().mount("/hello", routes![lib::hello]); rocket::ignite().mount("/hello", routes![lib::hello]);
// Get the active environment and ensure that it matches our expectations.
let config = config::active().unwrap(); let config = config::active().unwrap();
match environment { match environment {
Environment::Development => { Environment::Development => {
@ -39,7 +40,7 @@ pub fn test_config(environment: Environment) {
} }
} }
// Rocket `take`s the key, so this should always be `None` // Rocket `take`s the key, so this should always be `None`.
assert_eq!(config.take_session_key(), None); assert_eq!(config.take_session_key(), None);
} }

View File

@ -2,12 +2,11 @@
#![plugin(rocket_codegen)] #![plugin(rocket_codegen)]
extern crate rocket; extern crate rocket;
use rocket::config::Environment;
mod common; mod common;
#[test] #[test]
fn test() { fn test_development_config() {
common::test_config(Environment::Development); common::test_config(rocket::config::Environment::Development);
common::test_hello(); common::test_hello();
} }

View File

@ -2,12 +2,11 @@
#![plugin(rocket_codegen)] #![plugin(rocket_codegen)]
extern crate rocket; extern crate rocket;
use rocket::config::Environment;
mod common; mod common;
#[test] #[test]
fn test() { fn test_production_config() {
common::test_config(Environment::Production); common::test_config(rocket::config::Environment::Production);
common::test_hello(); common::test_hello();
} }

View File

@ -2,12 +2,11 @@
#![plugin(rocket_codegen)] #![plugin(rocket_codegen)]
extern crate rocket; extern crate rocket;
use rocket::config::Environment;
mod common; mod common;
#[test] #[test]
fn test() { fn test_staging_config() {
common::test_config(Environment::Staging); common::test_config(rocket::config::Environment::Staging);
common::test_hello(); common::test_hello();
} }