1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-12-01 02:54:36 +01:00
actix-web/tests/test_client.rs

105 lines
4.2 KiB
Rust
Raw Normal View History

2018-11-14 07:53:30 +01:00
extern crate actix;
extern crate actix_http;
extern crate actix_net;
extern crate bytes;
extern crate futures;
use actix_net::service::NewServiceExt;
use bytes::Bytes;
2018-11-19 06:48:20 +01:00
use futures::future::{self, ok};
2018-11-14 07:53:30 +01:00
use actix_http::HttpMessage;
2018-11-19 06:48:20 +01:00
use actix_http::{client, h1, test::TestServer, Request, Response};
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";
#[test]
fn test_h1_v2() {
2018-11-19 06:48:20 +01:00
let mut srv = TestServer::with_factory(move || {
h1::H1Service::build()
.finish(|_| future::ok::<_, ()>(Response::Ok().body(STR)))
.map(|_| ())
2018-11-14 07:53:30 +01:00
});
2018-11-19 06:48:20 +01:00
let mut connector = srv.new_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"));
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());
// read response
2018-11-19 06:48:20 +01:00
let bytes = srv.block_on(response.body()).unwrap();
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();
let response = srv.block_on(request.send(&mut connector)).unwrap();
2018-11-14 07:53:30 +01:00
assert!(response.status().is_success());
// read response
2018-11-19 06:48:20 +01:00
let bytes = srv.block_on(response.body()).unwrap();
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
2018-11-14 07:53:30 +01:00
}
#[test]
fn test_connection_close() {
2018-11-19 06:48:20 +01:00
let mut srv = TestServer::with_factory(move || {
h1::H1Service::build()
.finish(|_| ok::<_, ()>(Response::Ok().body(STR)))
.map(|_| ())
2018-11-14 07:53:30 +01:00
});
2018-11-19 06:48:20 +01:00
let mut connector = srv.new_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() {
2018-11-19 06:48:20 +01:00
let mut srv = TestServer::with_factory(move || {
h1::H1Service::build()
.finish(|req: Request| {
if req.uri().query().unwrap().contains("qp=") {
ok::<_, ()>(Response::Ok().finish())
} else {
ok::<_, ()>(Response::BadRequest().finish())
}
}).map(|_| ())
2018-11-14 07:53:30 +01:00
});
2018-11-19 06:48:20 +01:00
let mut connector = srv.new_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());
}