Add tests for config example.

This commit is contained in:
Seth Lopez 2016-12-27 17:35:14 -05:00 committed by Sergio Benitez
parent 1e2237d726
commit e650587159
8 changed files with 122 additions and 7 deletions

View File

@ -7,3 +7,6 @@ workspace = "../../"
[dependencies] [dependencies]
rocket = { path = "../../lib" } rocket = { path = "../../lib" }
rocket_codegen = { path = "../../codegen" } rocket_codegen = { path = "../../codegen" }
[dev-dependencies]
rocket = { path = "../../lib", features = ["testing"] }

View File

@ -0,0 +1,9 @@
#![feature(plugin)]
#![plugin(rocket_codegen)]
extern crate rocket;
#[get("/")]
pub fn hello() -> &'static str {
"Hello, world!"
}

View File

@ -2,14 +2,12 @@
#![plugin(rocket_codegen)] #![plugin(rocket_codegen)]
extern crate rocket; extern crate rocket;
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.
#[get("/")]
fn hello() -> &'static str {
"Hello, world!"
}
fn main() { fn main() {
rocket::ignite().mount("/hello", routes![hello]).launch() rocket::ignite().mount("/hello", routes![config::hello]).launch()
} }

View File

@ -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()));
}

View File

@ -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()));
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}