2018-12-11 03:08:33 +01:00
|
|
|
use actix_service::NewService;
|
2019-03-17 09:02:51 +01:00
|
|
|
use bytes::{Bytes, BytesMut};
|
2018-11-19 06:48:20 +01:00
|
|
|
use futures::future::{self, ok};
|
2019-03-17 09:02:51 +01:00
|
|
|
use futures::{Future, Stream};
|
2018-11-14 07:53:30 +01:00
|
|
|
|
2019-03-17 09:02:51 +01:00
|
|
|
use actix_http::{
|
|
|
|
client, error::PayloadError, HttpMessage, HttpService, Request, Response,
|
|
|
|
};
|
2019-01-27 19:59:07 +01:00
|
|
|
use actix_http_test::TestServer;
|
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-03-17 09:02:51 +01:00
|
|
|
fn load_body<S>(stream: S) -> impl Future<Item = BytesMut, Error = PayloadError>
|
|
|
|
where
|
|
|
|
S: Stream<Item = Bytes, Error = PayloadError>,
|
|
|
|
{
|
|
|
|
stream.fold(BytesMut::new(), move |mut body, chunk| {
|
|
|
|
body.extend_from_slice(&chunk);
|
|
|
|
Ok::<_, PayloadError>(body)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:53:30 +01:00
|
|
|
#[test]
|
|
|
|
fn test_h1_v2() {
|
2018-12-11 03:08:33 +01:00
|
|
|
env_logger::init();
|
2019-02-19 03:46:30 +01:00
|
|
|
let mut srv = TestServer::new(move || {
|
2019-03-09 19:39:06 +01:00
|
|
|
HttpService::build()
|
2018-11-19 06:48:20 +01:00
|
|
|
.finish(|_| future::ok::<_, ()>(Response::Ok().body(STR)))
|
|
|
|
.map(|_| ())
|
2018-11-14 07:53:30 +01:00
|
|
|
});
|
2019-03-13 22:41:40 +01:00
|
|
|
let mut connector = srv.connector();
|
2018-11-14 07:53:30 +01:00
|
|
|
|
2018-11-19 06:48:20 +01:00
|
|
|
let request = srv.get().finish().unwrap();
|
|
|
|
let response = srv.block_on(request.send(&mut connector)).unwrap();
|
2018-11-14 07:53:30 +01:00
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
2018-11-19 06:48:20 +01:00
|
|
|
let request = srv.get().header("x-test", "111").finish().unwrap();
|
2018-11-14 07:53:30 +01:00
|
|
|
let repr = format!("{:?}", request);
|
|
|
|
assert!(repr.contains("ClientRequest"));
|
|
|
|
assert!(repr.contains("x-test"));
|
|
|
|
|
2019-02-13 22:52:11 +01:00
|
|
|
let mut response = srv.block_on(request.send(&mut connector)).unwrap();
|
2018-11-14 07:53:30 +01:00
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
2019-03-17 09:02:51 +01:00
|
|
|
let bytes = srv.block_on(load_body(response.take_payload())).unwrap();
|
2018-11-14 18:38:16 +01:00
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2018-11-14 07:53:30 +01:00
|
|
|
|
2018-11-19 06:48:20 +01:00
|
|
|
let request = srv.post().finish().unwrap();
|
2019-02-13 22:52:11 +01:00
|
|
|
let mut response = srv.block_on(request.send(&mut connector)).unwrap();
|
2018-11-14 07:53:30 +01:00
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
2019-03-17 09:02:51 +01:00
|
|
|
let bytes = srv.block_on(load_body(response.take_payload())).unwrap();
|
2018-11-14 18:38:16 +01:00
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_connection_close() {
|
2019-02-19 03:46:30 +01:00
|
|
|
let mut srv = TestServer::new(move || {
|
2019-03-09 19:39:06 +01:00
|
|
|
HttpService::build()
|
2018-11-19 06:48:20 +01:00
|
|
|
.finish(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
|
|
|
.map(|_| ())
|
2018-11-14 07:53:30 +01:00
|
|
|
});
|
2019-03-13 22:41:40 +01:00
|
|
|
let mut connector = srv.connector();
|
2018-11-14 07:53:30 +01:00
|
|
|
|
2018-11-19 06:48:20 +01:00
|
|
|
let request = srv.get().close().finish().unwrap();
|
|
|
|
let response = srv.block_on(request.send(&mut connector)).unwrap();
|
2018-11-14 07:53:30 +01:00
|
|
|
assert!(response.status().is_success());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_with_query_parameter() {
|
2019-02-19 03:46:30 +01:00
|
|
|
let mut srv = TestServer::new(move || {
|
2019-03-09 19:39:06 +01:00
|
|
|
HttpService::build()
|
2018-11-19 06:48:20 +01:00
|
|
|
.finish(|req: Request| {
|
|
|
|
if req.uri().query().unwrap().contains("qp=") {
|
|
|
|
ok::<_, ()>(Response::Ok().finish())
|
|
|
|
} else {
|
|
|
|
ok::<_, ()>(Response::BadRequest().finish())
|
|
|
|
}
|
2018-12-06 23:32:52 +01:00
|
|
|
})
|
|
|
|
.map(|_| ())
|
2018-11-14 07:53:30 +01:00
|
|
|
});
|
2019-03-13 22:41:40 +01:00
|
|
|
let mut connector = srv.connector();
|
2018-11-14 07:53:30 +01:00
|
|
|
|
2018-11-19 06:48:20 +01:00
|
|
|
let request = client::ClientRequest::get(srv.url("/?qp=5"))
|
2018-11-14 07:53:30 +01:00
|
|
|
.finish()
|
|
|
|
.unwrap();
|
|
|
|
|
2018-11-19 06:48:20 +01:00
|
|
|
let response = srv.block_on(request.send(&mut connector)).unwrap();
|
2018-11-14 07:53:30 +01:00
|
|
|
assert!(response.status().is_success());
|
|
|
|
}
|