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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of OpenEthereum.

// OpenEthereum 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.

// OpenEthereum 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 OpenEthereum.  If not, see <http://www.gnu.org/licenses/>.

use http::{self, hyper};
use rpc_servers::{HttpServer, MetaIoHandler};

use tests::{helpers::Server, http_client};
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| {
        rpc_servers::start_http_with_middleware(
            &address,
            http::DomainsValidation::Disabled,
            http::DomainsValidation::Disabled,
            handler,
            extractors::RpcExtractor,
            |request: hyper::Request<hyper::Body>| http::RequestMiddlewareAction::Proceed {
                should_continue_on_invalid_cors: false,
                request,
            },
            1,
            5,
            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 tests {
    use super::{request, Server};
    use jsonrpc_core::{MetaIoHandler, Value};
    use v1::Metadata;

    fn serve() -> (Server<::HttpServer>, ::std::net::SocketAddr) {
        let mut io = MetaIoHandler::default();
        io.add_method_with_meta("hello", |_, meta: Metadata| {
            Ok(Value::String(format!("{}", meta.origin)))
        });
        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 = "{\"jsonrpc\":\"2.0\",\"result\":\"unknown origin / unknown agent via RPC\",\"id\":1}\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 =
            "{\"jsonrpc\":\"2.0\",\"result\":\"unknown origin / curl/7.16.3 via RPC\",\"id\":1}\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_respond_valid_to_any_requested_header() {
        // given
        let (server, address) = serve();
        let headers = "Something, Anything, Xyz, 123, _?";

        // when
        let res = request(
            server,
            &format!(
                "\
				OPTIONS / HTTP/1.1\r\n\
				Host: {}\r\n\
				Origin: http://openethereum.github.io\r\n\
				Content-Length: 0\r\n\
				Content-Type: application/json\r\n\
				Connection: close\r\n\
				Access-Control-Request-Headers: {}\r\n\
				\r\n\
			",
                address, headers
            ),
        );

        // then
        assert_eq!(res.status, "HTTP/1.1 200 OK".to_owned());
        let expected = format!("access-control-allow-headers: {}", headers);
        assert!(
            res.headers.contains(&expected),
            "Headers missing in {:?}",
            res.headers
        );
    }
}