Skip to content

Commit

Permalink
OPTIONS request 'Content-type' should not be json (#58)
Browse files Browse the repository at this point in the history
* OPTIONS request 'Content-type' should not be json

Because of CORS browser send OPTIONS request and failed with '415 Unsupported Media Type'.

* Add testcase and change order of statements
  • Loading branch information
Dmitry Ulanov authored and tomusdrw committed Mar 7, 2017
1 parent 366dbdf commit 3db8455
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
16 changes: 8 additions & 8 deletions minihttp/src/lib.rs
Expand Up @@ -308,14 +308,6 @@ impl<M: jsonrpc::Metadata, S: jsonrpc::Middleware<M>> tokio_service::Service for
).boxed();
}

// Validate content type
let content_type = request.header("Content-type");
if !is_json(content_type) {
return future::ok(
res::invalid_content_type()
).boxed();
}

// Extract CORS headers
let origin = request.header("Origin");
let cors = cors::get_cors_header(origin, &self.cors_domains);
Expand All @@ -327,6 +319,14 @@ impl<M: jsonrpc::Metadata, S: jsonrpc::Middleware<M>> tokio_service::Service for
).boxed();
}

// Validate content type
let content_type = request.header("Content-type");
if !is_json(content_type) {
return future::ok(
res::invalid_content_type()
).boxed();
}

// Extract metadata
let metadata = self.meta_extractor.read_metadata(&request);

Expand Down
16 changes: 16 additions & 0 deletions minihttp/src/tests.rs
Expand Up @@ -87,6 +87,22 @@ fn should_return_method_not_allowed_for_get() {
assert_eq!(response.body, "Used HTTP Method is not allowed. POST or OPTIONS is required.\n".to_owned());
}

#[test]
fn should_ignore_media_type_if_options() {
// given
let server = serve();

// when
let response = request(server,
Method::Options,
Headers::new(),
"{}",
);

// then
assert_eq!(response.status, StatusCode::Ok);
}

#[test]
fn should_return_unsupported_media_type_if_not_json() {
// given
Expand Down

0 comments on commit 3db8455

Please sign in to comment.