mirror of https://github.com/rwf2/Rocket.git
Add tests for config example.
This commit is contained in:
parent
1e2237d726
commit
e650587159
|
@ -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"] }
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
#![feature(plugin)]
|
||||||
|
#![plugin(rocket_codegen)]
|
||||||
|
|
||||||
|
extern crate rocket;
|
||||||
|
|
||||||
|
#[get("/")]
|
||||||
|
pub fn hello() -> &'static str {
|
||||||
|
"Hello, world!"
|
||||||
|
}
|
|
@ -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()
|
||||||
}
|
}
|
||||||
|
|
|
@ -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()));
|
||||||
|
}
|
|
@ -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()));
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
Loading…
Reference in New Issue