2019-11-14 13:38:24 +01:00
|
|
|
use std::io;
|
|
|
|
|
2019-03-14 06:51:31 +01:00
|
|
|
use actix_codec::{BytesCodec, Framed};
|
2019-12-02 06:43:52 +01:00
|
|
|
use actix_rt::net::TcpStream;
|
2019-11-14 13:38:24 +01:00
|
|
|
use actix_service::{service_fn, Service, ServiceFactory};
|
2019-11-25 16:49:11 +01:00
|
|
|
use actix_testing::TestServer;
|
2019-03-14 06:51:31 +01:00
|
|
|
use bytes::Bytes;
|
2019-11-14 13:38:24 +01:00
|
|
|
use futures::SinkExt;
|
2019-03-14 06:51:31 +01:00
|
|
|
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
use actix_connect::Connect;
|
2019-03-14 06:51:31 +01:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
#[cfg(feature = "openssl")]
|
2019-11-25 16:49:11 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_string() {
|
2019-09-25 06:00:54 +02:00
|
|
|
let srv = TestServer::with(|| {
|
2019-12-02 06:43:52 +01:00
|
|
|
service_fn(|io: TcpStream| {
|
2019-11-14 13:38:24 +01:00
|
|
|
async {
|
2019-12-02 06:43:52 +01:00
|
|
|
let mut framed = Framed::new(io, BytesCodec);
|
2019-11-14 13:38:24 +01:00
|
|
|
framed.send(Bytes::from_static(b"test")).await?;
|
|
|
|
Ok::<_, io::Error>(())
|
|
|
|
}
|
2019-03-14 06:51:31 +01:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
let mut conn = actix_connect::default_connector();
|
2019-03-14 06:51:31 +01:00
|
|
|
let addr = format!("localhost:{}", srv.port());
|
2019-11-25 16:49:11 +01:00
|
|
|
let con = conn.call(addr.into()).await.unwrap();
|
2019-03-14 06:51:31 +01:00
|
|
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
#[cfg(feature = "rustls")]
|
2019-11-25 16:49:11 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_rustls_string() {
|
2019-09-25 06:00:54 +02:00
|
|
|
let srv = TestServer::with(|| {
|
2019-12-02 06:43:52 +01:00
|
|
|
service_fn(|io: TcpStream| {
|
2019-11-14 13:38:24 +01:00
|
|
|
async {
|
2019-12-02 06:43:52 +01:00
|
|
|
let mut framed = Framed::new(io, BytesCodec);
|
2019-11-14 13:38:24 +01:00
|
|
|
framed.send(Bytes::from_static(b"test")).await?;
|
|
|
|
Ok::<_, io::Error>(())
|
|
|
|
}
|
2019-07-24 10:14:26 +02:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
let mut conn = actix_connect::default_connector();
|
2019-07-24 10:14:26 +02:00
|
|
|
let addr = format!("localhost:{}", srv.port());
|
2019-11-25 16:49:11 +01:00
|
|
|
let con = conn.call(addr.into()).await.unwrap();
|
2019-07-24 10:14:26 +02:00
|
|
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
|
|
|
}
|
2019-10-04 07:30:13 +02:00
|
|
|
|
2019-11-26 03:26:22 +01:00
|
|
|
#[actix_rt::test]
|
2019-11-14 13:38:24 +01:00
|
|
|
async fn test_static_str() {
|
2019-09-25 05:51:28 +02:00
|
|
|
let srv = TestServer::with(|| {
|
2019-12-02 06:43:52 +01:00
|
|
|
service_fn(|io: TcpStream| {
|
2019-11-14 13:38:24 +01:00
|
|
|
async {
|
2019-12-02 06:43:52 +01:00
|
|
|
let mut framed = Framed::new(io, BytesCodec);
|
2019-11-14 13:38:24 +01:00
|
|
|
framed.send(Bytes::from_static(b"test")).await?;
|
|
|
|
Ok::<_, io::Error>(())
|
|
|
|
}
|
2019-03-14 06:51:31 +01:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
let resolver = actix_connect::start_default_resolver();
|
|
|
|
let mut conn = actix_connect::new_connector(resolver.clone());
|
2019-09-25 05:51:28 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
let con = conn.call(Connect::with("10", srv.addr())).await.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-11-14 13:38:24 +01:00
|
|
|
let mut conn = actix_connect::new_connector(resolver);
|
|
|
|
let con = conn.call(connect).await;
|
2019-03-15 19:37:51 +01:00
|
|
|
assert!(con.is_err());
|
2019-03-14 06:51:31 +01:00
|
|
|
}
|
|
|
|
|
2019-11-25 16:49:11 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_new_service() {
|
2019-09-25 05:51:28 +02:00
|
|
|
let srv = TestServer::with(|| {
|
2019-12-02 06:43:52 +01:00
|
|
|
service_fn(|io: TcpStream| {
|
2019-11-14 13:38:24 +01:00
|
|
|
async {
|
2019-12-02 06:43:52 +01:00
|
|
|
let mut framed = Framed::new(io, BytesCodec);
|
2019-11-14 13:38:24 +01:00
|
|
|
framed.send(Bytes::from_static(b"test")).await?;
|
|
|
|
Ok::<_, io::Error>(())
|
|
|
|
}
|
2019-03-14 06:51:31 +01:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2019-11-25 16:49:11 +01:00
|
|
|
let resolver =
|
|
|
|
actix_connect::start_resolver(ResolverConfig::default(), ResolverOpts::default());
|
|
|
|
|
|
|
|
let factory = actix_connect::new_connector_factory(resolver);
|
2019-09-25 05:51:28 +02:00
|
|
|
|
2019-12-02 16:27:48 +01:00
|
|
|
let mut conn = factory.new_service(()).await.unwrap();
|
2019-11-25 16:49:11 +01:00
|
|
|
let con = conn.call(Connect::with("10", srv.addr())).await.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-11-14 13:38:24 +01:00
|
|
|
#[cfg(feature = "openssl")]
|
2019-11-25 16:49:11 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_uri() {
|
2019-11-14 13:38:24 +01:00
|
|
|
use http::HttpTryFrom;
|
|
|
|
|
2019-09-25 06:00:54 +02:00
|
|
|
let srv = TestServer::with(|| {
|
2019-12-02 06:43:52 +01:00
|
|
|
service_fn(|io: TcpStream| {
|
2019-11-14 13:38:24 +01:00
|
|
|
async {
|
2019-12-02 06:43:52 +01:00
|
|
|
let mut framed = Framed::new(io, BytesCodec);
|
2019-11-14 13:38:24 +01:00
|
|
|
framed.send(Bytes::from_static(b"test")).await?;
|
|
|
|
Ok::<_, io::Error>(())
|
|
|
|
}
|
2019-03-14 19:15:32 +01:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
let mut conn = actix_connect::default_connector();
|
|
|
|
let addr = http::Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap();
|
2019-11-25 16:49:11 +01:00
|
|
|
let con = conn.call(addr.into()).await.unwrap();
|
2019-03-14 19:15:32 +01:00
|
|
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
|
|
|
}
|
2019-07-24 10:14:26 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
#[cfg(feature = "rustls")]
|
2019-11-25 16:49:11 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_rustls_uri() {
|
2019-11-14 13:38:24 +01:00
|
|
|
use http::HttpTryFrom;
|
|
|
|
|
2019-09-25 06:00:54 +02:00
|
|
|
let srv = TestServer::with(|| {
|
2019-12-02 06:43:52 +01:00
|
|
|
service_fn(|io: TcpStream| {
|
2019-11-14 13:38:24 +01:00
|
|
|
async {
|
2019-12-02 06:43:52 +01:00
|
|
|
let mut framed = Framed::new(io, BytesCodec);
|
2019-11-14 13:38:24 +01:00
|
|
|
framed.send(Bytes::from_static(b"test")).await?;
|
|
|
|
Ok::<_, io::Error>(())
|
|
|
|
}
|
2019-07-24 10:14:26 +02:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
let mut conn = actix_connect::default_connector();
|
|
|
|
let addr = http::Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap();
|
2019-11-25 16:49:11 +01:00
|
|
|
let con = conn.call(addr.into()).await.unwrap();
|
2019-07-24 10:14:26 +02:00
|
|
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
|
|
|
}
|