2019-03-14 06:51:31 +01:00
|
|
|
use actix_codec::{BytesCodec, Framed};
|
|
|
|
use actix_server_config::Io;
|
2019-05-12 15:03:50 +02:00
|
|
|
use actix_service::{service_fn, NewService, Service};
|
2019-09-25 05:51:28 +02:00
|
|
|
use actix_testing::{self as test, TestServer};
|
2019-03-14 06:51:31 +01:00
|
|
|
use bytes::Bytes;
|
|
|
|
use futures::{future::lazy, Future, Sink};
|
2019-09-25 06:00:54 +02:00
|
|
|
use http::{HttpTryFrom, Uri};
|
2019-03-14 06:51:31 +01:00
|
|
|
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
|
|
|
|
2019-09-25 06:00:54 +02:00
|
|
|
use actix_connect::{default_connector, Connect};
|
2019-03-14 06:51:31 +01:00
|
|
|
|
2019-04-12 21:28:18 +02:00
|
|
|
#[cfg(feature = "ssl")]
|
2019-03-14 06:51:31 +01:00
|
|
|
#[test]
|
|
|
|
fn test_string() {
|
2019-09-25 06:00:54 +02:00
|
|
|
let srv = TestServer::with(|| {
|
2019-05-12 15:03:50 +02:00
|
|
|
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
2019-03-14 06:51:31 +01:00
|
|
|
Framed::new(io.into_parts().0, BytesCodec)
|
|
|
|
.send(Bytes::from_static(b"test"))
|
|
|
|
.then(|_| Ok::<_, ()>(()))
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2019-04-12 21:28:18 +02:00
|
|
|
let mut conn = default_connector();
|
2019-03-14 06:51:31 +01:00
|
|
|
let addr = format!("localhost:{}", srv.port());
|
2019-09-25 06:00:54 +02:00
|
|
|
let con = test::call_service(&mut conn, addr.into());
|
2019-03-14 06:51:31 +01:00
|
|
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
|
|
|
}
|
|
|
|
|
2019-07-24 10:14:26 +02:00
|
|
|
#[cfg(feature = "rust-tls")]
|
|
|
|
#[test]
|
|
|
|
fn test_rustls_string() {
|
2019-09-25 06:00:54 +02:00
|
|
|
let srv = TestServer::with(|| {
|
2019-07-24 10:14:26 +02:00
|
|
|
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
|
|
|
Framed::new(io.into_parts().0, BytesCodec)
|
|
|
|
.send(Bytes::from_static(b"test"))
|
|
|
|
.then(|_| Ok::<_, ()>(()))
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut conn = default_connector();
|
|
|
|
let addr = format!("localhost:{}", srv.port());
|
2019-09-25 06:00:54 +02:00
|
|
|
let con = test::call_service(&mut conn, addr.into());
|
2019-07-24 10:14:26 +02:00
|
|
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
|
|
|
}
|
2019-03-14 06:51:31 +01:00
|
|
|
#[test]
|
|
|
|
fn test_static_str() {
|
2019-09-25 05:51:28 +02:00
|
|
|
let srv = TestServer::with(|| {
|
2019-05-12 15:03:50 +02:00
|
|
|
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
2019-03-14 06:51:31 +01:00
|
|
|
Framed::new(io.into_parts().0, BytesCodec)
|
|
|
|
.send(Bytes::from_static(b"test"))
|
|
|
|
.then(|_| Ok::<_, ()>(()))
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2019-09-25 05:51:28 +02:00
|
|
|
let resolver = test::block_on(lazy(
|
|
|
|
|| Ok::<_, ()>(actix_connect::start_default_resolver()),
|
|
|
|
))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut conn = test::block_on(lazy(|| {
|
|
|
|
Ok::<_, ()>(actix_connect::new_connector(resolver.clone()))
|
|
|
|
}))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let con = test::block_on(conn.call(Connect::with("10", srv.addr()))).unwrap();
|
2019-03-14 06:51:31 +01:00
|
|
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
2019-03-15 19:37:51 +01:00
|
|
|
|
|
|
|
let connect = Connect::new(srv.host().to_owned());
|
2019-09-25 05:51:28 +02:00
|
|
|
let mut conn =
|
|
|
|
test::block_on(lazy(|| Ok::<_, ()>(actix_connect::new_connector(resolver)))).unwrap();
|
|
|
|
let con = test::block_on(conn.call(connect));
|
2019-03-15 19:37:51 +01:00
|
|
|
assert!(con.is_err());
|
2019-03-14 06:51:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_service() {
|
2019-09-25 05:51:28 +02:00
|
|
|
let srv = TestServer::with(|| {
|
2019-05-12 15:03:50 +02:00
|
|
|
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
2019-03-14 06:51:31 +01:00
|
|
|
Framed::new(io.into_parts().0, BytesCodec)
|
|
|
|
.send(Bytes::from_static(b"test"))
|
|
|
|
.then(|_| Ok::<_, ()>(()))
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2019-09-25 05:51:28 +02:00
|
|
|
let resolver = test::block_on(lazy(|| {
|
|
|
|
Ok::<_, ()>(actix_connect::start_resolver(
|
|
|
|
ResolverConfig::default(),
|
|
|
|
ResolverOpts::default(),
|
|
|
|
))
|
|
|
|
}))
|
|
|
|
.unwrap();
|
|
|
|
let factory = test::block_on(lazy(|| {
|
|
|
|
Ok::<_, ()>(actix_connect::new_connector_factory(resolver))
|
|
|
|
}))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut conn = test::block_on(factory.new_service(&())).unwrap();
|
|
|
|
let con = test::block_on(conn.call(Connect::with("10", srv.addr()))).unwrap();
|
2019-03-14 06:51:31 +01:00
|
|
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
|
|
|
}
|
2019-03-14 19:15:32 +01:00
|
|
|
|
2019-04-12 21:28:18 +02:00
|
|
|
#[cfg(feature = "ssl")]
|
2019-03-14 19:15:32 +01:00
|
|
|
#[test]
|
|
|
|
fn test_uri() {
|
2019-09-25 06:00:54 +02:00
|
|
|
let srv = TestServer::with(|| {
|
2019-05-12 15:03:50 +02:00
|
|
|
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
2019-03-14 19:15:32 +01:00
|
|
|
Framed::new(io.into_parts().0, BytesCodec)
|
|
|
|
.send(Bytes::from_static(b"test"))
|
|
|
|
.then(|_| Ok::<_, ()>(()))
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2019-04-12 21:28:18 +02:00
|
|
|
let mut conn = default_connector();
|
2019-03-14 19:15:32 +01:00
|
|
|
let addr = Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap();
|
2019-09-25 06:00:54 +02:00
|
|
|
let con = test::call_service(&mut conn, addr.into());
|
2019-03-14 19:15:32 +01:00
|
|
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
|
|
|
}
|
2019-07-24 10:14:26 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "rust-tls")]
|
|
|
|
#[test]
|
|
|
|
fn test_rustls_uri() {
|
2019-09-25 06:00:54 +02:00
|
|
|
let srv = TestServer::with(|| {
|
2019-07-24 10:14:26 +02:00
|
|
|
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
|
|
|
Framed::new(io.into_parts().0, BytesCodec)
|
|
|
|
.send(Bytes::from_static(b"test"))
|
|
|
|
.then(|_| Ok::<_, ()>(()))
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut conn = default_connector();
|
|
|
|
let addr = Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap();
|
2019-09-25 06:00:54 +02:00
|
|
|
let con = test::call_service(&mut conn, addr.into());
|
2019-07-24 10:14:26 +02:00
|
|
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
|
|
|
}
|