Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Update library to be compatible with gotham 0.3 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ license = "MIT OR Apache-2.0"

[dependencies]
futures = "0.1"
gotham = "0.2"
gotham_derive = "0.2"
hyper = "0.11"
gotham = { path = "../gotham/gotham" }
gotham_derive = { path = "../gotham/gotham_derive" }
http = "0.1"
hyper = "0.12"
unicase = "2.1"

[dev-dependencies]
Expand Down
69 changes: 44 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ extern crate gotham_derive;

extern crate futures;
extern crate gotham;
extern crate http;
extern crate hyper;
extern crate unicase;

use futures::Future;
use gotham::handler::HandlerFuture;
use gotham::middleware::Middleware;
use gotham::state::{FromState, State};
use http::header::{
ACCESS_CONTROL_ALLOW_CREDENTIALS, ACCESS_CONTROL_ALLOW_HEADERS,
};
use hyper::header::{
AccessControlAllowCredentials, AccessControlAllowHeaders, AccessControlAllowMethods,
AccessControlAllowMethods,
AccessControlAllowOrigin, AccessControlMaxAge, Headers, Origin,
};
use hyper::Method;
use http::method::Method;
use std::option::Option;
use unicase::Ascii;

Expand Down Expand Up @@ -83,7 +87,12 @@ impl CORSMiddleware {
/// use hyper::Method;
///
/// fn create_custom_middleware() -> CORSMiddleware {
/// let methods = vec![Method::Delete, Method::Get, Method::Head, Method::Options];
/// let methods = vec![
/// Method::DELETE,
/// Method::GET,
/// Method::HEAD,
/// Method::OPTIONS,
/// ];
///
/// let max_age = 1000;
///
Expand Down Expand Up @@ -120,13 +129,13 @@ impl CORSMiddleware {
/// values, use the new() function.
pub fn default() -> CORSMiddleware {
let methods = vec![
Method::Delete,
Method::Get,
Method::Head,
Method::Options,
Method::Patch,
Method::Post,
Method::Put,
Method::DELETE,
Method::GET,
Method::HEAD,
Method::OPTIONS,
Method::PATCH,
Method::POST,
Method::PUT,
];

let origin = None;
Expand Down Expand Up @@ -158,8 +167,8 @@ impl Middleware for CORSMiddleware {

let mut headers = Headers::new();

headers.set(AccessControlAllowCredentials);
headers.set(AccessControlAllowHeaders(vec![
headers.set(ACCESS_CONTROL_ALLOW_CREDENTIALS);
headers.set(ACCESS_CONTROL_ALLOW_HEADERS(vec![
Ascii::new("Authorization".to_string()),
Ascii::new("Content-Type".to_string()),
]));
Expand Down Expand Up @@ -191,15 +200,15 @@ mod tests {
use gotham::test::TestServer;
use hyper::Method::Options;
use hyper::StatusCode;
use hyper::{Get, Head};
use hyper::{GET, HEAD};

// Since we cannot construct 'State' ourselves, we need to test via an 'actual' app
fn handler(state: State) -> Box<HandlerFuture> {
let body = "Hello World".to_string();

let response = create_response(
&state,
StatusCode::Ok,
StatusCode::OK,
Some((body.into_bytes(), mime::TEXT_PLAIN)),
);

Expand All @@ -216,7 +225,12 @@ mod tests {
}

fn custom_router() -> Router {
let methods = vec![Method::Delete, Method::Get, Method::Head, Method::Options];
let methods = vec![
Method::DELETE,
Method::GET,
Method::HEAD,
Method::OPTIONS,
];

let max_age = 1000;

Expand All @@ -243,7 +257,7 @@ mod tests {
.perform()
.unwrap();

assert_eq!(response.status(), StatusCode::Ok);
assert_eq!(response.status(), StatusCode::OK);
let headers = response.headers();
assert_eq!(
headers
Expand All @@ -268,7 +282,7 @@ mod tests {
.perform()
.unwrap();

assert_eq!(response.status(), StatusCode::Ok);
assert_eq!(response.status(), StatusCode::OK);
let headers = response.headers();
assert_eq!(
headers
Expand All @@ -285,7 +299,12 @@ mod tests {

#[test]
fn test_new_cors_middleware() {
let methods = vec![Method::Delete, Method::Get, Method::Head, Method::Options];
let methods = vec![
Method::DELETE,
Method::GET,
Method::HEAD,
Method::OPTIONS,
];

let max_age = 1000;

Expand All @@ -306,13 +325,13 @@ mod tests {
fn test_default_cors_middleware() {
let test = CORSMiddleware::default();
let methods = vec![
Method::Delete,
Method::Get,
Method::Head,
Method::Options,
Method::Patch,
Method::Post,
Method::Put,
Method::DELETE,
Method::GET,
Method::HEAD,
Method::OPTIONS,
Method::PATCH,
Method::POST,
Method::PUT,
];

assert_eq!(test.methods, methods);
Expand Down