2021-03-07 18:33:16 +00:00
|
|
|
use actix_http::{
|
|
|
|
error, http, http::StatusCode, HttpMessage, HttpService, Request, Response,
|
|
|
|
};
|
2019-12-12 17:08:38 +00:00
|
|
|
use actix_http_test::test_server;
|
2021-01-03 23:47:04 +00:00
|
|
|
use actix_service::ServiceFactoryExt;
|
|
|
|
use bytes::Bytes;
|
2021-03-07 18:33:16 +00:00
|
|
|
use futures_util::{
|
|
|
|
future::{self, ok},
|
2021-03-11 03:48:38 +00:00
|
|
|
StreamExt as _,
|
2021-03-07 18:33:16 +00:00
|
|
|
};
|
2018-11-14 06:53:30 +00: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 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h1_v2() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
|
|
|
.finish(|_| future::ok::<_, ()>(Response::Ok().body(STR)))
|
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2018-11-14 06:53:30 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let response = srv.get("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2018-11-14 06:53:30 +00:00
|
|
|
|
2021-01-15 02:11:10 +00:00
|
|
|
let request = srv.get("/").insert_header(("x-test", "111")).send();
|
2019-11-26 05:25:50 +00:00
|
|
|
let mut response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2018-11-14 06:53:30 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2018-11-14 06:53:30 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let mut response = srv.post("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2018-11-14 06:53:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_connection_close() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-11-26 05:25:50 +00:00
|
|
|
HttpService::build()
|
|
|
|
.finish(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2019-11-26 05:25:50 +00:00
|
|
|
.map(|_| ())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let response = srv.get("/").force_close().send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2018-11-14 06:53:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_with_query_parameter() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-11-26 05:25:50 +00:00
|
|
|
HttpService::build()
|
|
|
|
.finish(|req: Request| {
|
|
|
|
if req.uri().query().unwrap().contains("qp=") {
|
|
|
|
ok::<_, ()>(Response::Ok().finish())
|
|
|
|
} else {
|
|
|
|
ok::<_, ()>(Response::BadRequest().finish())
|
|
|
|
}
|
|
|
|
})
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2019-11-26 05:25:50 +00:00
|
|
|
.map(|_| ())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2018-11-14 06:53:30 +00:00
|
|
|
|
2019-11-26 05:25:50 +00: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 06:53:30 +00:00
|
|
|
}
|
2021-03-07 18:33:16 +00:00
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h1_expect() {
|
|
|
|
let srv = test_server(move || {
|
|
|
|
HttpService::build()
|
|
|
|
.expect(|req: Request| async {
|
|
|
|
if req.headers().contains_key("AUTH") {
|
|
|
|
Ok(req)
|
|
|
|
} else {
|
|
|
|
Err(error::ErrorExpectationFailed("expect failed"))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.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");
|
|
|
|
|
|
|
|
Ok::<_, ()>(Response::Ok().finish())
|
|
|
|
})
|
|
|
|
.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);
|
|
|
|
|
|
|
|
// test exepct would continue
|
|
|
|
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());
|
|
|
|
}
|