2016-12-11 19:14:42 +01:00
|
|
|
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
|
2016-08-30 16:05:18 +02:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
use std::env;
|
2016-09-01 10:16:04 +02:00
|
|
|
use std::str;
|
2016-08-30 16:05:18 +02:00
|
|
|
use std::sync::Arc;
|
2016-10-22 15:21:41 +02:00
|
|
|
use env_logger::LogBuilder;
|
2016-08-30 16:05:18 +02:00
|
|
|
|
|
|
|
use ServerBuilder;
|
|
|
|
use Server;
|
2016-12-27 16:38:55 +01:00
|
|
|
use fetch::Fetch;
|
2016-09-01 10:16:04 +02:00
|
|
|
use devtools::http_client;
|
2016-12-22 18:26:39 +01:00
|
|
|
use parity_reactor::Remote;
|
2016-08-30 16:05:18 +02:00
|
|
|
|
2016-12-27 16:38:55 +01:00
|
|
|
mod registrar;
|
|
|
|
mod fetch;
|
2016-08-30 16:05:18 +02:00
|
|
|
|
2016-12-27 16:38:55 +01:00
|
|
|
use self::registrar::FakeRegistrar;
|
|
|
|
use self::fetch::FakeFetch;
|
2016-08-30 16:05:18 +02:00
|
|
|
|
2016-12-27 16:38:55 +01:00
|
|
|
const SIGNER_PORT: u16 = 18180;
|
2016-08-30 16:05:18 +02:00
|
|
|
|
2016-10-22 15:21:41 +02:00
|
|
|
fn init_logger() {
|
|
|
|
// Initialize logger
|
|
|
|
if let Ok(log) = env::var("RUST_LOG") {
|
|
|
|
let mut builder = LogBuilder::new();
|
|
|
|
builder.parse(&log);
|
2016-12-11 15:41:49 +01:00
|
|
|
let _ = builder.init(); // ignore errors since ./test.sh will call this multiple times.
|
2016-10-22 15:21:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-28 13:45:25 +01:00
|
|
|
pub fn init_server<F, B>(hosts: Option<Vec<String>>, process: F, remote: Remote) -> (Server, Arc<FakeRegistrar>) where
|
2016-12-27 16:38:55 +01:00
|
|
|
F: FnOnce(ServerBuilder) -> ServerBuilder<B>,
|
|
|
|
B: Fetch,
|
|
|
|
{
|
2016-10-22 15:21:41 +02:00
|
|
|
init_logger();
|
2016-08-30 16:05:18 +02:00
|
|
|
let registrar = Arc::new(FakeRegistrar::new());
|
|
|
|
let mut dapps_path = env::temp_dir();
|
|
|
|
dapps_path.push("non-existent-dir-to-prevent-fs-files-from-loading");
|
2016-12-27 16:38:55 +01:00
|
|
|
let server = process(ServerBuilder::new(
|
2016-12-28 13:45:25 +01:00
|
|
|
dapps_path.to_str().unwrap().into(), registrar.clone(), remote,
|
2016-12-27 16:38:55 +01:00
|
|
|
))
|
|
|
|
.signer_address(Some(("127.0.0.1".into(), SIGNER_PORT)))
|
|
|
|
.start_unsecured_http(&"127.0.0.1:0".parse().unwrap(), hosts).unwrap();
|
2016-09-01 11:16:19 +02:00
|
|
|
(
|
2016-12-27 16:38:55 +01:00
|
|
|
server,
|
2016-09-01 11:16:19 +02:00
|
|
|
registrar,
|
|
|
|
)
|
2016-08-30 16:05:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serve_with_auth(user: &str, pass: &str) -> Server {
|
2016-10-22 15:21:41 +02:00
|
|
|
init_logger();
|
2016-08-30 16:05:18 +02:00
|
|
|
let registrar = Arc::new(FakeRegistrar::new());
|
2016-09-01 11:16:19 +02:00
|
|
|
let mut dapps_path = env::temp_dir();
|
|
|
|
dapps_path.push("non-existent-dir-to-prevent-fs-files-from-loading");
|
2016-12-27 16:38:55 +01:00
|
|
|
ServerBuilder::new(dapps_path.to_str().unwrap().into(), registrar.clone(), Remote::new_sync())
|
|
|
|
.signer_address(Some(("127.0.0.1".into(), SIGNER_PORT)))
|
|
|
|
.start_basic_auth_http(&"127.0.0.1:0".parse().unwrap(), None, user, pass).unwrap()
|
2016-08-30 16:05:18 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 11:16:19 +02:00
|
|
|
pub fn serve_hosts(hosts: Option<Vec<String>>) -> Server {
|
2016-12-28 13:45:25 +01:00
|
|
|
init_server(hosts, |builder| builder, Remote::new_sync()).0
|
2016-09-01 11:16:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serve_with_registrar() -> (Server, Arc<FakeRegistrar>) {
|
2016-12-28 13:45:25 +01:00
|
|
|
init_server(None, |builder| builder, Remote::new_sync())
|
2016-10-22 20:06:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serve_with_registrar_and_sync() -> (Server, Arc<FakeRegistrar>) {
|
2016-12-27 16:38:55 +01:00
|
|
|
init_server(None, |builder| {
|
|
|
|
builder.sync_status(Arc::new(|| true))
|
2016-12-28 13:45:25 +01:00
|
|
|
}, Remote::new_sync())
|
2016-12-27 16:38:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serve_with_registrar_and_fetch() -> (Server, FakeFetch, Arc<FakeRegistrar>) {
|
2016-12-28 13:45:25 +01:00
|
|
|
serve_with_registrar_and_fetch_and_threads(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serve_with_registrar_and_fetch_and_threads(multi_threaded: bool) -> (Server, FakeFetch, Arc<FakeRegistrar>) {
|
2016-12-27 16:38:55 +01:00
|
|
|
let fetch = FakeFetch::default();
|
|
|
|
let f = fetch.clone();
|
|
|
|
let (server, reg) = init_server(None, move |builder| {
|
|
|
|
builder.fetch(f.clone())
|
2016-12-28 13:45:25 +01:00
|
|
|
}, if multi_threaded { Remote::new_thread_per_future() } else { Remote::new_sync() });
|
2016-12-27 16:38:55 +01:00
|
|
|
|
|
|
|
(server, fetch, reg)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serve_with_fetch(web_token: &'static str) -> (Server, FakeFetch) {
|
|
|
|
let fetch = FakeFetch::default();
|
|
|
|
let f = fetch.clone();
|
|
|
|
let (server, _) = init_server(None, move |builder| {
|
|
|
|
builder
|
|
|
|
.fetch(f.clone())
|
|
|
|
.web_proxy_tokens(Arc::new(move |token| &token == web_token))
|
2016-12-28 13:45:25 +01:00
|
|
|
}, Remote::new_sync());
|
2016-12-27 16:38:55 +01:00
|
|
|
|
|
|
|
(server, fetch)
|
2016-09-01 11:16:19 +02:00
|
|
|
}
|
|
|
|
|
2016-08-30 16:05:18 +02:00
|
|
|
pub fn serve() -> Server {
|
2016-12-28 13:45:25 +01:00
|
|
|
init_server(None, |builder| builder, Remote::new_sync()).0
|
2016-08-30 16:05:18 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 10:16:04 +02:00
|
|
|
pub fn request(server: Server, request: &str) -> http_client::Response {
|
|
|
|
http_client::request(server.addr(), request)
|
2016-08-30 16:05:18 +02:00
|
|
|
}
|
2016-10-19 11:02:21 +02:00
|
|
|
|
|
|
|
pub fn assert_security_headers(headers: &[String]) {
|
2016-10-22 15:21:41 +02:00
|
|
|
http_client::assert_security_headers_present(headers, None)
|
|
|
|
}
|
|
|
|
pub fn assert_security_headers_for_embed(headers: &[String]) {
|
|
|
|
http_client::assert_security_headers_present(headers, Some(SIGNER_PORT))
|
2016-10-19 11:02:21 +02:00
|
|
|
}
|