1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-12-18 09:53:11 +01:00
actix-web/actix-http/tests/test_client.rs

89 lines
3.6 KiB
Rust
Raw Normal View History

2019-11-19 13:54:19 +01:00
use actix_service::ServiceFactory;
2019-03-30 05:13:39 +01:00
use bytes::Bytes;
2018-11-19 06:48:20 +01:00
use futures::future::{self, ok};
2018-11-14 07:53:30 +01:00
2019-03-30 05:13:39 +01:00
use actix_http::{http, HttpService, Request, Response};
2019-12-12 18:08:38 +01:00
use actix_http_test::test_server;
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]
async fn test_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()
.finish(|_| future::ok::<_, ()>(Response::Ok().body(STR)))
.tcp()
2019-11-26 06:25:50 +01:00
});
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
2019-11-26 06:25:50 +01:00
let request = srv.get("/").header("x-test", "111").send();
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]
async fn test_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()
.finish(|_| ok::<_, ()>(Response::Ok().body(STR)))
2019-12-02 12:33:11 +01:00
.tcp()
2019-11-26 06:25:50 +01:00
.map(|_| ())
});
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]
async fn test_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()
.finish(|req: Request| {
if req.uri().query().unwrap().contains("qp=") {
ok::<_, ()>(Response::Ok().finish())
} else {
ok::<_, ()>(Response::BadRequest().finish())
}
})
2019-12-02 12:33:11 +01:00
.tcp()
2019-11-26 06:25:50 +01:00
.map(|_| ())
});
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
}