1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-25 06:39:22 +02:00

move internalerror to actix web (#2215)

This commit is contained in:
Rob Ede
2021-05-14 16:40:00 +01:00
committed by GitHub
parent f277b128b6
commit 2a8c650f2c
18 changed files with 456 additions and 397 deletions

View File

@ -1,10 +1,11 @@
use actix_http::{
error, http, http::StatusCode, HttpMessage, HttpService, Request, Response,
http, http::StatusCode, HttpMessage, HttpService, Request, Response, ResponseError,
};
use actix_http_test::test_server;
use actix_service::ServiceFactoryExt;
use actix_utils::future;
use bytes::Bytes;
use derive_more::{Display, Error};
use futures_util::StreamExt as _;
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
@ -92,6 +93,16 @@ async fn test_with_query_parameter() {
assert!(response.status().is_success());
}
#[derive(Debug, Display, Error)]
#[display(fmt = "expect failed")]
struct ExpectFailed;
impl ResponseError for ExpectFailed {
fn status_code(&self) -> StatusCode {
StatusCode::EXPECTATION_FAILED
}
}
#[actix_rt::test]
async fn test_h1_expect() {
let srv = test_server(move || {
@ -100,7 +111,7 @@ async fn test_h1_expect() {
if req.headers().contains_key("AUTH") {
Ok(req)
} else {
Err(error::ErrorExpectationFailed("expect failed"))
Err(ExpectFailed)
}
})
.h1(|req: Request| async move {
@ -134,7 +145,7 @@ async fn test_h1_expect() {
let response = request.send_body("expect body").await.unwrap();
assert_eq!(response.status(), StatusCode::EXPECTATION_FAILED);
// test exepct would continue
// test expect would continue
let request = srv
.request(http::Method::GET, srv.url("/"))
.insert_header(("Expect", "100-continue"))

View File

@ -6,17 +6,18 @@ use std::io;
use actix_http::{
body::{Body, SizedStream},
error::{ErrorBadRequest, PayloadError},
error::PayloadError,
http::{
header::{self, HeaderName, HeaderValue},
Method, StatusCode, Version,
},
Error, HttpMessage, HttpService, Request, Response,
Error, HttpMessage, HttpService, Request, Response, ResponseError,
};
use actix_http_test::test_server;
use actix_service::{fn_service, ServiceFactoryExt};
use actix_utils::future::{err, ok, ready};
use bytes::{Bytes, BytesMut};
use derive_more::{Display, Error};
use futures_core::Stream;
use futures_util::stream::{once, StreamExt as _};
use openssl::{
@ -401,11 +402,21 @@ async fn test_h2_response_http_error_handling() {
assert_eq!(bytes, Bytes::from_static(b"failed to parse header value"));
}
#[derive(Debug, Display, Error)]
#[display(fmt = "error")]
struct BadRequest;
impl ResponseError for BadRequest {
fn status_code(&self) -> StatusCode {
StatusCode::BAD_REQUEST
}
}
#[actix_rt::test]
async fn test_h2_service_error() {
let mut srv = test_server(move || {
HttpService::build()
.h2(|_| err::<Response<Body>, Error>(ErrorBadRequest("error")))
.h2(|_| err::<Response<Body>, _>(BadRequest))
.openssl(tls_config())
.map_err(|_| ())
})

View File

@ -4,18 +4,18 @@ extern crate tls_rustls as rustls;
use actix_http::{
body::{Body, SizedStream},
error::{self, PayloadError},
error::PayloadError,
http::{
header::{self, HeaderName, HeaderValue},
Method, StatusCode, Version,
},
Error, HttpService, Request, Response,
Error, HttpService, Request, Response, ResponseError,
};
use actix_http_test::test_server;
use actix_service::{fn_factory_with_config, fn_service};
use actix_utils::future::{err, ok};
use bytes::{Bytes, BytesMut};
use derive_more::{Display, Error};
use futures_core::Stream;
use futures_util::stream::{once, StreamExt as _};
use rustls::{
@ -417,11 +417,21 @@ async fn test_h2_response_http_error_handling() {
assert_eq!(bytes, Bytes::from_static(b"failed to parse header value"));
}
#[derive(Debug, Display, Error)]
#[display(fmt = "error")]
struct BadRequest;
impl ResponseError for BadRequest {
fn status_code(&self) -> StatusCode {
StatusCode::BAD_REQUEST
}
}
#[actix_rt::test]
async fn test_h2_service_error() {
let mut srv = test_server(move || {
HttpService::build()
.h2(|_| err::<Response<Body>, Error>(error::ErrorBadRequest("error")))
.h2(|_| err::<Response<Body>, _>(BadRequest))
.rustls(tls_config())
})
.await;
@ -438,7 +448,7 @@ async fn test_h2_service_error() {
async fn test_h1_service_error() {
let mut srv = test_server(move || {
HttpService::build()
.h1(|_| err::<Response<Body>, Error>(error::ErrorBadRequest("error")))
.h1(|_| err::<Response<Body>, _>(BadRequest))
.rustls(tls_config())
})
.await;

View File

@ -2,23 +2,22 @@ use std::io::{Read, Write};
use std::time::Duration;
use std::{net, thread};
use actix_http::{
body::{Body, SizedStream},
http::{self, header, StatusCode},
Error, HttpService, KeepAlive, Request, Response,
};
use actix_http::{HttpMessage, ResponseError};
use actix_http_test::test_server;
use actix_rt::time::sleep;
use actix_service::fn_service;
use actix_utils::future::{err, ok, ready};
use bytes::Bytes;
use derive_more::{Display, Error};
use futures_util::stream::{once, StreamExt as _};
use futures_util::FutureExt as _;
use regex::Regex;
use actix_http::HttpMessage;
use actix_http::{
body::{Body, SizedStream},
error,
http::{self, header, StatusCode},
Error, HttpService, KeepAlive, Request, Response,
};
#[actix_rt::test]
async fn test_h1() {
let srv = test_server(|| {
@ -58,6 +57,16 @@ async fn test_h1_2() {
assert!(response.status().is_success());
}
#[derive(Debug, Display, Error)]
#[display(fmt = "expect failed")]
struct ExpectFailed;
impl ResponseError for ExpectFailed {
fn status_code(&self) -> StatusCode {
StatusCode::PRECONDITION_FAILED
}
}
#[actix_rt::test]
async fn test_expect_continue() {
let srv = test_server(|| {
@ -66,7 +75,7 @@ async fn test_expect_continue() {
if req.head().uri.query() == Some("yes=") {
ok(req)
} else {
err(error::ErrorPreconditionFailed("error"))
err(ExpectFailed)
}
}))
.finish(|_| ok::<_, ()>(Response::ok()))
@ -96,7 +105,7 @@ async fn test_expect_continue_h1() {
if req.head().uri.query() == Some("yes=") {
ok(req)
} else {
err(error::ErrorPreconditionFailed("error"))
err(ExpectFailed)
}
})
}))
@ -647,11 +656,21 @@ async fn test_h1_response_http_error_handling() {
assert_eq!(bytes, Bytes::from_static(b"failed to parse header value"));
}
#[derive(Debug, Display, Error)]
#[display(fmt = "error")]
struct BadRequest;
impl ResponseError for BadRequest {
fn status_code(&self) -> StatusCode {
StatusCode::BAD_REQUEST
}
}
#[actix_rt::test]
async fn test_h1_service_error() {
let mut srv = test_server(|| {
HttpService::build()
.h1(|_| err::<Response<Body>, _>(error::ErrorBadRequest("error")))
.h1(|_| err::<Response<Body>, _>(BadRequest))
.tcp()
})
.await;