2021-06-17 18:57:58 +02:00
|
|
|
use std::convert::Infallible;
|
|
|
|
|
2021-12-08 07:01:11 +01:00
|
|
|
use actix_http::{body::BoxBody, HttpMessage, HttpService, Request, Response, StatusCode};
|
2019-12-12 18:08:38 +01:00
|
|
|
use actix_http_test::test_server;
|
2021-01-04 00:47:04 +01:00
|
|
|
use actix_service::ServiceFactoryExt;
|
2021-04-01 16:26:13 +02:00
|
|
|
use actix_utils::future;
|
2021-01-04 00:47:04 +01:00
|
|
|
use bytes::Bytes;
|
2024-08-18 16:17:03 +02:00
|
|
|
use derive_more::derive::{Display, Error};
|
2021-04-01 16:26:13 +02:00
|
|
|
use futures_util::StreamExt as _;
|
2018-11-14 07:53:30 +01:00
|
|
|
|
|
|
|
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World";
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
2022-01-31 18:30:34 +01:00
|
|
|
async fn h1_v2() {
|
2019-12-12 18:08:38 +01:00
|
|
|
let srv = test_server(move || {
|
2019-12-02 12:33:11 +01:00
|
|
|
HttpService::build()
|
2021-06-17 18:57:58 +02:00
|
|
|
.finish(|_| future::ok::<_, Infallible>(Response::ok().set_body(STR)))
|
2019-12-02 12:33:11 +01:00
|
|
|
.tcp()
|
2020-03-08 08:42:45 +01:00
|
|
|
})
|
|
|
|
.await;
|
2018-11-14 07:53:30 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let response = srv.get("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2018-11-14 07:53:30 +01:00
|
|
|
|
2021-01-15 03:11:10 +01:00
|
|
|
let request = srv.get("/").insert_header(("x-test", "111")).send();
|
2019-11-26 06:25:50 +01:00
|
|
|
let mut response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2018-11-14 07:53:30 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2018-11-14 07:53:30 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let mut response = srv.post("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-11-19 13:54:19 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
2022-01-31 18:30:34 +01:00
|
|
|
async fn connection_close() {
|
2019-12-12 18:08:38 +01:00
|
|
|
let srv = test_server(move || {
|
2019-11-26 06:25:50 +01:00
|
|
|
HttpService::build()
|
2021-06-17 18:57:58 +02:00
|
|
|
.finish(|_| future::ok::<_, Infallible>(Response::ok().set_body(STR)))
|
2019-12-02 12:33:11 +01:00
|
|
|
.tcp()
|
2019-11-26 06:25:50 +01:00
|
|
|
.map(|_| ())
|
2020-03-08 08:42:45 +01:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-19 13:54:19 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let response = srv.get("/").force_close().send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
2022-01-31 18:30:34 +01:00
|
|
|
async fn with_query_parameter() {
|
2019-12-12 18:08:38 +01:00
|
|
|
let srv = test_server(move || {
|
2019-11-26 06:25:50 +01:00
|
|
|
HttpService::build()
|
2021-06-17 18:57:58 +02:00
|
|
|
.finish(|req: Request| async move {
|
2019-11-26 06:25:50 +01:00
|
|
|
if req.uri().query().unwrap().contains("qp=") {
|
2021-06-17 18:57:58 +02:00
|
|
|
Ok::<_, Infallible>(Response::ok())
|
2019-11-26 06:25:50 +01:00
|
|
|
} else {
|
2021-06-17 18:57:58 +02:00
|
|
|
Ok(Response::bad_request())
|
2019-11-26 06:25:50 +01:00
|
|
|
}
|
|
|
|
})
|
2019-12-02 12:33:11 +01:00
|
|
|
.tcp()
|
2019-11-26 06:25:50 +01:00
|
|
|
.map(|_| ())
|
2020-03-08 08:42:45 +01:00
|
|
|
})
|
|
|
|
.await;
|
2018-11-14 07:53:30 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let request = srv.request(http::Method::GET, srv.url("/?qp=5"));
|
|
|
|
let response = request.send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
2021-03-07 19:33:16 +01:00
|
|
|
|
2021-05-14 17:40:00 +02:00
|
|
|
#[derive(Debug, Display, Error)]
|
2024-08-18 16:17:03 +02:00
|
|
|
#[display("expect failed")]
|
2021-05-14 17:40:00 +02:00
|
|
|
struct ExpectFailed;
|
|
|
|
|
2021-12-04 20:40:47 +01:00
|
|
|
impl From<ExpectFailed> for Response<BoxBody> {
|
2021-06-17 18:57:58 +02:00
|
|
|
fn from(_: ExpectFailed) -> Self {
|
|
|
|
Response::new(StatusCode::EXPECTATION_FAILED)
|
2021-05-14 17:40:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 19:33:16 +01:00
|
|
|
#[actix_rt::test]
|
2022-01-31 18:30:34 +01:00
|
|
|
async fn h1_expect() {
|
2021-03-07 19:33:16 +01:00
|
|
|
let srv = test_server(move || {
|
|
|
|
HttpService::build()
|
|
|
|
.expect(|req: Request| async {
|
|
|
|
if req.headers().contains_key("AUTH") {
|
|
|
|
Ok(req)
|
|
|
|
} else {
|
2021-05-14 17:40:00 +02:00
|
|
|
Err(ExpectFailed)
|
2021-03-07 19:33:16 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.h1(|req: Request| async move {
|
|
|
|
let (_, mut body) = req.into_parts();
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
while let Some(Ok(chunk)) = body.next().await {
|
|
|
|
buf.extend_from_slice(&chunk);
|
|
|
|
}
|
|
|
|
let str = std::str::from_utf8(&buf).unwrap();
|
|
|
|
assert_eq!(str, "expect body");
|
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
Ok::<_, Infallible>(Response::ok())
|
2021-03-07 19:33:16 +01:00
|
|
|
})
|
|
|
|
.tcp()
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// test expect without payload.
|
|
|
|
let request = srv
|
|
|
|
.request(http::Method::GET, srv.url("/"))
|
|
|
|
.insert_header(("Expect", "100-continue"));
|
|
|
|
|
|
|
|
let response = request.send().await;
|
|
|
|
assert!(response.is_err());
|
|
|
|
|
|
|
|
// test expect would fail to continue
|
|
|
|
let request = srv
|
|
|
|
.request(http::Method::GET, srv.url("/"))
|
|
|
|
.insert_header(("Expect", "100-continue"));
|
|
|
|
|
|
|
|
let response = request.send_body("expect body").await.unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::EXPECTATION_FAILED);
|
|
|
|
|
2021-05-14 17:40:00 +02:00
|
|
|
// test expect would continue
|
2021-03-07 19:33:16 +01:00
|
|
|
let request = srv
|
|
|
|
.request(http::Method::GET, srv.url("/"))
|
|
|
|
.insert_header(("Expect", "100-continue"))
|
|
|
|
.insert_header(("AUTH", "996"));
|
|
|
|
|
|
|
|
let response = request.send_body("expect body").await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
}
|