mirror of https://github.com/fafhrd91/actix-web
update tests with new bounds
This commit is contained in:
parent
d514680631
commit
9ce94fd91a
|
@ -18,6 +18,7 @@ path = "src/lib.rs"
|
|||
|
||||
[dependencies]
|
||||
actix-web = { version = "4.0.0-beta.6", default-features = false }
|
||||
actix-http = "3.0.0-beta.6"
|
||||
actix-service = "2.0.0"
|
||||
actix-utils = "3.0.0"
|
||||
|
||||
|
|
|
@ -164,9 +164,10 @@ impl From<BytesMut> for AnyBody {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S> From<SizedStream<S>> for AnyBody
|
||||
impl<S, E> From<SizedStream<S>> for AnyBody
|
||||
where
|
||||
S: Stream<Item = Result<Bytes, Error>> + 'static,
|
||||
S: Stream<Item = Result<Bytes, E>> + 'static,
|
||||
E: Into<Box<dyn StdError>> + 'static,
|
||||
{
|
||||
fn from(s: SizedStream<S>) -> Body {
|
||||
AnyBody::from_message(s)
|
||||
|
|
|
@ -196,7 +196,7 @@ mod tests {
|
|||
pin!(val);
|
||||
assert_eq!(val.size(), BodySize::Empty);
|
||||
assert!(poll_fn(|cx| val.as_mut().poll_next(cx)).await.is_none());
|
||||
|
||||
|
||||
let mut val = Box::pin(());
|
||||
assert_eq!(val.size(), BodySize::Empty);
|
||||
assert!(poll_fn(|cx| val.as_mut().poll_next(cx)).await.is_none());
|
||||
|
|
|
@ -326,8 +326,19 @@ pub enum EncoderError<E> {
|
|||
Io(io::Error),
|
||||
}
|
||||
|
||||
impl<E: StdError> StdError for EncoderError<E> {
|
||||
impl<E: StdError + 'static> StdError for EncoderError<E> {
|
||||
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
||||
None
|
||||
match self {
|
||||
EncoderError::Body(err) => Some(err),
|
||||
EncoderError::Boxed(err) => Some(&**err),
|
||||
EncoderError::Blocking(err) => Some(err),
|
||||
EncoderError::Io(err) => Some(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: StdError + 'static> From<EncoderError<E>> for crate::Error {
|
||||
fn from(err: EncoderError<E>) -> Self {
|
||||
crate::Error::new_encoder().with_cause(err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,10 @@ use std::{error::Error as StdError, fmt, io, str::Utf8Error, string::FromUtf8Err
|
|||
use derive_more::{Display, Error, From};
|
||||
use http::{uri::InvalidUri, StatusCode};
|
||||
|
||||
use crate::{Response, body::{AnyBody, Body}, ws};
|
||||
use crate::{
|
||||
body::{AnyBody, Body},
|
||||
ws, Response,
|
||||
};
|
||||
|
||||
pub use http::Error as HttpError;
|
||||
|
||||
|
@ -46,10 +49,16 @@ impl Error {
|
|||
Self::new(Kind::SendResponse)
|
||||
}
|
||||
|
||||
// TODO: remove allow
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn new_io() -> Self {
|
||||
Self::new(Kind::Io)
|
||||
}
|
||||
|
||||
pub(crate) fn new_encoder() -> Self {
|
||||
Self::new(Kind::Encoder)
|
||||
}
|
||||
|
||||
pub(crate) fn new_ws() -> Self {
|
||||
Self::new(Kind::Ws)
|
||||
}
|
||||
|
@ -93,6 +102,9 @@ pub enum Kind {
|
|||
|
||||
#[display(fmt = "connection error")]
|
||||
Io,
|
||||
|
||||
#[display(fmt = "encoder error")]
|
||||
Encoder,
|
||||
}
|
||||
|
||||
impl fmt::Debug for Error {
|
||||
|
@ -424,7 +436,10 @@ mod tests {
|
|||
fn test_as_response() {
|
||||
let orig = io::Error::new(io::ErrorKind::Other, "other");
|
||||
let err: Error = ParseError::Io(orig).into();
|
||||
assert_eq!(format!("{}", err), "error parsing HTTP message: IO error: other");
|
||||
assert_eq!(
|
||||
format!("{}", err),
|
||||
"error parsing HTTP message: IO error: other"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -402,7 +402,10 @@ async fn test_h2_response_http_error_handling() {
|
|||
|
||||
// read response
|
||||
let bytes = srv.load_body(response).await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(b"error processing HTTP: failed to parse header value"));
|
||||
assert_eq!(
|
||||
bytes,
|
||||
Bytes::from_static(b"error processing HTTP: failed to parse header value")
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Debug, Display, Error)]
|
||||
|
|
|
@ -154,7 +154,9 @@ where
|
|||
let app_cfg =
|
||||
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
||||
|
||||
let fac = factory().into_factory().map_err(|err| err.into());
|
||||
let fac = factory()
|
||||
.into_factory()
|
||||
.map_err(|err| err.into().error_response());
|
||||
|
||||
HttpService::build()
|
||||
.client_timeout(timeout)
|
||||
|
@ -165,7 +167,9 @@ where
|
|||
let app_cfg =
|
||||
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
||||
|
||||
let fac = factory().into_factory().map_err(|err| err.into());
|
||||
let fac = factory()
|
||||
.into_factory()
|
||||
.map_err(|err| err.into().error_response());
|
||||
|
||||
HttpService::build()
|
||||
.client_timeout(timeout)
|
||||
|
@ -176,7 +180,9 @@ where
|
|||
let app_cfg =
|
||||
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
||||
|
||||
let fac = factory().into_factory().map_err(|err| err.into());
|
||||
let fac = factory()
|
||||
.into_factory()
|
||||
.map_err(|err| err.into().error_response());
|
||||
|
||||
HttpService::build()
|
||||
.client_timeout(timeout)
|
||||
|
@ -190,7 +196,9 @@ where
|
|||
let app_cfg =
|
||||
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
||||
|
||||
let fac = factory().into_factory().map_err(|err| err.into());
|
||||
let fac = factory()
|
||||
.into_factory()
|
||||
.map_err(|err| err.into().error_response());
|
||||
|
||||
HttpService::build()
|
||||
.client_timeout(timeout)
|
||||
|
@ -201,7 +209,9 @@ where
|
|||
let app_cfg =
|
||||
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
||||
|
||||
let fac = factory().into_factory().map_err(|err| err.into());
|
||||
let fac = factory()
|
||||
.into_factory()
|
||||
.map_err(|err| err.into().error_response());
|
||||
|
||||
HttpService::build()
|
||||
.client_timeout(timeout)
|
||||
|
@ -212,7 +222,9 @@ where
|
|||
let app_cfg =
|
||||
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
||||
|
||||
let fac = factory().into_factory().map_err(|err| err.into());
|
||||
let fac = factory()
|
||||
.into_factory()
|
||||
.map_err(|err| err.into().error_response());
|
||||
|
||||
HttpService::build()
|
||||
.client_timeout(timeout)
|
||||
|
@ -226,7 +238,9 @@ where
|
|||
let app_cfg =
|
||||
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
||||
|
||||
let fac = factory().into_factory().map_err(|err| err.into());
|
||||
let fac = factory()
|
||||
.into_factory()
|
||||
.map_err(|err| err.into().error_response());
|
||||
|
||||
HttpService::build()
|
||||
.client_timeout(timeout)
|
||||
|
@ -237,7 +251,9 @@ where
|
|||
let app_cfg =
|
||||
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
||||
|
||||
let fac = factory().into_factory().map_err(|err| err.into());
|
||||
let fac = factory()
|
||||
.into_factory()
|
||||
.map_err(|err| err.into().error_response());
|
||||
|
||||
HttpService::build()
|
||||
.client_timeout(timeout)
|
||||
|
@ -248,7 +264,9 @@ where
|
|||
let app_cfg =
|
||||
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
||||
|
||||
let fac = factory().into_factory().map_err(|err| err.into());
|
||||
let fac = factory()
|
||||
.into_factory()
|
||||
.map_err(|err| err.into().error_response());
|
||||
|
||||
HttpService::build()
|
||||
.client_timeout(timeout)
|
||||
|
|
|
@ -22,10 +22,11 @@ use actix_http::{
|
|||
http::HeaderValue,
|
||||
ws::{hash_key, Codec},
|
||||
};
|
||||
use actix_web::error::{Error, PayloadError};
|
||||
use actix_web::http::{header, Method, StatusCode};
|
||||
use actix_web::HttpResponseBuilder;
|
||||
use actix_web::{HttpRequest, HttpResponse};
|
||||
use actix_web::{
|
||||
error::{Error, PayloadError},
|
||||
http::{header, Method, StatusCode},
|
||||
HttpRequest, HttpResponse, HttpResponseBuilder,
|
||||
};
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use bytestring::ByteString;
|
||||
use futures_core::Stream;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use actix_http::Error;
|
||||
use std::error::Error as StdError;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> Result<(), Error> {
|
||||
async fn main() -> Result<(), Box<dyn StdError>> {
|
||||
std::env::set_var("RUST_LOG", "actix_http=trace");
|
||||
env_logger::init();
|
||||
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
use std::{any::type_name, ops::Deref, sync::Arc};
|
||||
|
||||
use actix_http::{ Extensions};
|
||||
use actix_http::Extensions;
|
||||
use actix_utils::future::{err, ok, Ready};
|
||||
use futures_core::future::LocalBoxFuture;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{
|
||||
dev::Payload,Error, error::ErrorInternalServerError, extract::FromRequest, request::HttpRequest,
|
||||
dev::Payload, error::ErrorInternalServerError, extract::FromRequest, request::HttpRequest,
|
||||
Error,
|
||||
};
|
||||
|
||||
/// Data factory.
|
||||
|
|
|
@ -171,134 +171,134 @@ error_helper!(
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use actix_http::{error::ParseError, Response};
|
||||
use actix_http::error::ParseError;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_internal_error() {
|
||||
let err = InternalError::from_response(ParseError::Method, HttpResponse::Ok().finish());
|
||||
let resp: Response<Body> = err.error_response();
|
||||
let resp: HttpResponse = err.error_response();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_helpers() {
|
||||
let res: Response<Body> = ErrorBadRequest("err").into();
|
||||
let res: HttpResponse = ErrorBadRequest("err").into();
|
||||
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
||||
|
||||
let res: Response<Body> = ErrorUnauthorized("err").into();
|
||||
let res: HttpResponse = ErrorUnauthorized("err").into();
|
||||
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
|
||||
|
||||
let res: Response<Body> = ErrorPaymentRequired("err").into();
|
||||
let res: HttpResponse = ErrorPaymentRequired("err").into();
|
||||
assert_eq!(res.status(), StatusCode::PAYMENT_REQUIRED);
|
||||
|
||||
let res: Response<Body> = ErrorForbidden("err").into();
|
||||
let res: HttpResponse = ErrorForbidden("err").into();
|
||||
assert_eq!(res.status(), StatusCode::FORBIDDEN);
|
||||
|
||||
let res: Response<Body> = ErrorNotFound("err").into();
|
||||
let res: HttpResponse = ErrorNotFound("err").into();
|
||||
assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
||||
|
||||
let res: Response<Body> = ErrorMethodNotAllowed("err").into();
|
||||
let res: HttpResponse = ErrorMethodNotAllowed("err").into();
|
||||
assert_eq!(res.status(), StatusCode::METHOD_NOT_ALLOWED);
|
||||
|
||||
let res: Response<Body> = ErrorNotAcceptable("err").into();
|
||||
let res: HttpResponse = ErrorNotAcceptable("err").into();
|
||||
assert_eq!(res.status(), StatusCode::NOT_ACCEPTABLE);
|
||||
|
||||
let res: Response<Body> = ErrorProxyAuthenticationRequired("err").into();
|
||||
let res: HttpResponse = ErrorProxyAuthenticationRequired("err").into();
|
||||
assert_eq!(res.status(), StatusCode::PROXY_AUTHENTICATION_REQUIRED);
|
||||
|
||||
let res: Response<Body> = ErrorRequestTimeout("err").into();
|
||||
let res: HttpResponse = ErrorRequestTimeout("err").into();
|
||||
assert_eq!(res.status(), StatusCode::REQUEST_TIMEOUT);
|
||||
|
||||
let res: Response<Body> = ErrorConflict("err").into();
|
||||
let res: HttpResponse = ErrorConflict("err").into();
|
||||
assert_eq!(res.status(), StatusCode::CONFLICT);
|
||||
|
||||
let res: Response<Body> = ErrorGone("err").into();
|
||||
let res: HttpResponse = ErrorGone("err").into();
|
||||
assert_eq!(res.status(), StatusCode::GONE);
|
||||
|
||||
let res: Response<Body> = ErrorLengthRequired("err").into();
|
||||
let res: HttpResponse = ErrorLengthRequired("err").into();
|
||||
assert_eq!(res.status(), StatusCode::LENGTH_REQUIRED);
|
||||
|
||||
let res: Response<Body> = ErrorPreconditionFailed("err").into();
|
||||
let res: HttpResponse = ErrorPreconditionFailed("err").into();
|
||||
assert_eq!(res.status(), StatusCode::PRECONDITION_FAILED);
|
||||
|
||||
let res: Response<Body> = ErrorPayloadTooLarge("err").into();
|
||||
let res: HttpResponse = ErrorPayloadTooLarge("err").into();
|
||||
assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
|
||||
|
||||
let res: Response<Body> = ErrorUriTooLong("err").into();
|
||||
let res: HttpResponse = ErrorUriTooLong("err").into();
|
||||
assert_eq!(res.status(), StatusCode::URI_TOO_LONG);
|
||||
|
||||
let res: Response<Body> = ErrorUnsupportedMediaType("err").into();
|
||||
let res: HttpResponse = ErrorUnsupportedMediaType("err").into();
|
||||
assert_eq!(res.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE);
|
||||
|
||||
let res: Response<Body> = ErrorRangeNotSatisfiable("err").into();
|
||||
let res: HttpResponse = ErrorRangeNotSatisfiable("err").into();
|
||||
assert_eq!(res.status(), StatusCode::RANGE_NOT_SATISFIABLE);
|
||||
|
||||
let res: Response<Body> = ErrorExpectationFailed("err").into();
|
||||
let res: HttpResponse = ErrorExpectationFailed("err").into();
|
||||
assert_eq!(res.status(), StatusCode::EXPECTATION_FAILED);
|
||||
|
||||
let res: Response<Body> = ErrorImATeapot("err").into();
|
||||
let res: HttpResponse = ErrorImATeapot("err").into();
|
||||
assert_eq!(res.status(), StatusCode::IM_A_TEAPOT);
|
||||
|
||||
let res: Response<Body> = ErrorMisdirectedRequest("err").into();
|
||||
let res: HttpResponse = ErrorMisdirectedRequest("err").into();
|
||||
assert_eq!(res.status(), StatusCode::MISDIRECTED_REQUEST);
|
||||
|
||||
let res: Response<Body> = ErrorUnprocessableEntity("err").into();
|
||||
let res: HttpResponse = ErrorUnprocessableEntity("err").into();
|
||||
assert_eq!(res.status(), StatusCode::UNPROCESSABLE_ENTITY);
|
||||
|
||||
let res: Response<Body> = ErrorLocked("err").into();
|
||||
let res: HttpResponse = ErrorLocked("err").into();
|
||||
assert_eq!(res.status(), StatusCode::LOCKED);
|
||||
|
||||
let res: Response<Body> = ErrorFailedDependency("err").into();
|
||||
let res: HttpResponse = ErrorFailedDependency("err").into();
|
||||
assert_eq!(res.status(), StatusCode::FAILED_DEPENDENCY);
|
||||
|
||||
let res: Response<Body> = ErrorUpgradeRequired("err").into();
|
||||
let res: HttpResponse = ErrorUpgradeRequired("err").into();
|
||||
assert_eq!(res.status(), StatusCode::UPGRADE_REQUIRED);
|
||||
|
||||
let res: Response<Body> = ErrorPreconditionRequired("err").into();
|
||||
let res: HttpResponse = ErrorPreconditionRequired("err").into();
|
||||
assert_eq!(res.status(), StatusCode::PRECONDITION_REQUIRED);
|
||||
|
||||
let res: Response<Body> = ErrorTooManyRequests("err").into();
|
||||
let res: HttpResponse = ErrorTooManyRequests("err").into();
|
||||
assert_eq!(res.status(), StatusCode::TOO_MANY_REQUESTS);
|
||||
|
||||
let res: Response<Body> = ErrorRequestHeaderFieldsTooLarge("err").into();
|
||||
let res: HttpResponse = ErrorRequestHeaderFieldsTooLarge("err").into();
|
||||
assert_eq!(res.status(), StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE);
|
||||
|
||||
let res: Response<Body> = ErrorUnavailableForLegalReasons("err").into();
|
||||
let res: HttpResponse = ErrorUnavailableForLegalReasons("err").into();
|
||||
assert_eq!(res.status(), StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS);
|
||||
|
||||
let res: Response<Body> = ErrorInternalServerError("err").into();
|
||||
let res: HttpResponse = ErrorInternalServerError("err").into();
|
||||
assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||
|
||||
let res: Response<Body> = ErrorNotImplemented("err").into();
|
||||
let res: HttpResponse = ErrorNotImplemented("err").into();
|
||||
assert_eq!(res.status(), StatusCode::NOT_IMPLEMENTED);
|
||||
|
||||
let res: Response<Body> = ErrorBadGateway("err").into();
|
||||
let res: HttpResponse = ErrorBadGateway("err").into();
|
||||
assert_eq!(res.status(), StatusCode::BAD_GATEWAY);
|
||||
|
||||
let res: Response<Body> = ErrorServiceUnavailable("err").into();
|
||||
let res: HttpResponse = ErrorServiceUnavailable("err").into();
|
||||
assert_eq!(res.status(), StatusCode::SERVICE_UNAVAILABLE);
|
||||
|
||||
let res: Response<Body> = ErrorGatewayTimeout("err").into();
|
||||
let res: HttpResponse = ErrorGatewayTimeout("err").into();
|
||||
assert_eq!(res.status(), StatusCode::GATEWAY_TIMEOUT);
|
||||
|
||||
let res: Response<Body> = ErrorHttpVersionNotSupported("err").into();
|
||||
let res: HttpResponse = ErrorHttpVersionNotSupported("err").into();
|
||||
assert_eq!(res.status(), StatusCode::HTTP_VERSION_NOT_SUPPORTED);
|
||||
|
||||
let res: Response<Body> = ErrorVariantAlsoNegotiates("err").into();
|
||||
let res: HttpResponse = ErrorVariantAlsoNegotiates("err").into();
|
||||
assert_eq!(res.status(), StatusCode::VARIANT_ALSO_NEGOTIATES);
|
||||
|
||||
let res: Response<Body> = ErrorInsufficientStorage("err").into();
|
||||
let res: HttpResponse = ErrorInsufficientStorage("err").into();
|
||||
assert_eq!(res.status(), StatusCode::INSUFFICIENT_STORAGE);
|
||||
|
||||
let res: Response<Body> = ErrorLoopDetected("err").into();
|
||||
let res: HttpResponse = ErrorLoopDetected("err").into();
|
||||
assert_eq!(res.status(), StatusCode::LOOP_DETECTED);
|
||||
|
||||
let res: Response<Body> = ErrorNotExtended("err").into();
|
||||
let res: HttpResponse = ErrorNotExtended("err").into();
|
||||
assert_eq!(res.status(), StatusCode::NOT_EXTENDED);
|
||||
|
||||
let res: Response<Body> = ErrorNetworkAuthenticationRequired("err").into();
|
||||
let res: HttpResponse = ErrorNetworkAuthenticationRequired("err").into();
|
||||
assert_eq!(res.status(), StatusCode::NETWORK_AUTHENTICATION_REQUIRED);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,10 +6,7 @@ use std::{
|
|||
io::{self, Write as _},
|
||||
};
|
||||
|
||||
use actix_http::{
|
||||
body::{AnyBody, Body},
|
||||
header, Response, StatusCode,
|
||||
};
|
||||
use actix_http::{body::AnyBody, header, Response, StatusCode};
|
||||
use bytes::BytesMut;
|
||||
|
||||
use crate::{__downcast_dyn, __downcast_get_type_id};
|
||||
|
@ -79,6 +76,12 @@ impl<T: ResponseError + 'static> From<T> for Error {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<Error> for Response<AnyBody> {
|
||||
fn from(err: Error) -> Response<AnyBody> {
|
||||
err.error_response().into()
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////
|
||||
/////////////////////
|
||||
/////////////////////
|
||||
|
@ -164,7 +167,7 @@ impl ResponseError for actix_http::Error {
|
|||
}
|
||||
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
HttpResponse::new(self.status_code()).set_body(Body::from(self.to_string()))
|
||||
HttpResponse::new(self.status_code()).set_body(self.to_string().into())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -191,6 +194,8 @@ impl ResponseError for actix_http::error::PayloadError {
|
|||
}
|
||||
}
|
||||
|
||||
impl ResponseError for actix_http::ws::ProtocolError {}
|
||||
|
||||
impl ResponseError for actix_http::error::ContentTypeError {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
StatusCode::BAD_REQUEST
|
||||
|
@ -209,10 +214,14 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_error_casting() {
|
||||
let err = actix_http::error::PayloadError::Overflow;
|
||||
use actix_http::error::{ContentTypeError, PayloadError};
|
||||
|
||||
let err = PayloadError::Overflow;
|
||||
let resp_err: &dyn ResponseError = &err;
|
||||
|
||||
let err = resp_err.downcast_ref::<PayloadError>().unwrap();
|
||||
assert_eq!(err.to_string(), "Payload reached size limit.");
|
||||
|
||||
let not_err = resp_err.downcast_ref::<ContentTypeError>();
|
||||
assert!(not_err.is_none());
|
||||
}
|
||||
|
|
|
@ -140,7 +140,7 @@ pub mod dev {
|
|||
pub use crate::types::json::JsonBody;
|
||||
pub use crate::types::readlines::Readlines;
|
||||
|
||||
pub use actix_http::body::{Body, BodySize, MessageBody, ResponseBody, SizedStream};
|
||||
pub use actix_http::body::{AnyBody, Body, BodySize, MessageBody, ResponseBody, SizedStream};
|
||||
#[cfg(feature = "compress")]
|
||||
pub use actix_http::encoding::Decoder as Decompress;
|
||||
pub use actix_http::ResponseBuilder as BaseHttpResponseBuilder;
|
||||
|
|
|
@ -50,7 +50,7 @@ where
|
|||
T: Transform<S, Req>,
|
||||
T::Future: 'static,
|
||||
T::Response: MapServiceResponseBody,
|
||||
Error: From<T::Error>,
|
||||
T::Error: Into<Error>,
|
||||
{
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
|
@ -75,7 +75,7 @@ impl<S, Req> Service<Req> for CompatMiddleware<S>
|
|||
where
|
||||
S: Service<Req>,
|
||||
S::Response: MapServiceResponseBody,
|
||||
Error: From<S::Error>,
|
||||
S::Error: Into<Error>,
|
||||
{
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
|
@ -99,12 +99,16 @@ impl<Fut, T, E> Future for CompatMiddlewareFuture<Fut>
|
|||
where
|
||||
Fut: Future<Output = Result<T, E>>,
|
||||
T: MapServiceResponseBody,
|
||||
Error: From<E>,
|
||||
E: Into<Error>,
|
||||
{
|
||||
type Output = Result<ServiceResponse, Error>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let res = ready!(self.project().fut.poll(cx))?;
|
||||
let res = match ready!(self.project().fut.poll(cx)) {
|
||||
Ok(res) => res,
|
||||
Err(err) => return Poll::Ready(Err(err.into())),
|
||||
};
|
||||
|
||||
Poll::Ready(Ok(res.map_body()))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,6 @@ use actix_http::{
|
|||
body::{MessageBody, ResponseBody},
|
||||
encoding::Encoder,
|
||||
http::header::{ContentEncoding, ACCEPT_ENCODING},
|
||||
Error,
|
||||
};
|
||||
use actix_service::{Service, Transform};
|
||||
use actix_utils::future::{ok, Ready};
|
||||
|
@ -23,6 +22,7 @@ use pin_project::pin_project;
|
|||
use crate::{
|
||||
dev::BodyEncoding,
|
||||
service::{ServiceRequest, ServiceResponse},
|
||||
Error,
|
||||
};
|
||||
|
||||
/// Middleware for compressing response payloads.
|
||||
|
|
|
@ -8,7 +8,7 @@ use std::{
|
|||
};
|
||||
|
||||
use actix_http::{
|
||||
body::{Body, BodyStream},
|
||||
body::{AnyBody, BodyStream},
|
||||
http::{
|
||||
header::{self, HeaderName, IntoHeaderPair, IntoHeaderValue},
|
||||
ConnectionType, Error as HttpError, StatusCode,
|
||||
|
@ -33,7 +33,7 @@ use crate::{
|
|||
///
|
||||
/// This type can be used to construct an instance of `Response` through a builder-like pattern.
|
||||
pub struct HttpResponseBuilder {
|
||||
res: Option<Response<Body>>,
|
||||
res: Option<Response<AnyBody>>,
|
||||
err: Option<HttpError>,
|
||||
#[cfg(feature = "cookies")]
|
||||
cookies: Option<CookieJar>,
|
||||
|
@ -311,7 +311,7 @@ impl HttpResponseBuilder {
|
|||
///
|
||||
/// `HttpResponseBuilder` can not be used after this call.
|
||||
#[inline]
|
||||
pub fn body<B: Into<Body>>(&mut self, body: B) -> HttpResponse<Body> {
|
||||
pub fn body<B: Into<AnyBody>>(&mut self, body: B) -> HttpResponse<AnyBody> {
|
||||
match self.message_body(body.into()) {
|
||||
Ok(res) => res,
|
||||
Err(err) => HttpResponse::from_error(err),
|
||||
|
@ -357,7 +357,7 @@ impl HttpResponseBuilder {
|
|||
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
|
||||
E: Into<Box<dyn StdError>> + 'static,
|
||||
{
|
||||
self.body(Body::from_message(BodyStream::new(stream)))
|
||||
self.body(AnyBody::from_message(BodyStream::new(stream)))
|
||||
}
|
||||
|
||||
/// Set a json body and generate `Response`
|
||||
|
@ -376,7 +376,7 @@ impl HttpResponseBuilder {
|
|||
self.insert_header((header::CONTENT_TYPE, mime::APPLICATION_JSON));
|
||||
}
|
||||
|
||||
self.body(Body::from(body))
|
||||
self.body(AnyBody::from(body))
|
||||
}
|
||||
Err(err) => HttpResponse::from_error(JsonPayloadError::Serialize(err)),
|
||||
}
|
||||
|
@ -387,7 +387,7 @@ impl HttpResponseBuilder {
|
|||
/// `HttpResponseBuilder` can not be used after this call.
|
||||
#[inline]
|
||||
pub fn finish(&mut self) -> HttpResponse {
|
||||
self.body(Body::Empty)
|
||||
self.body(AnyBody::Empty)
|
||||
}
|
||||
|
||||
/// This method construct new `HttpResponseBuilder`
|
||||
|
@ -416,7 +416,7 @@ impl From<HttpResponseBuilder> for HttpResponse {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<HttpResponseBuilder> for Response<Body> {
|
||||
impl From<HttpResponseBuilder> for Response<AnyBody> {
|
||||
fn from(mut builder: HttpResponseBuilder) -> Self {
|
||||
builder.finish().into()
|
||||
}
|
||||
|
|
|
@ -188,6 +188,7 @@ impl Route {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::convert::Infallible;
|
||||
use std::time::Duration;
|
||||
|
||||
use actix_rt::time::sleep;
|
||||
|
@ -215,7 +216,7 @@ mod tests {
|
|||
}))
|
||||
.route(web::post().to(|| async {
|
||||
sleep(Duration::from_millis(100)).await;
|
||||
Ok::<_, ()>(HttpResponse::Created())
|
||||
Ok::<_, Infallible>(HttpResponse::Created())
|
||||
}))
|
||||
.route(web::delete().to(|| async {
|
||||
sleep(Duration::from_millis(100)).await;
|
||||
|
|
|
@ -8,9 +8,7 @@ use std::{
|
|||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use actix_http::{
|
||||
body::MessageBody, Error, Extensions, HttpService, KeepAlive, Request, Response,
|
||||
};
|
||||
use actix_http::{body::MessageBody, Extensions, HttpService, KeepAlive, Request, Response};
|
||||
use actix_server::{Server, ServerBuilder};
|
||||
use actix_service::{
|
||||
map_config, IntoServiceFactory, Service, ServiceFactory, ServiceFactoryExt as _,
|
||||
|
@ -21,7 +19,7 @@ use actix_tls::accept::openssl::{AlpnError, SslAcceptor, SslAcceptorBuilder};
|
|||
#[cfg(feature = "rustls")]
|
||||
use actix_tls::accept::rustls::ServerConfig as RustlsServerConfig;
|
||||
|
||||
use crate::config::AppConfig;
|
||||
use crate::{config::AppConfig, Error};
|
||||
|
||||
struct Socket {
|
||||
scheme: &'static str,
|
||||
|
@ -305,7 +303,9 @@ where
|
|||
svc
|
||||
};
|
||||
|
||||
let fac = factory().into_factory().map_err(|err| err.into());
|
||||
let fac = factory()
|
||||
.into_factory()
|
||||
.map_err(|err| err.into().error_response());
|
||||
|
||||
svc.finish(map_config(fac, move |_| {
|
||||
AppConfig::new(false, host.clone(), addr)
|
||||
|
@ -362,7 +362,9 @@ where
|
|||
svc
|
||||
};
|
||||
|
||||
let fac = factory().into_factory().map_err(|err| err.into());
|
||||
let fac = factory()
|
||||
.into_factory()
|
||||
.map_err(|err| err.into().error_response());
|
||||
|
||||
svc.finish(map_config(fac, move |_| {
|
||||
AppConfig::new(true, host.clone(), addr)
|
||||
|
@ -418,7 +420,9 @@ where
|
|||
svc
|
||||
};
|
||||
|
||||
let fac = factory().into_factory().map_err(|err| err.into());
|
||||
let fac = factory()
|
||||
.into_factory()
|
||||
.map_err(|err| err.into().error_response());
|
||||
|
||||
svc.finish(map_config(fac, move |_| {
|
||||
AppConfig::new(true, host.clone(), addr)
|
||||
|
@ -543,7 +547,9 @@ where
|
|||
svc
|
||||
};
|
||||
|
||||
let fac = factory().into_factory().map_err(|err| err.into());
|
||||
let fac = factory()
|
||||
.into_factory()
|
||||
.map_err(|err| err.into().error_response());
|
||||
|
||||
svc.finish(map_config(fac, move |_| config.clone()))
|
||||
})
|
||||
|
@ -581,7 +587,9 @@ where
|
|||
socket_addr,
|
||||
);
|
||||
|
||||
let fac = factory().into_factory().map_err(|err| err.into());
|
||||
let fac = factory()
|
||||
.into_factory()
|
||||
.map_err(|err| err.into().error_response());
|
||||
|
||||
fn_service(|io: UnixStream| async { Ok((io, Protocol::Http1, None)) }).and_then(
|
||||
HttpService::build()
|
||||
|
|
|
@ -2,9 +2,9 @@ use std::cell::{Ref, RefMut};
|
|||
use std::rc::Rc;
|
||||
use std::{fmt, net};
|
||||
|
||||
use actix_http::body::{Body, MessageBody};
|
||||
use actix_http::http::{HeaderMap, Method, StatusCode, Uri, Version};
|
||||
use actix_http::{
|
||||
body::{AnyBody, MessageBody},
|
||||
http::{HeaderMap, Method, StatusCode, Uri, Version},
|
||||
Extensions, HttpMessage, Payload, PayloadStream, RequestHead, Response, ResponseHead,
|
||||
};
|
||||
use actix_router::{IntoPattern, Path, Resource, ResourceDef, Url};
|
||||
|
@ -330,12 +330,12 @@ impl fmt::Debug for ServiceRequest {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct ServiceResponse<B = Body> {
|
||||
pub struct ServiceResponse<B = AnyBody> {
|
||||
request: HttpRequest,
|
||||
response: HttpResponse<B>,
|
||||
}
|
||||
|
||||
impl ServiceResponse<Body> {
|
||||
impl ServiceResponse<AnyBody> {
|
||||
/// Create service response from the error
|
||||
pub fn from_err<E: Into<Error>>(err: E, request: HttpRequest) -> Self {
|
||||
let response = HttpResponse::from_error(err);
|
||||
|
|
Loading…
Reference in New Issue