UI server refactoring (#5580)
* Full API in Authenticated WS server. * Replacing UI server with Hyper. * Solving CLI, RPCs and tests. * Porting signer tests. * Fixing origin recognition for dapps/rpc. * Fixing tests. Adding parity-rpc-client to test. * Dapps exposed as RPC method. * JS code to support new connection scheme. * Fixing dapps tests. * Updating allowed origins/hosts to support web3.site. * Fixing tests, fixing UI. * Fixing tests. * Removing invalid tests. * Fixing merge. * 404 fallback for UI * Improve ContentFetcher constructor readability. * Naming. * Update .gitlab-ci.yml fix CI lint error * Fixing tests and linting issues. * Fixing new tests. * UI hosts. * Submodules fix.
This commit is contained in:
committed by
Arkadiy Paronyan
parent
7499efecf6
commit
cbcc369a2d
172
rpc/src/tests/rpc.rs
Normal file
172
rpc/src/tests/rpc.rs
Normal file
@@ -0,0 +1,172 @@
|
||||
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
||||
// 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 devtools::http_client;
|
||||
use jsonrpc_core::MetaIoHandler;
|
||||
use http::{self, hyper};
|
||||
|
||||
use {HttpSettings, HttpServer};
|
||||
use tests::helpers::Server;
|
||||
use v1::{extractors, Metadata};
|
||||
|
||||
fn serve(handler: Option<MetaIoHandler<Metadata>>) -> Server<HttpServer> {
|
||||
let address = "127.0.0.1:0".parse().unwrap();
|
||||
let handler = handler.unwrap_or_default();
|
||||
|
||||
Server::new(|remote| ::start_http(
|
||||
&address,
|
||||
http::DomainsValidation::Disabled,
|
||||
http::DomainsValidation::Disabled,
|
||||
handler,
|
||||
remote,
|
||||
extractors::RpcExtractor,
|
||||
HttpSettings::Dapps(Some(|_req: &hyper::server::Request<hyper::net::HttpStream>, _control: &hyper::Control| {
|
||||
http::RequestMiddlewareAction::Proceed {
|
||||
should_continue_on_invalid_cors: false
|
||||
}
|
||||
})),
|
||||
).unwrap())
|
||||
}
|
||||
|
||||
/// Test a single request to running server
|
||||
fn request(server: Server<HttpServer>, request: &str) -> http_client::Response {
|
||||
http_client::request(server.server.address(), request)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod testsing {
|
||||
use jsonrpc_core::{MetaIoHandler, Value};
|
||||
use jsonrpc_core::futures::{Future, future};
|
||||
use v1::Metadata;
|
||||
use super::{request, Server};
|
||||
|
||||
fn serve() -> (Server<::HttpServer>, ::std::net::SocketAddr) {
|
||||
let mut io = MetaIoHandler::default();
|
||||
io.add_method_with_meta("hello", |_, meta: Metadata| {
|
||||
future::ok(Value::String(format!("{}", meta.origin))).boxed()
|
||||
});
|
||||
let server = super::serve(Some(io));
|
||||
let address = server.server.address().to_owned();
|
||||
|
||||
(server, address)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_extract_rpc_origin() {
|
||||
// given
|
||||
let (server, address) = serve();
|
||||
|
||||
// when
|
||||
let req = r#"{"method":"hello","params":[],"jsonrpc":"2.0","id":1}"#;
|
||||
let expected = "34\n{\"jsonrpc\":\"2.0\",\"result\":\"unknown via RPC\",\"id\":1}\n\n0\n\n";
|
||||
let res = request(server,
|
||||
&format!("\
|
||||
POST / HTTP/1.1\r\n\
|
||||
Host: {}\r\n\
|
||||
Content-Type: application/json\r\n\
|
||||
Content-Length: {}\r\n\
|
||||
Connection: close\r\n\
|
||||
\r\n\
|
||||
{}
|
||||
", address, req.len(), req)
|
||||
);
|
||||
|
||||
// then
|
||||
res.assert_status("HTTP/1.1 200 OK");
|
||||
assert_eq!(res.body, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_extract_rpc_origin_with_service() {
|
||||
// given
|
||||
let (server, address) = serve();
|
||||
|
||||
// when
|
||||
let req = r#"{"method":"hello","params":[],"jsonrpc":"2.0","id":1}"#;
|
||||
let expected = "38\n{\"jsonrpc\":\"2.0\",\"result\":\"curl/7.16.3 via RPC\",\"id\":1}\n\n0\n\n";
|
||||
let res = request(server,
|
||||
&format!("\
|
||||
POST / HTTP/1.1\r\n\
|
||||
Host: {}\r\n\
|
||||
Content-Type: application/json\r\n\
|
||||
Content-Length: {}\r\n\
|
||||
Connection: close\r\n\
|
||||
User-Agent: curl/7.16.3\r\n\
|
||||
\r\n\
|
||||
{}
|
||||
", address, req.len(), req)
|
||||
);
|
||||
|
||||
// then
|
||||
res.assert_status("HTTP/1.1 200 OK");
|
||||
assert_eq!(res.body, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_extract_dapp_origin() {
|
||||
// given
|
||||
let (server, address) = serve();
|
||||
|
||||
// when
|
||||
let req = r#"{"method":"hello","params":[],"jsonrpc":"2.0","id":1}"#;
|
||||
let expected = "3A\n{\"jsonrpc\":\"2.0\",\"result\":\"Dapp http://parity.io\",\"id\":1}\n\n0\n\n";
|
||||
let res = request(server,
|
||||
&format!("\
|
||||
POST / HTTP/1.1\r\n\
|
||||
Host: {}\r\n\
|
||||
Content-Type: application/json\r\n\
|
||||
Content-Length: {}\r\n\
|
||||
Origin: http://parity.io\r\n\
|
||||
Connection: close\r\n\
|
||||
User-Agent: curl/7.16.3\r\n\
|
||||
\r\n\
|
||||
{}
|
||||
", address, req.len(), req)
|
||||
);
|
||||
|
||||
// then
|
||||
res.assert_status("HTTP/1.1 200 OK");
|
||||
assert_eq!(res.body, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_extract_dapp_origin_from_extension() {
|
||||
// given
|
||||
let (server, address) = serve();
|
||||
|
||||
// when
|
||||
let req = r#"{"method":"hello","params":[],"jsonrpc":"2.0","id":1}"#;
|
||||
let expected = "44\n{\"jsonrpc\":\"2.0\",\"result\":\"Dapp http://wallet.ethereum.org\",\"id\":1}\n\n0\n\n";
|
||||
let res = request(server,
|
||||
&format!("\
|
||||
POST / HTTP/1.1\r\n\
|
||||
Host: {}\r\n\
|
||||
Content-Type: application/json\r\n\
|
||||
Content-Length: {}\r\n\
|
||||
Origin: null\r\n\
|
||||
X-Parity-Origin: http://wallet.ethereum.org\r\n\
|
||||
Connection: close\r\n\
|
||||
User-Agent: curl/7.16.3\r\n\
|
||||
\r\n\
|
||||
{}
|
||||
", address, req.len(), req)
|
||||
);
|
||||
|
||||
// then
|
||||
res.assert_status("HTTP/1.1 200 OK");
|
||||
assert_eq!(res.body, expected);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user