2018-12-11 02:08:33 +00:00
|
|
|
use actix_service::NewService;
|
2019-03-30 04:13:39 +00:00
|
|
|
use bytes::Bytes;
|
2018-11-19 05:48:20 +00:00
|
|
|
use futures::future::{self, ok};
|
2018-11-14 06:53:30 +00:00
|
|
|
|
2019-03-30 04:13:39 +00:00
|
|
|
use actix_http::{http, HttpService, Request, Response};
|
2019-01-27 18:59:07 +00:00
|
|
|
use actix_http_test::TestServer;
|
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";
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_h1_v2() {
|
2018-12-11 02:08:33 +00:00
|
|
|
env_logger::init();
|
2019-02-19 02:46:30 +00:00
|
|
|
let mut srv = TestServer::new(move || {
|
2019-04-09 00:49:27 +00:00
|
|
|
HttpService::build().finish(|_| future::ok::<_, ()>(Response::Ok().body(STR)))
|
2018-11-14 06:53:30 +00:00
|
|
|
});
|
2019-04-02 20:35:01 +00:00
|
|
|
let response = srv.block_on(srv.get("/").send()).unwrap();
|
2018-11-14 06:53:30 +00:00
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
2019-04-02 20:35:01 +00:00
|
|
|
let request = srv.get("/").header("x-test", "111").send();
|
2019-03-30 01:51:07 +00:00
|
|
|
let response = srv.block_on(request).unwrap();
|
2018-11-14 06:53:30 +00:00
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
2019-03-30 01:51:07 +00:00
|
|
|
let bytes = srv.load_body(response).unwrap();
|
2018-11-14 17:38:16 +00:00
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2018-11-14 06:53:30 +00:00
|
|
|
|
2019-04-02 20:35:01 +00:00
|
|
|
let response = srv.block_on(srv.post("/").send()).unwrap();
|
2018-11-14 06:53:30 +00:00
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
2019-03-30 01:51:07 +00:00
|
|
|
let bytes = srv.load_body(response).unwrap();
|
2018-11-14 17:38:16 +00:00
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2018-11-14 06:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_connection_close() {
|
2019-02-19 02:46:30 +00:00
|
|
|
let mut srv = TestServer::new(move || {
|
2019-03-09 18:39:06 +00:00
|
|
|
HttpService::build()
|
2018-11-19 05:48:20 +00:00
|
|
|
.finish(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
|
|
|
.map(|_| ())
|
2018-11-14 06:53:30 +00:00
|
|
|
});
|
2019-04-05 23:46:44 +00:00
|
|
|
println!("REQ: {:?}", srv.get("/").force_close());
|
2019-04-02 21:47:59 +00:00
|
|
|
let response = srv.block_on(srv.get("/").force_close().send()).unwrap();
|
2019-04-05 23:46:44 +00:00
|
|
|
println!("RES: {:?}", response);
|
2018-11-14 06:53:30 +00:00
|
|
|
assert!(response.status().is_success());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_with_query_parameter() {
|
2019-02-19 02:46:30 +00:00
|
|
|
let mut srv = TestServer::new(move || {
|
2019-03-09 18:39:06 +00:00
|
|
|
HttpService::build()
|
2018-11-19 05:48:20 +00:00
|
|
|
.finish(|req: Request| {
|
|
|
|
if req.uri().query().unwrap().contains("qp=") {
|
|
|
|
ok::<_, ()>(Response::Ok().finish())
|
|
|
|
} else {
|
|
|
|
ok::<_, ()>(Response::BadRequest().finish())
|
|
|
|
}
|
2018-12-06 22:32:52 +00:00
|
|
|
})
|
|
|
|
.map(|_| ())
|
2018-11-14 06:53:30 +00:00
|
|
|
});
|
|
|
|
|
2019-03-26 18:43:22 +00:00
|
|
|
let request = srv.request(http::Method::GET, srv.url("/?qp=5")).send();
|
|
|
|
let response = srv.block_on(request).unwrap();
|
2018-11-14 06:53:30 +00:00
|
|
|
assert!(response.status().is_success());
|
|
|
|
}
|