mirror of
https://github.com/ntex-rs/ntex-extras.git
synced 2025-04-03 21:07:40 +03:00
upgrade ntex 0.5 release
This commit is contained in:
parent
74148e3ddf
commit
6ec85aa238
10 changed files with 124 additions and 200 deletions
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "ntex-cors"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
authors = ["ntex contributors <team@ntex.rs>"]
|
||||
description = "Cross-origin resource sharing (CORS) for ntex applications."
|
||||
readme = "README.md"
|
||||
|
@ -16,6 +16,9 @@ name = "ntex_cors"
|
|||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
ntex = "0.3.18"
|
||||
derive_more = "0.99.11"
|
||||
futures = "0.3.13"
|
||||
ntex = "0.5.10"
|
||||
derive_more = "0.99"
|
||||
futures = "0.3"
|
||||
|
||||
[dev-dependencies]
|
||||
ntex = { version = "0.5", features=["tokio"] }
|
||||
|
|
|
@ -45,19 +45,19 @@
|
|||
//! endpoint.
|
||||
//!
|
||||
//! Cors middleware automatically handle *OPTIONS* preflight request.
|
||||
use std::collections::HashSet;
|
||||
use std::convert::TryFrom;
|
||||
use std::iter::FromIterator;
|
||||
use std::rc::Rc;
|
||||
use std::task::{Context, Poll};
|
||||
use std::{
|
||||
collections::HashSet, convert::TryFrom, iter::FromIterator, marker::PhantomData, rc::Rc,
|
||||
};
|
||||
|
||||
use derive_more::Display;
|
||||
use futures::future::{ok, Either, FutureExt, LocalBoxFuture, Ready};
|
||||
use ntex::http::header::{self, HeaderName, HeaderValue};
|
||||
use ntex::http::{error::HttpError, HeaderMap, Method, RequestHead, StatusCode, Uri};
|
||||
use ntex::service::{Service, Transform};
|
||||
use ntex::web::dev::{WebRequest, WebResponse};
|
||||
use ntex::web::{DefaultError, ErrorRenderer, HttpResponse, WebResponseError};
|
||||
use ntex::web::{
|
||||
DefaultError, ErrorRenderer, HttpResponse, WebRequest, WebResponse, WebResponseError,
|
||||
};
|
||||
|
||||
/// A set of errors that can occur during processing CORS
|
||||
#[derive(Debug, Display)]
|
||||
|
@ -196,7 +196,7 @@ impl Cors {
|
|||
}
|
||||
|
||||
/// Build a new CORS default middleware
|
||||
pub fn default() -> CorsFactory {
|
||||
pub fn default<Err>() -> CorsFactory<Err> {
|
||||
let inner = Inner {
|
||||
origins: AllOrSome::default(),
|
||||
origins_str: None,
|
||||
|
@ -220,7 +220,7 @@ impl Cors {
|
|||
supports_credentials: false,
|
||||
vary_header: true,
|
||||
};
|
||||
CorsFactory { inner: Rc::new(inner) }
|
||||
CorsFactory { inner: Rc::new(inner), _t: PhantomData }
|
||||
}
|
||||
|
||||
/// Add an origin that are allowed to make requests.
|
||||
|
@ -462,7 +462,7 @@ impl Cors {
|
|||
}
|
||||
|
||||
/// Construct cors middleware
|
||||
pub fn finish(self) -> CorsFactory {
|
||||
pub fn finish<Err>(self) -> CorsFactory<Err> {
|
||||
let mut slf = if !self.methods {
|
||||
self.allowed_methods(vec![
|
||||
Method::GET,
|
||||
|
@ -503,7 +503,7 @@ impl Cors {
|
|||
);
|
||||
}
|
||||
|
||||
CorsFactory { inner: Rc::new(cors) }
|
||||
CorsFactory { inner: Rc::new(cors), _t: PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -716,27 +716,20 @@ impl Inner {
|
|||
///
|
||||
/// The Cors struct contains the settings for CORS requests to be validated and
|
||||
/// for responses to be generated.
|
||||
pub struct CorsFactory {
|
||||
pub struct CorsFactory<Err> {
|
||||
inner: Rc<Inner>,
|
||||
_t: PhantomData<Err>,
|
||||
}
|
||||
|
||||
impl<S, Err> Transform<S> for CorsFactory
|
||||
impl<S, Err> Transform<S> for CorsFactory<Err>
|
||||
where
|
||||
S: Service<Request = WebRequest<Err>, Response = WebResponse>,
|
||||
S: Service<WebRequest<Err>, Response = WebResponse>,
|
||||
S::Future: 'static,
|
||||
Err: ErrorRenderer,
|
||||
Err::Container: From<S::Error>,
|
||||
CorsError: WebResponseError<Err>,
|
||||
{
|
||||
type Request = WebRequest<Err>;
|
||||
type Response = WebResponse;
|
||||
type Error = S::Error;
|
||||
type InitError = ();
|
||||
type Transform = CorsMiddleware<S>;
|
||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||
type Service = CorsMiddleware<S>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ok(CorsMiddleware { service, inner: self.inner.clone() })
|
||||
fn new_transform(&self, service: S) -> Self::Service {
|
||||
CorsMiddleware { service, inner: self.inner.clone() }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -750,15 +743,14 @@ pub struct CorsMiddleware<S> {
|
|||
inner: Rc<Inner>,
|
||||
}
|
||||
|
||||
impl<S, Err> Service for CorsMiddleware<S>
|
||||
impl<S, Err> Service<WebRequest<Err>> for CorsMiddleware<S>
|
||||
where
|
||||
S: Service<Request = WebRequest<Err>, Response = WebResponse>,
|
||||
S: Service<WebRequest<Err>, Response = WebResponse>,
|
||||
S::Future: 'static,
|
||||
Err: ErrorRenderer,
|
||||
Err::Container: From<S::Error>,
|
||||
CorsError: WebResponseError<Err>,
|
||||
{
|
||||
type Request = WebRequest<Err>;
|
||||
type Response = WebResponse;
|
||||
type Error = S::Error;
|
||||
type Future = Either<
|
||||
|
@ -803,19 +795,20 @@ where
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use ntex::service::{fn_service, Transform};
|
||||
use ntex::web::test::{self, TestRequest};
|
||||
use ntex::web::{self, test, test::TestRequest};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[ntex::test]
|
||||
#[should_panic(expected = "Credentials are allowed, but the Origin is set to")]
|
||||
async fn cors_validates_illegal_allow_credentials() {
|
||||
let _cors = Cors::new().supports_credentials().send_wildcard().finish();
|
||||
let _cors =
|
||||
Cors::new().supports_credentials().send_wildcard().finish::<web::DefaultError>();
|
||||
}
|
||||
|
||||
#[ntex::test]
|
||||
async fn validate_origin_allows_all_origins() {
|
||||
let cors = Cors::new().finish().new_transform(test::ok_service()).await.unwrap();
|
||||
let cors = Cors::new().finish().new_transform(test::ok_service());
|
||||
let req =
|
||||
TestRequest::with_header("Origin", "https://www.example.com").to_srv_request();
|
||||
|
||||
|
@ -825,7 +818,7 @@ mod tests {
|
|||
|
||||
#[ntex::test]
|
||||
async fn default() {
|
||||
let cors = Cors::default().new_transform(test::ok_service()).await.unwrap();
|
||||
let cors = Cors::default().new_transform(test::ok_service());
|
||||
let req =
|
||||
TestRequest::with_header("Origin", "https://www.example.com").to_srv_request();
|
||||
|
||||
|
@ -842,9 +835,7 @@ mod tests {
|
|||
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
|
||||
.allowed_header(header::CONTENT_TYPE)
|
||||
.finish()
|
||||
.new_transform(test::ok_service())
|
||||
.await
|
||||
.unwrap();
|
||||
.new_transform(test::ok_service());
|
||||
|
||||
let req = TestRequest::with_header("Origin", "https://www.example.com")
|
||||
.method(Method::OPTIONS)
|
||||
|
@ -923,9 +914,7 @@ mod tests {
|
|||
let cors = Cors::new()
|
||||
.allowed_origin("https://www.example.com")
|
||||
.finish()
|
||||
.new_transform(test::ok_service())
|
||||
.await
|
||||
.unwrap();
|
||||
.new_transform(test::ok_service::<web::DefaultError>());
|
||||
|
||||
let req = TestRequest::with_header("Origin", "https://www.unknown.com")
|
||||
.method(Method::GET)
|
||||
|
@ -940,9 +929,7 @@ mod tests {
|
|||
let cors = Cors::new()
|
||||
.allowed_origin("https://www.example.com")
|
||||
.finish()
|
||||
.new_transform(test::ok_service())
|
||||
.await
|
||||
.unwrap();
|
||||
.new_transform(test::ok_service());
|
||||
|
||||
let req = TestRequest::with_header("Origin", "https://www.example.com")
|
||||
.method(Method::GET)
|
||||
|
@ -954,12 +941,7 @@ mod tests {
|
|||
|
||||
#[ntex::test]
|
||||
async fn test_no_origin_response() {
|
||||
let cors = Cors::new()
|
||||
.disable_preflight()
|
||||
.finish()
|
||||
.new_transform(test::ok_service())
|
||||
.await
|
||||
.unwrap();
|
||||
let cors = Cors::new().disable_preflight().finish().new_transform(test::ok_service());
|
||||
|
||||
let req = TestRequest::default().method(Method::GET).to_srv_request();
|
||||
let resp = test::call_service(&cors, req).await;
|
||||
|
@ -987,9 +969,7 @@ mod tests {
|
|||
.expose_headers(exposed_headers.clone())
|
||||
.allowed_header(header::CONTENT_TYPE)
|
||||
.finish()
|
||||
.new_transform(test::ok_service())
|
||||
.await
|
||||
.unwrap();
|
||||
.new_transform(test::ok_service());
|
||||
|
||||
let req = TestRequest::with_header("Origin", "https://www.example.com")
|
||||
.method(Method::OPTIONS)
|
||||
|
@ -1033,9 +1013,7 @@ mod tests {
|
|||
ok::<_, std::convert::Infallible>(req.into_response(
|
||||
HttpResponse::Ok().header(header::VARY, "Accept").finish(),
|
||||
))
|
||||
}))
|
||||
.await
|
||||
.unwrap();
|
||||
}));
|
||||
let req = TestRequest::with_header("Origin", "https://www.example.com")
|
||||
.method(Method::OPTIONS)
|
||||
.to_srv_request();
|
||||
|
@ -1050,9 +1028,7 @@ mod tests {
|
|||
.allowed_origin("https://www.example.com")
|
||||
.allowed_origin("https://www.google.com")
|
||||
.finish()
|
||||
.new_transform(test::ok_service())
|
||||
.await
|
||||
.unwrap();
|
||||
.new_transform(test::ok_service());
|
||||
|
||||
let req = TestRequest::with_header("Origin", "https://www.example.com")
|
||||
.method(Method::OPTIONS)
|
||||
|
@ -1073,9 +1049,7 @@ mod tests {
|
|||
.allowed_origin("https://example.org")
|
||||
.allowed_methods(vec![Method::GET])
|
||||
.finish()
|
||||
.new_transform(test::ok_service())
|
||||
.await
|
||||
.unwrap();
|
||||
.new_transform(test::ok_service());
|
||||
|
||||
let req = TestRequest::with_header("Origin", "https://example.com")
|
||||
.method(Method::GET)
|
||||
|
@ -1105,9 +1079,7 @@ mod tests {
|
|||
.allowed_origin("https://example.org")
|
||||
.allowed_methods(vec![Method::GET])
|
||||
.finish()
|
||||
.new_transform(test::ok_service())
|
||||
.await
|
||||
.unwrap();
|
||||
.new_transform(test::ok_service());
|
||||
|
||||
let req = TestRequest::with_header("Origin", "https://example.com")
|
||||
.header(header::ACCESS_CONTROL_REQUEST_METHOD, "GET")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue