2021-11-22 18:16:56 +00:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
io::{Read, Write},
|
|
|
|
net::{IpAddr, Ipv4Addr},
|
|
|
|
sync::{
|
|
|
|
atomic::{AtomicUsize, Ordering},
|
|
|
|
Arc,
|
|
|
|
},
|
|
|
|
time::Duration,
|
|
|
|
};
|
2019-03-27 03:45:00 +00:00
|
|
|
|
2021-04-01 14:26:13 +00:00
|
|
|
use actix_utils::future::ok;
|
2019-03-27 03:45:00 +00:00
|
|
|
use bytes::Bytes;
|
2021-04-09 17:07:10 +00:00
|
|
|
use cookie::Cookie;
|
2021-04-01 14:26:13 +00:00
|
|
|
use futures_util::stream;
|
2019-03-27 03:45:00 +00:00
|
|
|
use rand::Rng;
|
|
|
|
|
2021-11-22 18:16:56 +00:00
|
|
|
#[cfg(feature = "compress-brotli")]
|
|
|
|
use brotli2::write::BrotliEncoder;
|
|
|
|
|
|
|
|
#[cfg(feature = "compress-gzip")]
|
|
|
|
use flate2::{read::GzDecoder, write::GzEncoder, Compression};
|
|
|
|
|
2021-01-17 05:19:32 +00:00
|
|
|
use actix_http::{
|
|
|
|
http::{self, StatusCode},
|
|
|
|
HttpService,
|
|
|
|
};
|
2019-12-12 17:08:38 +00:00
|
|
|
use actix_http_test::test_server;
|
2021-04-16 19:28:21 +00:00
|
|
|
use actix_service::{fn_service, map_config, ServiceFactoryExt as _};
|
2019-12-12 17:08:38 +00:00
|
|
|
use actix_web::{
|
2021-01-17 05:19:32 +00:00
|
|
|
dev::{AppConfig, BodyEncoding},
|
2021-04-09 17:07:10 +00:00
|
|
|
http::header,
|
|
|
|
web, App, Error, HttpRequest, HttpResponse,
|
2019-12-12 17:08:38 +00:00
|
|
|
};
|
2021-02-17 11:55:11 +00:00
|
|
|
use awc::error::{JsonPayloadError, PayloadError, SendRequestError};
|
2019-03-27 03:45:00 +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";
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_simple() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2021-02-11 23:03:17 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
|
2019-12-12 17:08:38 +00:00
|
|
|
});
|
2019-03-27 03:45:00 +00:00
|
|
|
|
2021-01-15 02:11:10 +00:00
|
|
|
let request = srv.get("/").insert_header(("x-test", "111")).send();
|
2019-11-26 05:25:50 +00:00
|
|
|
let mut response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-03-27 03:45:00 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2019-03-27 03:45:00 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let mut response = srv.post("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-03-27 03:45:00 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2019-04-24 18:27:57 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// camel case
|
|
|
|
let response = srv.post("/").camel_case().send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-03-27 03:45:00 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_json() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2019-12-12 17:08:38 +00:00
|
|
|
App::new().service(
|
2019-11-26 05:25:50 +00:00
|
|
|
web::resource("/").route(web::to(|_: web::Json<String>| HttpResponse::Ok())),
|
2019-12-12 17:08:38 +00:00
|
|
|
)
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let request = srv
|
|
|
|
.get("/")
|
2021-01-15 02:11:10 +00:00
|
|
|
.insert_header(("x-test", "111"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.send_json(&"TEST".to_string());
|
|
|
|
let response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-04-19 04:28:23 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_form() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2019-12-12 17:08:38 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(
|
2019-11-26 05:25:50 +00:00
|
|
|
|_: web::Form<HashMap<String, String>>| HttpResponse::Ok(),
|
2019-12-12 17:08:38 +00:00
|
|
|
)))
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
2019-11-20 17:33:22 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let mut data = HashMap::new();
|
|
|
|
let _ = data.insert("key".to_string(), "TEST".to_string());
|
2019-11-20 17:33:22 +00:00
|
|
|
|
2021-01-15 02:11:10 +00:00
|
|
|
let request = srv
|
|
|
|
.get("/")
|
|
|
|
.append_header(("x-test", "111"))
|
|
|
|
.send_form(&data);
|
2019-11-26 05:25:50 +00:00
|
|
|
let response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-04-19 04:28:23 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_timeout() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2020-03-08 07:42:45 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(|| async {
|
2021-01-03 23:47:04 +00:00
|
|
|
actix_rt::time::sleep(Duration::from_millis(200)).await;
|
2020-03-08 07:42:45 +00:00
|
|
|
Ok::<_, Error>(HttpResponse::Ok().body(STR))
|
2019-12-12 17:08:38 +00:00
|
|
|
})))
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let connector = awc::Connector::new()
|
2021-02-07 01:00:40 +00:00
|
|
|
.connector(actix_tls::connect::default_connector())
|
2021-02-18 12:30:09 +00:00
|
|
|
.timeout(Duration::from_secs(15));
|
2019-11-26 05:25:50 +00:00
|
|
|
|
2020-09-11 08:24:39 +00:00
|
|
|
let client = awc::Client::builder()
|
2019-11-26 05:25:50 +00:00
|
|
|
.connector(connector)
|
|
|
|
.timeout(Duration::from_millis(50))
|
|
|
|
.finish();
|
|
|
|
|
|
|
|
let request = client.get(srv.url("/")).send();
|
|
|
|
match request.await {
|
2021-01-04 01:01:35 +00:00
|
|
|
Err(SendRequestError::Timeout) => {}
|
2019-11-26 05:25:50 +00:00
|
|
|
_ => panic!(),
|
|
|
|
}
|
2019-03-29 05:33:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_timeout_override() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2020-03-08 07:42:45 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(|| async {
|
2021-01-03 23:47:04 +00:00
|
|
|
actix_rt::time::sleep(Duration::from_millis(200)).await;
|
2020-03-08 07:42:45 +00:00
|
|
|
Ok::<_, Error>(HttpResponse::Ok().body(STR))
|
2019-12-12 17:08:38 +00:00
|
|
|
})))
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
2020-09-11 08:24:39 +00:00
|
|
|
let client = awc::Client::builder()
|
2019-11-26 05:25:50 +00:00
|
|
|
.timeout(Duration::from_millis(50000))
|
|
|
|
.finish();
|
|
|
|
let request = client
|
|
|
|
.get(srv.url("/"))
|
|
|
|
.timeout(Duration::from_millis(50))
|
|
|
|
.send();
|
|
|
|
match request.await {
|
2021-01-04 01:01:35 +00:00
|
|
|
Err(SendRequestError::Timeout) => {}
|
2019-11-26 05:25:50 +00:00
|
|
|
_ => panic!(),
|
|
|
|
}
|
2019-03-29 21:07:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 11:55:11 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_response_timeout() {
|
2021-04-01 14:26:13 +00:00
|
|
|
use futures_util::stream::{once, StreamExt as _};
|
2021-02-17 11:55:11 +00:00
|
|
|
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2021-02-17 11:55:11 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(|| async {
|
|
|
|
Ok::<_, Error>(
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type("application/json")
|
|
|
|
.streaming(Box::pin(once(async {
|
|
|
|
actix_rt::time::sleep(Duration::from_millis(200)).await;
|
|
|
|
Ok::<_, Error>(Bytes::from(STR))
|
|
|
|
}))),
|
|
|
|
)
|
|
|
|
})))
|
|
|
|
});
|
|
|
|
|
|
|
|
let client = awc::Client::new();
|
|
|
|
|
|
|
|
let res = client
|
|
|
|
.get(srv.url("/"))
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.timeout(Duration::from_millis(500))
|
|
|
|
.body()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(std::str::from_utf8(res.as_ref()).unwrap(), STR);
|
|
|
|
|
|
|
|
let res = client
|
|
|
|
.get(srv.url("/"))
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.timeout(Duration::from_millis(100))
|
|
|
|
.next()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
match res {
|
|
|
|
Err(PayloadError::Io(e)) => assert_eq!(e.kind(), std::io::ErrorKind::TimedOut),
|
|
|
|
_ => panic!("Response error type is not matched"),
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = client
|
|
|
|
.get(srv.url("/"))
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.timeout(Duration::from_millis(100))
|
|
|
|
.body()
|
|
|
|
.await;
|
|
|
|
match res {
|
|
|
|
Err(PayloadError::Io(e)) => assert_eq!(e.kind(), std::io::ErrorKind::TimedOut),
|
|
|
|
_ => panic!("Response error type is not matched"),
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = client
|
|
|
|
.get(srv.url("/"))
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.timeout(Duration::from_millis(100))
|
|
|
|
.json::<HashMap<String, String>>()
|
|
|
|
.await;
|
|
|
|
match res {
|
|
|
|
Err(JsonPayloadError::Payload(PayloadError::Io(e))) => {
|
|
|
|
assert_eq!(e.kind(), std::io::ErrorKind::TimedOut)
|
|
|
|
}
|
|
|
|
_ => panic!("Response error type is not matched"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_connection_reuse() {
|
|
|
|
let num = Arc::new(AtomicUsize::new(0));
|
|
|
|
let num2 = num.clone();
|
|
|
|
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-11-26 05:25:50 +00:00
|
|
|
let num2 = num2.clone();
|
2021-04-16 19:28:21 +00:00
|
|
|
fn_service(move |io| {
|
2019-11-26 05:25:50 +00:00
|
|
|
num2.fetch_add(1, Ordering::Relaxed);
|
|
|
|
ok(io)
|
|
|
|
})
|
2019-12-02 11:33:11 +00:00
|
|
|
.and_then(
|
2019-12-20 11:27:32 +00:00
|
|
|
HttpService::new(map_config(
|
2020-07-21 23:28:33 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(HttpResponse::Ok))),
|
2019-12-20 11:27:32 +00:00
|
|
|
|_| AppConfig::default(),
|
|
|
|
))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp(),
|
|
|
|
)
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let client = awc::Client::default();
|
|
|
|
|
|
|
|
// req 1
|
|
|
|
let request = client.get(srv.url("/")).send();
|
|
|
|
let response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// req 2
|
|
|
|
let req = client.post(srv.url("/"));
|
|
|
|
let response = req.send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// one connection
|
|
|
|
assert_eq!(num.load(Ordering::Relaxed), 1);
|
2019-04-23 21:57:03 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_connection_force_close() {
|
|
|
|
let num = Arc::new(AtomicUsize::new(0));
|
|
|
|
let num2 = num.clone();
|
|
|
|
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-11-26 05:25:50 +00:00
|
|
|
let num2 = num2.clone();
|
2021-04-16 19:28:21 +00:00
|
|
|
fn_service(move |io| {
|
2019-11-26 05:25:50 +00:00
|
|
|
num2.fetch_add(1, Ordering::Relaxed);
|
|
|
|
ok(io)
|
|
|
|
})
|
2019-12-02 11:33:11 +00:00
|
|
|
.and_then(
|
2019-12-20 11:27:32 +00:00
|
|
|
HttpService::new(map_config(
|
2020-07-21 23:28:33 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(HttpResponse::Ok))),
|
2019-12-20 11:27:32 +00:00
|
|
|
|_| AppConfig::default(),
|
|
|
|
))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp(),
|
|
|
|
)
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let client = awc::Client::default();
|
|
|
|
|
|
|
|
// req 1
|
|
|
|
let request = client.get(srv.url("/")).force_close().send();
|
|
|
|
let response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// req 2
|
|
|
|
let req = client.post(srv.url("/")).force_close();
|
|
|
|
let response = req.send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// two connection
|
|
|
|
assert_eq!(num.load(Ordering::Relaxed), 2);
|
2019-04-23 21:57:03 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_connection_server_close() {
|
|
|
|
let num = Arc::new(AtomicUsize::new(0));
|
|
|
|
let num2 = num.clone();
|
|
|
|
|
2019-12-22 12:39:25 +00:00
|
|
|
let srv = test_server(move || {
|
|
|
|
let num2 = num2.clone();
|
2021-04-16 19:28:21 +00:00
|
|
|
fn_service(move |io| {
|
2019-12-22 12:39:25 +00:00
|
|
|
num2.fetch_add(1, Ordering::Relaxed);
|
|
|
|
ok(io)
|
|
|
|
})
|
|
|
|
.and_then(
|
|
|
|
HttpService::new(map_config(
|
|
|
|
App::new().service(
|
|
|
|
web::resource("/")
|
|
|
|
.route(web::to(|| HttpResponse::Ok().force_close().finish())),
|
|
|
|
),
|
|
|
|
|_| AppConfig::default(),
|
|
|
|
))
|
|
|
|
.tcp(),
|
|
|
|
)
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let client = awc::Client::default();
|
|
|
|
|
|
|
|
// req 1
|
|
|
|
let request = client.get(srv.url("/")).send();
|
|
|
|
let response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// req 2
|
|
|
|
let req = client.post(srv.url("/"));
|
|
|
|
let response = req.send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// two connection
|
|
|
|
assert_eq!(num.load(Ordering::Relaxed), 2);
|
2019-04-23 21:57:03 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_connection_wait_queue() {
|
|
|
|
let num = Arc::new(AtomicUsize::new(0));
|
|
|
|
let num2 = num.clone();
|
|
|
|
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-11-26 05:25:50 +00:00
|
|
|
let num2 = num2.clone();
|
2021-04-16 19:28:21 +00:00
|
|
|
fn_service(move |io| {
|
2019-11-26 05:25:50 +00:00
|
|
|
num2.fetch_add(1, Ordering::Relaxed);
|
|
|
|
ok(io)
|
|
|
|
})
|
2019-12-02 11:33:11 +00:00
|
|
|
.and_then(
|
2019-12-20 11:27:32 +00:00
|
|
|
HttpService::new(map_config(
|
2019-12-22 12:39:25 +00:00
|
|
|
App::new().service(
|
|
|
|
web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))),
|
|
|
|
),
|
2019-12-20 11:27:32 +00:00
|
|
|
|_| AppConfig::default(),
|
2019-12-02 11:33:11 +00:00
|
|
|
))
|
|
|
|
.tcp(),
|
|
|
|
)
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
2020-09-11 08:24:39 +00:00
|
|
|
let client = awc::Client::builder()
|
2021-02-18 12:30:09 +00:00
|
|
|
.connector(awc::Connector::new().limit(1))
|
2019-11-26 05:25:50 +00:00
|
|
|
.finish();
|
|
|
|
|
|
|
|
// req 1
|
|
|
|
let request = client.get(srv.url("/")).send();
|
|
|
|
let mut response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// req 2
|
|
|
|
let req2 = client.post(srv.url("/"));
|
|
|
|
let req2_fut = req2.send();
|
|
|
|
|
|
|
|
// read response 1
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
|
|
|
|
|
|
|
// req 2
|
|
|
|
let response = req2_fut.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// two connection
|
|
|
|
assert_eq!(num.load(Ordering::Relaxed), 1);
|
2019-04-23 21:57:03 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_connection_wait_queue_force_close() {
|
|
|
|
let num = Arc::new(AtomicUsize::new(0));
|
|
|
|
let num2 = num.clone();
|
|
|
|
|
2019-12-22 12:39:25 +00:00
|
|
|
let srv = test_server(move || {
|
|
|
|
let num2 = num2.clone();
|
2021-04-16 19:28:21 +00:00
|
|
|
fn_service(move |io| {
|
2019-12-22 12:39:25 +00:00
|
|
|
num2.fetch_add(1, Ordering::Relaxed);
|
|
|
|
ok(io)
|
|
|
|
})
|
|
|
|
.and_then(
|
|
|
|
HttpService::new(map_config(
|
|
|
|
App::new().service(
|
|
|
|
web::resource("/")
|
|
|
|
.route(web::to(|| HttpResponse::Ok().force_close().body(STR))),
|
|
|
|
),
|
|
|
|
|_| AppConfig::default(),
|
|
|
|
))
|
|
|
|
.tcp(),
|
|
|
|
)
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
2020-09-11 08:24:39 +00:00
|
|
|
let client = awc::Client::builder()
|
2021-02-18 12:30:09 +00:00
|
|
|
.connector(awc::Connector::new().limit(1))
|
2019-11-26 05:25:50 +00:00
|
|
|
.finish();
|
|
|
|
|
|
|
|
// req 1
|
|
|
|
let request = client.get(srv.url("/")).send();
|
|
|
|
let mut response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// req 2
|
|
|
|
let req2 = client.post(srv.url("/"));
|
|
|
|
let req2_fut = req2.send();
|
|
|
|
|
|
|
|
// read response 1
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
|
|
|
|
|
|
|
// req 2
|
|
|
|
let response = req2_fut.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// two connection
|
|
|
|
assert_eq!(num.load(Ordering::Relaxed), 2);
|
2019-04-15 03:45:44 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_with_query_parameter() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2019-12-12 17:08:38 +00:00
|
|
|
App::new().service(web::resource("/").to(|req: HttpRequest| {
|
|
|
|
if req.query_string().contains("qp") {
|
|
|
|
HttpResponse::Ok()
|
|
|
|
} else {
|
|
|
|
HttpResponse::BadRequest()
|
|
|
|
}
|
|
|
|
}))
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let res = awc::Client::new()
|
|
|
|
.get(srv.url("/?qp=5"))
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(res.status().is_success());
|
2019-04-15 03:45:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:16:56 +00:00
|
|
|
#[cfg(feature = "compress-gzip")]
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_no_decompress() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2019-12-12 17:08:38 +00:00
|
|
|
App::new()
|
2021-11-22 18:16:56 +00:00
|
|
|
.wrap(actix_web::middleware::Compress::default())
|
2019-12-12 17:08:38 +00:00
|
|
|
.service(web::resource("/").route(web::to(|| {
|
2019-11-26 05:25:50 +00:00
|
|
|
let mut res = HttpResponse::Ok().body(STR);
|
|
|
|
res.encoding(header::ContentEncoding::Gzip);
|
|
|
|
res
|
2019-12-12 17:08:38 +00:00
|
|
|
})))
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let mut res = awc::Client::new()
|
|
|
|
.get(srv.url("/"))
|
|
|
|
.no_decompress()
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(res.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = res.body().await.unwrap();
|
|
|
|
|
|
|
|
let mut e = GzDecoder::new(&bytes[..]);
|
|
|
|
let mut dec = Vec::new();
|
|
|
|
e.read_to_end(&mut dec).unwrap();
|
|
|
|
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
|
|
|
|
|
|
|
// POST
|
|
|
|
let mut res = awc::Client::new()
|
|
|
|
.post(srv.url("/"))
|
|
|
|
.no_decompress()
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(res.status().is_success());
|
|
|
|
|
|
|
|
let bytes = res.body().await.unwrap();
|
|
|
|
let mut e = GzDecoder::new(&bytes[..]);
|
|
|
|
let mut dec = Vec::new();
|
|
|
|
e.read_to_end(&mut dec).unwrap();
|
|
|
|
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
2019-04-15 03:45:44 +00:00
|
|
|
}
|
2019-03-27 03:45:00 +00:00
|
|
|
|
2021-11-22 18:16:56 +00:00
|
|
|
#[cfg(feature = "compress-gzip")]
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_client_gzip_encoding() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2019-12-12 17:08:38 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(|| {
|
2019-11-26 05:25:50 +00:00
|
|
|
let mut e = GzEncoder::new(Vec::new(), Compression::default());
|
|
|
|
e.write_all(STR.as_ref()).unwrap();
|
|
|
|
let data = e.finish().unwrap();
|
|
|
|
|
|
|
|
HttpResponse::Ok()
|
2021-01-15 02:11:10 +00:00
|
|
|
.insert_header(("content-encoding", "gzip"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.body(data)
|
2019-12-12 17:08:38 +00:00
|
|
|
})))
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// client request
|
|
|
|
let mut response = srv.post("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2019-03-27 03:45:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:16:56 +00:00
|
|
|
#[cfg(feature = "compress-gzip")]
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_client_gzip_encoding_large() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2019-12-12 17:08:38 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(|| {
|
2019-11-26 05:25:50 +00:00
|
|
|
let mut e = GzEncoder::new(Vec::new(), Compression::default());
|
|
|
|
e.write_all(STR.repeat(10).as_ref()).unwrap();
|
|
|
|
let data = e.finish().unwrap();
|
|
|
|
|
|
|
|
HttpResponse::Ok()
|
2021-01-15 02:11:10 +00:00
|
|
|
.insert_header(("content-encoding", "gzip"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.body(data)
|
2019-12-12 17:08:38 +00:00
|
|
|
})))
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// client request
|
|
|
|
let mut response = srv.post("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from(STR.repeat(10)));
|
2019-03-27 03:45:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:16:56 +00:00
|
|
|
#[cfg(feature = "compress-gzip")]
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_client_gzip_encoding_large_random() {
|
|
|
|
let data = rand::thread_rng()
|
|
|
|
.sample_iter(&rand::distributions::Alphanumeric)
|
|
|
|
.take(100_000)
|
2020-12-22 23:45:31 +00:00
|
|
|
.map(char::from)
|
2019-11-26 05:25:50 +00:00
|
|
|
.collect::<String>();
|
|
|
|
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2019-12-12 17:08:38 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(|data: Bytes| {
|
|
|
|
let mut e = GzEncoder::new(Vec::new(), Compression::default());
|
|
|
|
e.write_all(&data).unwrap();
|
|
|
|
let data = e.finish().unwrap();
|
|
|
|
HttpResponse::Ok()
|
2021-01-15 02:11:10 +00:00
|
|
|
.insert_header(("content-encoding", "gzip"))
|
2019-12-12 17:08:38 +00:00
|
|
|
.body(data)
|
|
|
|
})))
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// client request
|
|
|
|
let mut response = srv.post("/").send_body(data.clone()).await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from(data));
|
2019-03-27 03:45:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:16:56 +00:00
|
|
|
#[cfg(feature = "compress-brotli")]
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_client_brotli_encoding() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2019-12-12 17:08:38 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(|data: Bytes| {
|
2019-12-20 07:50:07 +00:00
|
|
|
let mut e = BrotliEncoder::new(Vec::new(), 5);
|
2019-12-12 17:08:38 +00:00
|
|
|
e.write_all(&data).unwrap();
|
2019-12-20 07:50:07 +00:00
|
|
|
let data = e.finish().unwrap();
|
2019-12-12 17:08:38 +00:00
|
|
|
HttpResponse::Ok()
|
2021-01-15 02:11:10 +00:00
|
|
|
.insert_header(("content-encoding", "br"))
|
2019-12-12 17:08:38 +00:00
|
|
|
.body(data)
|
|
|
|
})))
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// client request
|
|
|
|
let mut response = srv.post("/").send_body(STR).await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2019-03-27 03:45:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:16:56 +00:00
|
|
|
#[cfg(feature = "compress-brotli")]
|
2019-12-08 06:31:16 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_client_brotli_encoding_large_random() {
|
|
|
|
let data = rand::thread_rng()
|
|
|
|
.sample_iter(&rand::distributions::Alphanumeric)
|
|
|
|
.take(70_000)
|
2020-12-22 23:45:31 +00:00
|
|
|
.map(char::from)
|
2019-12-08 06:31:16 +00:00
|
|
|
.collect::<String>();
|
2019-03-27 03:45:00 +00:00
|
|
|
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2019-12-12 17:08:38 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(|data: Bytes| {
|
2019-12-20 07:50:07 +00:00
|
|
|
let mut e = BrotliEncoder::new(Vec::new(), 5);
|
2019-12-12 17:08:38 +00:00
|
|
|
e.write_all(&data).unwrap();
|
2019-12-20 07:50:07 +00:00
|
|
|
let data = e.finish().unwrap();
|
2019-12-12 17:08:38 +00:00
|
|
|
HttpResponse::Ok()
|
2021-01-15 02:11:10 +00:00
|
|
|
.insert_header(("content-encoding", "br"))
|
2019-12-12 17:08:38 +00:00
|
|
|
.body(data)
|
|
|
|
})))
|
2019-12-08 06:31:16 +00:00
|
|
|
});
|
2019-03-27 03:45:00 +00:00
|
|
|
|
2019-12-08 06:31:16 +00:00
|
|
|
// client request
|
|
|
|
let mut response = srv.post("/").send_body(data.clone()).await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes.len(), data.len());
|
|
|
|
assert_eq!(bytes, Bytes::from(data));
|
|
|
|
}
|
2019-03-27 03:45:00 +00:00
|
|
|
|
2021-01-17 05:19:32 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_client_deflate_encoding() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2021-01-17 05:19:32 +00:00
|
|
|
App::new().default_service(web::to(|body: Bytes| {
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.encoding(http::ContentEncoding::Br)
|
|
|
|
.body(body)
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
|
|
|
|
let req = srv.post("/").send_body(STR);
|
|
|
|
|
|
|
|
let mut res = req.await.unwrap();
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let bytes = res.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_client_deflate_encoding_large_random() {
|
|
|
|
let data = rand::thread_rng()
|
|
|
|
.sample_iter(rand::distributions::Alphanumeric)
|
|
|
|
.map(char::from)
|
|
|
|
.take(70_000)
|
|
|
|
.collect::<String>();
|
|
|
|
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2021-01-17 05:19:32 +00:00
|
|
|
App::new().default_service(web::to(|body: Bytes| {
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.encoding(http::ContentEncoding::Br)
|
|
|
|
.body(body)
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
|
|
|
|
let req = srv.post("/").send_body(data.clone());
|
|
|
|
|
|
|
|
let mut res = req.await.unwrap();
|
|
|
|
let bytes = res.body().await.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
assert_eq!(bytes, Bytes::from(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_client_streaming_explicit() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2021-01-17 05:19:32 +00:00
|
|
|
App::new().default_service(web::to(|body: web::Payload| {
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.encoding(http::ContentEncoding::Identity)
|
|
|
|
.streaming(body)
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
|
2021-02-11 23:03:17 +00:00
|
|
|
let body =
|
|
|
|
stream::once(async { Ok::<_, actix_http::Error>(Bytes::from_static(STR.as_bytes())) });
|
2021-01-17 05:19:32 +00:00
|
|
|
let req = srv.post("/").send_stream(Box::pin(body));
|
|
|
|
|
|
|
|
let mut res = req.await.unwrap();
|
|
|
|
assert!(res.status().is_success());
|
|
|
|
|
|
|
|
let bytes = res.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_body_streaming_implicit() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2021-01-17 05:19:32 +00:00
|
|
|
App::new().default_service(web::to(|| {
|
|
|
|
let body = stream::once(async {
|
|
|
|
Ok::<_, actix_http::Error>(Bytes::from_static(STR.as_bytes()))
|
|
|
|
});
|
|
|
|
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.encoding(http::ContentEncoding::Gzip)
|
|
|
|
.streaming(Box::pin(body))
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
|
|
|
|
let req = srv.get("/").send();
|
|
|
|
|
|
|
|
let mut res = req.await.unwrap();
|
|
|
|
assert!(res.status().is_success());
|
|
|
|
|
|
|
|
let bytes = res.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
|
|
|
}
|
2019-03-27 03:45:00 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_client_cookie_handling() {
|
2019-11-22 05:49:35 +00:00
|
|
|
use std::io::{Error as IoError, ErrorKind};
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let cookie1 = Cookie::build("cookie1", "value1").finish();
|
|
|
|
let cookie2 = Cookie::build("cookie2", "value2")
|
|
|
|
.domain("www.example.org")
|
|
|
|
.path("/")
|
|
|
|
.secure(true)
|
|
|
|
.http_only(true)
|
|
|
|
.finish();
|
|
|
|
// Q: are all these clones really necessary? A: Yes, possibly
|
|
|
|
let cookie1b = cookie1.clone();
|
|
|
|
let cookie2b = cookie2.clone();
|
|
|
|
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(move || {
|
2019-11-26 05:25:50 +00:00
|
|
|
let cookie1 = cookie1b.clone();
|
|
|
|
let cookie2 = cookie2b.clone();
|
|
|
|
|
2019-12-12 17:08:38 +00:00
|
|
|
App::new().route(
|
2019-11-26 05:25:50 +00:00
|
|
|
"/",
|
|
|
|
web::to(move |req: HttpRequest| {
|
|
|
|
let cookie1 = cookie1.clone();
|
|
|
|
let cookie2 = cookie2.clone();
|
|
|
|
|
|
|
|
async move {
|
|
|
|
// Check cookies were sent correctly
|
|
|
|
let res: Result<(), Error> = req
|
|
|
|
.cookie("cookie1")
|
|
|
|
.ok_or(())
|
|
|
|
.and_then(|c1| {
|
|
|
|
if c1.value() == "value1" {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.and_then(|()| req.cookie("cookie2").ok_or(()))
|
|
|
|
.and_then(|c2| {
|
|
|
|
if c2.value() == "value2" {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map_err(|_| Error::from(IoError::from(ErrorKind::NotFound)));
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
Err(e)
|
|
|
|
} else {
|
|
|
|
// Send some cookies back
|
|
|
|
Ok::<_, Error>(
|
|
|
|
HttpResponse::Ok().cookie(cookie1).cookie(cookie2).finish(),
|
|
|
|
)
|
2019-11-22 05:49:35 +00:00
|
|
|
}
|
2019-11-26 05:25:50 +00:00
|
|
|
}
|
|
|
|
}),
|
2019-12-12 17:08:38 +00:00
|
|
|
)
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let request = srv.get("/").cookie(cookie1.clone()).cookie(cookie2.clone());
|
|
|
|
let response = request.send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
let c1 = response.cookie("cookie1").expect("Missing cookie1");
|
|
|
|
assert_eq!(c1, cookie1);
|
|
|
|
let c2 = response.cookie("cookie2").expect("Missing cookie2");
|
|
|
|
assert_eq!(c2, cookie2);
|
2019-03-27 04:31:18 +00:00
|
|
|
}
|
2019-03-27 03:45:00 +00:00
|
|
|
|
2021-01-17 05:19:32 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn client_unread_response() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let addr = actix_test::unused_addr();
|
2021-02-06 16:05:33 +00:00
|
|
|
let lst = std::net::TcpListener::bind(addr).unwrap();
|
2021-01-17 05:19:32 +00:00
|
|
|
|
2021-02-06 16:05:33 +00:00
|
|
|
std::thread::spawn(move || {
|
2021-11-04 15:57:55 +00:00
|
|
|
let (mut stream, _) = lst.accept().unwrap();
|
|
|
|
let mut b = [0; 1000];
|
|
|
|
let _ = stream.read(&mut b).unwrap();
|
|
|
|
let _ = stream.write_all(
|
|
|
|
b"HTTP/1.1 200 OK\r\n\
|
2021-01-17 05:19:32 +00:00
|
|
|
connection: close\r\n\
|
|
|
|
\r\n\
|
|
|
|
welcome!",
|
2021-11-04 15:57:55 +00:00
|
|
|
);
|
2021-01-17 05:19:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// client request
|
|
|
|
let req = awc::Client::new().get(format!("http://{}/", addr).as_str());
|
|
|
|
let mut res = req.send().await.unwrap();
|
|
|
|
assert!(res.status().is_success());
|
|
|
|
|
|
|
|
// awc does not read all bytes unless content-length is specified
|
|
|
|
let bytes = res.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(b""));
|
|
|
|
}
|
2019-03-27 03:45:00 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn client_basic_auth() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2019-12-12 17:08:38 +00:00
|
|
|
App::new().route(
|
2019-11-26 05:25:50 +00:00
|
|
|
"/",
|
|
|
|
web::to(|req: HttpRequest| {
|
|
|
|
if req
|
|
|
|
.headers()
|
|
|
|
.get(header::AUTHORIZATION)
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
2021-03-07 19:29:02 +00:00
|
|
|
== format!("Basic {}", base64::encode("username:password"))
|
2019-11-26 05:25:50 +00:00
|
|
|
{
|
|
|
|
HttpResponse::Ok()
|
|
|
|
} else {
|
|
|
|
HttpResponse::BadRequest()
|
|
|
|
}
|
|
|
|
}),
|
2019-12-12 17:08:38 +00:00
|
|
|
)
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// set authorization header to Basic <base64 encoded username:password>
|
2021-03-07 19:29:02 +00:00
|
|
|
let request = srv.get("/").basic_auth("username", "password");
|
2019-11-26 05:25:50 +00:00
|
|
|
let response = request.send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-03-27 03:57:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn client_bearer_auth() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2019-12-12 17:08:38 +00:00
|
|
|
App::new().route(
|
2019-11-26 05:25:50 +00:00
|
|
|
"/",
|
|
|
|
web::to(|req: HttpRequest| {
|
|
|
|
if req
|
|
|
|
.headers()
|
|
|
|
.get(header::AUTHORIZATION)
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
== "Bearer someS3cr3tAutht0k3n"
|
|
|
|
{
|
|
|
|
HttpResponse::Ok()
|
|
|
|
} else {
|
|
|
|
HttpResponse::BadRequest()
|
|
|
|
}
|
|
|
|
}),
|
2019-12-12 17:08:38 +00:00
|
|
|
)
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// set authorization header to Bearer <token>
|
|
|
|
let request = srv.get("/").bearer_auth("someS3cr3tAutht0k3n");
|
|
|
|
let response = request.send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-03-27 03:57:06 +00:00
|
|
|
}
|
2021-02-27 22:31:14 +00:00
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_local_address() {
|
|
|
|
let ip = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
|
|
|
|
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(move || {
|
2021-02-27 22:31:14 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(
|
|
|
|
move |req: HttpRequest| async move {
|
|
|
|
assert_eq!(req.peer_addr().unwrap().ip(), ip);
|
|
|
|
Ok::<_, Error>(HttpResponse::Ok())
|
|
|
|
},
|
|
|
|
)))
|
|
|
|
});
|
|
|
|
let client = awc::Client::builder().local_address(ip).finish();
|
|
|
|
|
|
|
|
let res = client.get(srv.url("/")).send().await.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(res.status(), 200);
|
|
|
|
|
|
|
|
let client = awc::Client::builder()
|
|
|
|
.connector(
|
|
|
|
// connector local address setting should always be override by client builder.
|
|
|
|
awc::Connector::new().local_address(IpAddr::V4(Ipv4Addr::new(128, 0, 0, 1))),
|
|
|
|
)
|
|
|
|
.local_address(ip)
|
|
|
|
.finish();
|
|
|
|
|
|
|
|
let res = client.get(srv.url("/")).send().await.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(res.status(), 200);
|
|
|
|
}
|