mirror of https://github.com/fafhrd91/actix-web
Merge branch 'master' into service-request-mut
This commit is contained in:
commit
d5c9f87366
|
@ -1,10 +1,17 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
### Added
|
||||||
|
* Add extractors for `Uri` and `Method`. [#2263]
|
||||||
|
* Add extractor for `ConnectionInfo` and `PeerAddr`. [#2263]
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
* Change compression algorithm features flags. [#2250]
|
* Change compression algorithm features flags. [#2250]
|
||||||
|
* Deprecate `App::data` and `App::data_factory`. [#2271]
|
||||||
|
|
||||||
[#2250]: https://github.com/actix/actix-web/pull/2250
|
[#2250]: https://github.com/actix/actix-web/pull/2250
|
||||||
|
[#2271]: https://github.com/actix/actix-web/pull/2271
|
||||||
|
[#2263]: https://github.com/actix/actix-web/pull/2263
|
||||||
|
|
||||||
|
|
||||||
## 4.0.0-beta.7 - 2021-06-17
|
## 4.0.0-beta.7 - 2021-06-17
|
||||||
|
|
|
@ -103,7 +103,7 @@ time = { version = "0.2.23", default-features = false, features = ["std"] }
|
||||||
url = "2.1"
|
url = "2.1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-test = { version = "0.1.0-beta.2", features = ["openssl", "rustls"] }
|
actix-test = { version = "0.1.0-beta.3", features = ["openssl", "rustls"] }
|
||||||
awc = { version = "3.0.0-beta.6", features = ["openssl"] }
|
awc = { version = "3.0.0-beta.6", features = ["openssl"] }
|
||||||
|
|
||||||
brotli2 = "0.3.2"
|
brotli2 = "0.3.2"
|
||||||
|
|
|
@ -36,4 +36,4 @@ percent-encoding = "2.1"
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "2.2"
|
actix-rt = "2.2"
|
||||||
actix-web = "4.0.0-beta.7"
|
actix-web = "4.0.0-beta.7"
|
||||||
actix-test = "0.1.0-beta.2"
|
actix-test = "0.1.0-beta.3"
|
||||||
|
|
|
@ -3,6 +3,10 @@
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
|
||||||
|
|
||||||
|
## 0.1.0-beta.3 - 2021-06-20
|
||||||
|
* No significant changes from `0.1.0-beta.2`.
|
||||||
|
|
||||||
|
|
||||||
## 0.1.0-beta.2 - 2021-04-17
|
## 0.1.0-beta.2 - 2021-04-17
|
||||||
* No significant changes from `0.1.0-beta.1`.
|
* No significant changes from `0.1.0-beta.1`.
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "actix-test"
|
name = "actix-test"
|
||||||
version = "0.1.0-beta.2"
|
version = "0.1.0-beta.3"
|
||||||
authors = [
|
authors = [
|
||||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||||
"Rob Ede <robjtede@icloud.com>",
|
"Rob Ede <robjtede@icloud.com>",
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
* Update `actix` to `0.12`. [#2277]
|
||||||
|
|
||||||
|
[#2277]: https://github.com/actix/actix-web/pull/2277
|
||||||
|
|
||||||
|
|
||||||
## 4.0.0-beta.5 - 2021-06-17
|
## 4.0.0-beta.5 - 2021-06-17
|
||||||
|
|
|
@ -16,7 +16,7 @@ name = "actix_web_actors"
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix = { version = "0.11.0-beta.3", default-features = false }
|
actix = { version = "0.12.0", default-features = false }
|
||||||
actix-codec = "0.4.0"
|
actix-codec = "0.4.0"
|
||||||
actix-http = "3.0.0-beta.7"
|
actix-http = "3.0.0-beta.7"
|
||||||
actix-web = { version = "4.0.0-beta.7", default-features = false }
|
actix-web = { version = "4.0.0-beta.7", default-features = false }
|
||||||
|
@ -29,7 +29,7 @@ tokio = { version = "1", features = ["sync"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "2.2"
|
actix-rt = "2.2"
|
||||||
actix-test = "0.1.0-beta.2"
|
actix-test = "0.1.0-beta.3"
|
||||||
|
|
||||||
awc = { version = "3.0.0-beta.6", default-features = false }
|
awc = { version = "3.0.0-beta.6", default-features = false }
|
||||||
env_logger = "0.8"
|
env_logger = "0.8"
|
||||||
|
|
|
@ -20,7 +20,7 @@ proc-macro2 = "1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "2.2"
|
actix-rt = "2.2"
|
||||||
actix-test = "0.1.0-beta.2"
|
actix-test = "0.1.0-beta.3"
|
||||||
actix-utils = "3.0.0"
|
actix-utils = "3.0.0"
|
||||||
actix-web = "4.0.0-beta.7"
|
actix-web = "4.0.0-beta.7"
|
||||||
|
|
||||||
|
|
|
@ -83,7 +83,7 @@ actix-http-test = { version = "3.0.0-beta.4", features = ["openssl"] }
|
||||||
actix-utils = "3.0.0"
|
actix-utils = "3.0.0"
|
||||||
actix-server = "2.0.0-beta.3"
|
actix-server = "2.0.0-beta.3"
|
||||||
actix-tls = { version = "3.0.0-beta.5", features = ["openssl", "rustls"] }
|
actix-tls = { version = "3.0.0-beta.5", features = ["openssl", "rustls"] }
|
||||||
actix-test = { version = "0.1.0-beta.2", features = ["openssl", "rustls"] }
|
actix-test = { version = "0.1.0-beta.3", features = ["openssl", "rustls"] }
|
||||||
|
|
||||||
brotli2 = "0.3.2"
|
brotli2 = "0.3.2"
|
||||||
env_logger = "0.8"
|
env_logger = "0.8"
|
||||||
|
|
15
src/app.rs
15
src/app.rs
|
@ -98,13 +98,18 @@ where
|
||||||
/// web::resource("/index.html").route(
|
/// web::resource("/index.html").route(
|
||||||
/// web::get().to(index)));
|
/// web::get().to(index)));
|
||||||
/// ```
|
/// ```
|
||||||
|
#[deprecated(since = "4.0.0", note = "Use `.app_data(Data::new(val))` instead.")]
|
||||||
pub fn data<U: 'static>(self, data: U) -> Self {
|
pub fn data<U: 'static>(self, data: U) -> Self {
|
||||||
self.app_data(Data::new(data))
|
self.app_data(Data::new(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set application data factory. This function is
|
/// Add application data factory. This function is similar to `.data()` but it accepts a
|
||||||
/// similar to `.data()` but it accepts data factory. Data object get
|
/// "data factory". Data values are constructed asynchronously during application
|
||||||
/// constructed asynchronously during application initialization.
|
/// initialization, before the server starts accepting requests.
|
||||||
|
#[deprecated(
|
||||||
|
since = "4.0.0",
|
||||||
|
note = "Construct data value before starting server and use `.app_data(Data::new(val))` instead."
|
||||||
|
)]
|
||||||
pub fn data_factory<F, Out, D, E>(mut self, data: F) -> Self
|
pub fn data_factory<F, Out, D, E>(mut self, data: F) -> Self
|
||||||
where
|
where
|
||||||
F: Fn() -> Out + 'static,
|
F: Fn() -> Out + 'static,
|
||||||
|
@ -518,6 +523,8 @@ mod tests {
|
||||||
assert_eq!(resp.status(), StatusCode::CREATED);
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_data_factory() {
|
async fn test_data_factory() {
|
||||||
let srv = init_service(
|
let srv = init_service(
|
||||||
|
@ -541,6 +548,8 @@ mod tests {
|
||||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_data_factory_errors() {
|
async fn test_data_factory_errors() {
|
||||||
let srv = try_init_service(
|
let srv = try_init_service(
|
||||||
|
|
|
@ -349,6 +349,8 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_drop_data() {
|
async fn test_drop_data() {
|
||||||
let data = Arc::new(AtomicBool::new(false));
|
let data = Arc::new(AtomicBool::new(false));
|
||||||
|
|
|
@ -116,6 +116,7 @@ impl AppConfig {
|
||||||
AppConfig { secure, host, addr }
|
AppConfig { secure, host, addr }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Needed in actix-test crate.
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn __priv_test_new(secure: bool, host: String, addr: SocketAddr) -> Self {
|
pub fn __priv_test_new(secure: bool, host: String, addr: SocketAddr) -> Self {
|
||||||
AppConfig::new(secure, host, addr)
|
AppConfig::new(secure, host, addr)
|
||||||
|
|
|
@ -154,6 +154,8 @@ mod tests {
|
||||||
web, App, HttpResponse,
|
web, App, HttpResponse,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_data_extractor() {
|
async fn test_data_extractor() {
|
||||||
let srv = init_service(App::new().data("TEST".to_string()).service(
|
let srv = init_service(App::new().data("TEST".to_string()).service(
|
||||||
|
@ -221,6 +223,8 @@ mod tests {
|
||||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_route_data_extractor() {
|
async fn test_route_data_extractor() {
|
||||||
let srv = init_service(
|
let srv = init_service(
|
||||||
|
@ -250,6 +254,8 @@ mod tests {
|
||||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_override_data() {
|
async fn test_override_data() {
|
||||||
let srv =
|
let srv =
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
//! Request extractors
|
//! Request extractors
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
convert::Infallible,
|
||||||
future::Future,
|
future::Future,
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
};
|
};
|
||||||
|
|
||||||
use actix_utils::future::{ready, Ready};
|
use actix_http::http::{Method, Uri};
|
||||||
|
use actix_utils::future::{ok, Ready};
|
||||||
use futures_core::ready;
|
use futures_core::ready;
|
||||||
|
|
||||||
use crate::{dev::Payload, Error, HttpRequest};
|
use crate::{dev::Payload, Error, HttpRequest};
|
||||||
|
@ -216,14 +218,58 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Extract the request's URI.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
/// ```
|
||||||
|
/// use actix_web::{http::Uri, web, App, Responder};
|
||||||
|
///
|
||||||
|
/// async fn handler(uri: Uri) -> impl Responder {
|
||||||
|
/// format!("Requested path: {}", uri.path())
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// let app = App::new().default_service(web::to(handler));
|
||||||
|
/// ```
|
||||||
|
impl FromRequest for Uri {
|
||||||
|
type Error = Infallible;
|
||||||
|
type Future = Ready<Result<Self, Self::Error>>;
|
||||||
|
type Config = ();
|
||||||
|
|
||||||
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
|
ok(req.uri().clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extract the request's method.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
/// ```
|
||||||
|
/// use actix_web::{http::Method, web, App, Responder};
|
||||||
|
///
|
||||||
|
/// async fn handler(method: Method) -> impl Responder {
|
||||||
|
/// format!("Request method: {}", method)
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// let app = App::new().default_service(web::to(handler));
|
||||||
|
/// ```
|
||||||
|
impl FromRequest for Method {
|
||||||
|
type Error = Infallible;
|
||||||
|
type Future = Ready<Result<Self, Self::Error>>;
|
||||||
|
type Config = ();
|
||||||
|
|
||||||
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
|
ok(req.method().clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
impl FromRequest for () {
|
impl FromRequest for () {
|
||||||
type Error = Error;
|
type Error = Infallible;
|
||||||
type Future = Ready<Result<(), Error>>;
|
type Future = Ready<Result<(), Infallible>>;
|
||||||
type Config = ();
|
type Config = ();
|
||||||
|
|
||||||
fn from_request(_: &HttpRequest, _: &mut Payload) -> Self::Future {
|
fn from_request(_: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
ready(Ok(()))
|
ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -411,4 +457,18 @@ mod tests {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(r.is_err());
|
assert!(r.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_uri() {
|
||||||
|
let req = TestRequest::default().uri("/foo/bar").to_http_request();
|
||||||
|
let uri = Uri::extract(&req).await.unwrap();
|
||||||
|
assert_eq!(uri.path(), "/foo/bar");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_method() {
|
||||||
|
let req = TestRequest::default().method(Method::GET).to_http_request();
|
||||||
|
let method = Method::extract(&req).await.unwrap();
|
||||||
|
assert_eq!(method, Method::GET);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
111
src/info.rs
111
src/info.rs
|
@ -1,13 +1,36 @@
|
||||||
use std::cell::Ref;
|
use std::{cell::Ref, convert::Infallible, net::SocketAddr};
|
||||||
|
|
||||||
use crate::dev::{AppConfig, RequestHead};
|
use actix_utils::future::{err, ok, Ready};
|
||||||
use crate::http::header::{self, HeaderName};
|
use derive_more::{Display, Error};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
dev::{AppConfig, Payload, RequestHead},
|
||||||
|
http::header::{self, HeaderName},
|
||||||
|
FromRequest, HttpRequest, ResponseError,
|
||||||
|
};
|
||||||
|
|
||||||
const X_FORWARDED_FOR: &[u8] = b"x-forwarded-for";
|
const X_FORWARDED_FOR: &[u8] = b"x-forwarded-for";
|
||||||
const X_FORWARDED_HOST: &[u8] = b"x-forwarded-host";
|
const X_FORWARDED_HOST: &[u8] = b"x-forwarded-host";
|
||||||
const X_FORWARDED_PROTO: &[u8] = b"x-forwarded-proto";
|
const X_FORWARDED_PROTO: &[u8] = b"x-forwarded-proto";
|
||||||
|
|
||||||
/// `HttpRequest` connection information
|
/// HTTP connection information.
|
||||||
|
///
|
||||||
|
/// `ConnectionInfo` implements `FromRequest` and can be extracted in handlers.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
/// ```
|
||||||
|
/// # use actix_web::{HttpResponse, Responder};
|
||||||
|
/// use actix_web::dev::ConnectionInfo;
|
||||||
|
///
|
||||||
|
/// async fn handler(conn: ConnectionInfo) -> impl Responder {
|
||||||
|
/// match conn.host() {
|
||||||
|
/// "actix.rs" => HttpResponse::Ok().body("Welcome!"),
|
||||||
|
/// "admin.actix.rs" => HttpResponse::Ok().body("Admin portal."),
|
||||||
|
/// _ => HttpResponse::NotFound().finish()
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// # let _svc = actix_web::web::to(handler);
|
||||||
|
/// ```
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct ConnectionInfo {
|
pub struct ConnectionInfo {
|
||||||
scheme: String,
|
scheme: String,
|
||||||
|
@ -187,6 +210,65 @@ impl ConnectionInfo {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromRequest for ConnectionInfo {
|
||||||
|
type Error = Infallible;
|
||||||
|
type Future = Ready<Result<Self, Self::Error>>;
|
||||||
|
type Config = ();
|
||||||
|
|
||||||
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
|
ok(req.connection_info().clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extractor for peer's socket address.
|
||||||
|
///
|
||||||
|
/// Also see [`HttpRequest::peer_addr`].
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
/// ```
|
||||||
|
/// # use actix_web::Responder;
|
||||||
|
/// use actix_web::dev::PeerAddr;
|
||||||
|
///
|
||||||
|
/// async fn handler(peer_addr: PeerAddr) -> impl Responder {
|
||||||
|
/// let socket_addr = peer_addr.0;
|
||||||
|
/// socket_addr.to_string()
|
||||||
|
/// }
|
||||||
|
/// # let _svc = actix_web::web::to(handler);
|
||||||
|
/// ```
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Display)]
|
||||||
|
#[display(fmt = "{}", _0)]
|
||||||
|
pub struct PeerAddr(pub SocketAddr);
|
||||||
|
|
||||||
|
impl PeerAddr {
|
||||||
|
/// Unwrap into inner `SocketAddr` value.
|
||||||
|
pub fn into_inner(self) -> SocketAddr {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Display, Error)]
|
||||||
|
#[non_exhaustive]
|
||||||
|
#[display(fmt = "Missing peer address")]
|
||||||
|
pub struct MissingPeerAddr;
|
||||||
|
|
||||||
|
impl ResponseError for MissingPeerAddr {}
|
||||||
|
|
||||||
|
impl FromRequest for PeerAddr {
|
||||||
|
type Error = MissingPeerAddr;
|
||||||
|
type Future = Ready<Result<Self, Self::Error>>;
|
||||||
|
type Config = ();
|
||||||
|
|
||||||
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
|
match req.peer_addr() {
|
||||||
|
Some(addr) => ok(PeerAddr(addr)),
|
||||||
|
None => {
|
||||||
|
log::error!("Missing peer address.");
|
||||||
|
err(MissingPeerAddr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -239,4 +321,25 @@ mod tests {
|
||||||
let info = req.connection_info();
|
let info = req.connection_info();
|
||||||
assert_eq!(info.scheme(), "https");
|
assert_eq!(info.scheme(), "https");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_conn_info() {
|
||||||
|
let req = TestRequest::default()
|
||||||
|
.uri("http://actix.rs/")
|
||||||
|
.to_http_request();
|
||||||
|
let conn_info = ConnectionInfo::extract(&req).await.unwrap();
|
||||||
|
assert_eq!(conn_info.scheme(), "http");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_peer_addr() {
|
||||||
|
let addr = "127.0.0.1:8080".parse().unwrap();
|
||||||
|
let req = TestRequest::default().peer_addr(addr).to_http_request();
|
||||||
|
let peer_addr = PeerAddr::extract(&req).await.unwrap();
|
||||||
|
assert_eq!(peer_addr, PeerAddr(addr));
|
||||||
|
|
||||||
|
let req = TestRequest::default().to_http_request();
|
||||||
|
let res = PeerAddr::extract(&req).await;
|
||||||
|
assert!(res.is_err());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -131,7 +131,7 @@ pub mod dev {
|
||||||
pub use crate::config::{AppConfig, AppService};
|
pub use crate::config::{AppConfig, AppService};
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub use crate::handler::Handler;
|
pub use crate::handler::Handler;
|
||||||
pub use crate::info::ConnectionInfo;
|
pub use crate::info::{ConnectionInfo, PeerAddr};
|
||||||
pub use crate::rmap::ResourceMap;
|
pub use crate::rmap::ResourceMap;
|
||||||
pub use crate::service::{HttpServiceFactory, ServiceRequest, ServiceResponse, WebService};
|
pub use crate::service::{HttpServiceFactory, ServiceRequest, ServiceResponse, WebService};
|
||||||
|
|
||||||
|
|
|
@ -60,18 +60,6 @@ impl HttpRequest {
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub fn __priv_test_new(
|
|
||||||
path: Path<Url>,
|
|
||||||
head: Message<RequestHead>,
|
|
||||||
rmap: Rc<ResourceMap>,
|
|
||||||
config: AppConfig,
|
|
||||||
app_data: Rc<Extensions>,
|
|
||||||
) -> HttpRequest {
|
|
||||||
let app_state = AppInitServiceState::new(rmap, config);
|
|
||||||
Self::new(path, head, app_state, app_data)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HttpRequest {
|
impl HttpRequest {
|
||||||
|
@ -723,6 +711,8 @@ mod tests {
|
||||||
assert_eq!(body, Bytes::from_static(b"1"));
|
assert_eq!(body, Bytes::from_static(b"1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_extensions_dropped() {
|
async fn test_extensions_dropped() {
|
||||||
struct Tracker {
|
struct Tracker {
|
||||||
|
|
|
@ -196,6 +196,7 @@ where
|
||||||
/// ));
|
/// ));
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
#[deprecated(since = "4.0.0", note = "Use `.app_data(Data::new(val))` instead.")]
|
||||||
pub fn data<U: 'static>(self, data: U) -> Self {
|
pub fn data<U: 'static>(self, data: U) -> Self {
|
||||||
self.app_data(Data::new(data))
|
self.app_data(Data::new(data))
|
||||||
}
|
}
|
||||||
|
@ -694,6 +695,8 @@ mod tests {
|
||||||
assert_eq!(resp.status(), StatusCode::NO_CONTENT);
|
assert_eq!(resp.status(), StatusCode::NO_CONTENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_data() {
|
async fn test_data() {
|
||||||
let srv = init_service(
|
let srv = init_service(
|
||||||
|
@ -726,6 +729,8 @@ mod tests {
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_data_default_service() {
|
async fn test_data_default_service() {
|
||||||
let srv = init_service(
|
let srv = init_service(
|
||||||
|
|
|
@ -49,7 +49,10 @@ impl HttpResponse<AnyBody> {
|
||||||
/// Create an error response.
|
/// Create an error response.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn from_error(error: impl Into<Error>) -> Self {
|
pub fn from_error(error: impl Into<Error>) -> Self {
|
||||||
error.into().as_response_error().error_response()
|
let error = error.into();
|
||||||
|
let mut response = error.as_response_error().error_response();
|
||||||
|
response.error = Some(error);
|
||||||
|
response
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -146,6 +146,7 @@ where
|
||||||
/// );
|
/// );
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
#[deprecated(since = "4.0.0", note = "Use `.app_data(Data::new(val))` instead.")]
|
||||||
pub fn data<U: 'static>(self, data: U) -> Self {
|
pub fn data<U: 'static>(self, data: U) -> Self {
|
||||||
self.app_data(Data::new(data))
|
self.app_data(Data::new(data))
|
||||||
}
|
}
|
||||||
|
@ -990,6 +991,8 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_override_data() {
|
async fn test_override_data() {
|
||||||
let srv = init_service(App::new().data(1usize).service(
|
let srv = init_service(App::new().data(1usize).service(
|
||||||
|
@ -1008,6 +1011,8 @@ mod tests {
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_override_data_default_service() {
|
async fn test_override_data_default_service() {
|
||||||
let srv = init_service(App::new().data(1usize).service(
|
let srv = init_service(App::new().data(1usize).service(
|
||||||
|
|
|
@ -74,12 +74,6 @@ impl ServiceRequest {
|
||||||
Self { req, payload }
|
Self { req, payload }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct service request.
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub fn __priv_test_new(req: HttpRequest, payload: Payload) -> Self {
|
|
||||||
Self::new(req, payload)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Deconstruct request into parts
|
/// Deconstruct request into parts
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn into_parts(self) -> (HttpRequest, Payload) {
|
pub fn into_parts(self) -> (HttpRequest, Payload) {
|
||||||
|
@ -661,6 +655,8 @@ mod tests {
|
||||||
assert_eq!(resp.status(), http::StatusCode::NOT_FOUND);
|
assert_eq!(resp.status(), http::StatusCode::NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_service_data() {
|
async fn test_service_data() {
|
||||||
let srv =
|
let srv =
|
||||||
|
|
|
@ -839,6 +839,8 @@ mod tests {
|
||||||
assert!(res.status().is_success());
|
assert!(res.status().is_success());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_server_data() {
|
async fn test_server_data() {
|
||||||
async fn handler(data: web::Data<usize>) -> impl Responder {
|
async fn handler(data: web::Data<usize>) -> impl Responder {
|
||||||
|
|
|
@ -398,6 +398,8 @@ mod tests {
|
||||||
assert!(cfg.check_mimetype(&req).is_ok());
|
assert!(cfg.check_mimetype(&req).is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_config_recall_locations() {
|
async fn test_config_recall_locations() {
|
||||||
async fn bytes_handler(_: Bytes) -> impl Responder {
|
async fn bytes_handler(_: Bytes) -> impl Responder {
|
||||||
|
|
|
@ -0,0 +1,100 @@
|
||||||
|
use actix_utils::future::{ok, Ready};
|
||||||
|
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
|
||||||
|
use actix_web::test::{call_service, init_service, TestRequest};
|
||||||
|
use actix_web::{HttpResponse, ResponseError};
|
||||||
|
use futures_util::lock::Mutex;
|
||||||
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct MyError;
|
||||||
|
|
||||||
|
impl ResponseError for MyError {}
|
||||||
|
|
||||||
|
impl std::fmt::Display for MyError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "A custom error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_web::get("/test")]
|
||||||
|
async fn test() -> Result<actix_web::HttpResponse, actix_web::error::Error> {
|
||||||
|
Err(MyError)?;
|
||||||
|
Ok(HttpResponse::NoContent().finish())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct SpyMiddleware(Arc<Mutex<Option<bool>>>);
|
||||||
|
|
||||||
|
impl<S, B> Transform<S, ServiceRequest> for SpyMiddleware
|
||||||
|
where
|
||||||
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error>,
|
||||||
|
S::Future: 'static,
|
||||||
|
B: 'static,
|
||||||
|
{
|
||||||
|
type Response = ServiceResponse<B>;
|
||||||
|
type Error = actix_web::Error;
|
||||||
|
type Transform = Middleware<S>;
|
||||||
|
type InitError = ();
|
||||||
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||||
|
|
||||||
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
|
ok(Middleware {
|
||||||
|
was_error: self.0.clone(),
|
||||||
|
service,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub struct Middleware<S> {
|
||||||
|
was_error: Arc<Mutex<Option<bool>>>,
|
||||||
|
service: S,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S, B> Service<ServiceRequest> for Middleware<S>
|
||||||
|
where
|
||||||
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error>,
|
||||||
|
S::Future: 'static,
|
||||||
|
B: 'static,
|
||||||
|
{
|
||||||
|
type Response = ServiceResponse<B>;
|
||||||
|
type Error = actix_web::Error;
|
||||||
|
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
|
||||||
|
|
||||||
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
|
self.service.poll_ready(cx)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
|
let lock = self.was_error.clone();
|
||||||
|
let response_future = self.service.call(req);
|
||||||
|
Box::pin(async move {
|
||||||
|
let response = response_future.await;
|
||||||
|
if let Ok(success) = &response {
|
||||||
|
*lock.lock().await = Some(success.response().error().is_some());
|
||||||
|
}
|
||||||
|
response
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn error_cause_should_be_propagated_to_middlewares() {
|
||||||
|
let lock = Arc::new(Mutex::new(None));
|
||||||
|
let spy_middleware = SpyMiddleware(lock.clone());
|
||||||
|
|
||||||
|
let app = init_service(
|
||||||
|
actix_web::App::new()
|
||||||
|
.wrap(spy_middleware.clone())
|
||||||
|
.service(test),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
call_service(&app, TestRequest::with_uri("/test").to_request()).await;
|
||||||
|
|
||||||
|
let was_error_captured = lock.lock().await.unwrap();
|
||||||
|
assert!(was_error_captured);
|
||||||
|
}
|
|
@ -1028,6 +1028,8 @@ async fn test_normalize() {
|
||||||
assert!(response.status().is_success());
|
assert!(response.status().is_success());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_data_drop() {
|
async fn test_data_drop() {
|
||||||
use std::sync::{
|
use std::sync::{
|
||||||
|
|
Loading…
Reference in New Issue