forked from actix/actix-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_httpserver.rs
134 lines (111 loc) · 3.96 KB
/
test_httpserver.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#[cfg(feature = "openssl")]
extern crate tls_openssl as openssl;
#[cfg(any(unix, feature = "openssl"))]
use {
actix_web::{web, App, HttpResponse, HttpServer},
std::{sync::mpsc, thread, time::Duration},
};
#[cfg(unix)]
#[actix_rt::test]
async fn test_start() {
let addr = actix_test::unused_addr();
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
actix_rt::System::new()
.block_on(async {
let srv = HttpServer::new(|| {
App::new().service(
web::resource("/").route(web::to(|| HttpResponse::Ok().body("test"))),
)
})
.workers(1)
.backlog(1)
.max_connections(10)
.max_connection_rate(10)
.keep_alive(10)
.client_timeout(5000)
.client_shutdown(0)
.server_hostname("localhost")
.system_exit()
.disable_signals()
.bind(format!("{}", addr))
.unwrap()
.run();
tx.send(srv.handle()).unwrap();
srv.await
})
.unwrap();
});
let srv = rx.recv().unwrap();
let client = awc::Client::builder()
.connector(awc::Connector::new().timeout(Duration::from_millis(100)))
.finish();
let host = format!("http://{}", addr);
let response = client.get(host.clone()).send().await.unwrap();
assert!(response.status().is_success());
srv.stop(false).await;
}
#[cfg(feature = "openssl")]
fn ssl_acceptor() -> openssl::ssl::SslAcceptorBuilder {
use openssl::{
pkey::PKey,
ssl::{SslAcceptor, SslMethod},
x509::X509,
};
let cert = rcgen::generate_simple_self_signed(vec!["localhost".to_owned()]).unwrap();
let cert_file = cert.serialize_pem().unwrap();
let key_file = cert.serialize_private_key_pem();
let cert = X509::from_pem(cert_file.as_bytes()).unwrap();
let key = PKey::private_key_from_pem(key_file.as_bytes()).unwrap();
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
builder.set_certificate(&cert).unwrap();
builder.set_private_key(&key).unwrap();
builder
}
#[actix_rt::test]
#[cfg(feature = "openssl")]
async fn test_start_ssl() {
use actix_web::HttpRequest;
use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
let addr = actix_test::unused_addr();
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
actix_rt::System::new()
.block_on(async {
let builder = ssl_acceptor();
let srv = HttpServer::new(|| {
App::new().service(web::resource("/").route(web::to(|req: HttpRequest| {
assert!(req.app_config().secure());
HttpResponse::Ok().body("test")
})))
})
.workers(1)
.shutdown_timeout(1)
.system_exit()
.disable_signals()
.bind_openssl(format!("{}", addr), builder)
.unwrap();
let srv = srv.run();
tx.send(srv.handle()).unwrap();
srv.await
})
.unwrap()
});
let srv = rx.recv().unwrap();
let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
builder.set_verify(SslVerifyMode::NONE);
let _ = builder
.set_alpn_protos(b"\x02h2\x08http/1.1")
.map_err(|e| log::error!("Can not set alpn protocol: {:?}", e));
let client = awc::Client::builder()
.connector(
awc::Connector::new()
.openssl(builder.build())
.timeout(Duration::from_millis(100)),
)
.finish();
let host = format!("https://{}", addr);
let response = client.get(host.clone()).send().await.unwrap();
assert!(response.status().is_success());
srv.stop(false).await;
}