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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

//! hyper-stub provides functions to create [hyper] clients that convert requests
//! to responses using predefined functions, without doing any actual
//! networking. This means the entire request/response lifecycle happens in a
//! single process, and should have performance and stability improvements over,
//! for example, binding to a port. One potential use case for this is stubbing
//! HTTP interactions in tests to avoid slow/flakey/absent internet connections.
//!
//! The simplest case uses [`proxy_client_fn_ok`] to create a client bound to a
//! simple function that directly maps from a request to a response:
//!
//! ```
//! # extern crate futures;
//! # extern crate hyper;
//! # extern crate hyper_stub;
//! # extern crate tokio;
//! #
//! use futures::{Future, Stream};
//! use hyper::{Request, Response, Uri};
//! use hyper_stub::proxy_client_fn_ok;
//! use tokio::runtime::current_thread::Runtime;
//!
//! let echo_client = proxy_client_fn_ok(|request| {
//!     let body = request.into_body();
//!     Response::new(body)
//! });
//!
//! let url: Uri = "http://example.com".parse().unwrap();
//! let mut builder = Request::post(url);
//! let request = builder.body("hello world".into()).unwrap();
//! let future = echo_client.request(request)
//!     .and_then(|res| res.into_body().concat2())
//!     .map(|bytes| {
//!         let body = String::from_utf8(bytes.to_vec()).unwrap();
//!         println!("{}", body);
//!     })
//!     .map_err(|error| panic!("ERROR: {:?}", error));
//!
//! Runtime::new().unwrap().block_on(future).unwrap();
//! ```
//!
//! If the function needs to return an error, or respond to the request
//! asynchronously, [`proxy_client_fn`] can be used.
//!
//! Finally, an advanced use case is using hyper [`services`] instead of simple
//! functions. This can be done with the [`proxy_client`] function.
//!
//! [hyper]: https://hyper.rs
//! [services]: https://docs.rs/hyper/0.12.1/hyper/service/index.html
//! [`proxy_client_fn_ok`]: fn.proxy_client_fn_ok.html
//! [`proxy_client_fn`]: fn.proxy_client_fn.html
//! [`proxy_client`]: fn.proxy_client.html

extern crate futures;
extern crate hyper;
extern crate memsocket;
extern crate tokio;

mod connector;
mod never;

use connector::Connector;
use futures::prelude::*;
use hyper::body::{Body, Payload};
use hyper::client::connect::Connect;
use hyper::service::{NewService, Service};
use hyper::{Client, Request, Response};
use never::Never;
use std::error::Error;

/// Creates a hyper client whose requests are converted to responses by being
/// passed through a hyper [`Service`] instantiated by and returned from the given
/// [`NewService`].
///
/// [`proxy_client_fn`] is much more simple and almost as powerful, so should
/// generally be preferred.
///
/// [`Service`]: https://docs.rs/hyper/0.12.1/hyper/service/index.html
/// [`NewService`]: https://docs.rs/hyper/0.12.1/hyper/service/trait.NewService.html
/// [`proxy_client_fn`]: fn.proxy_client_fn.html
pub fn proxy_client<ResBody, ResponseError, ServiceError, ResponseFuture, ServiceFuture, S, N>(
    new_service: N,
) -> Client<Connector<N>>
where
    ResBody: Payload,
    ResponseError: Error + Send + Sync + 'static,
    ServiceError: Error + Send + Sync + 'static,
    ResponseFuture: Future<Item = Response<S::ResBody>, Error = ResponseError> + Send + 'static,
    ServiceFuture: Future<Item = S, Error = ServiceError> + Send + 'static,
    S: Service<ReqBody = Body, ResBody = ResBody, Error = ResponseError, Future = ResponseFuture>
        + Send
        + 'static,
    N: NewService<
            ReqBody = S::ReqBody,
            ResBody = S::ResBody,
            Future = ServiceFuture,
            Error = ResponseError,
            Service = S,
            InitError = ServiceError,
        >
        + Sync
        + Send,
{
    Client::builder()
        .set_host(true)
        .build(Connector::new(new_service))
}

/// Creates a hyper client whose requests are converted to responses by being
/// passed through the given handler function, which returns a future.
pub fn proxy_client_fn<E, Fut, F>(handler: F) -> Client<impl Connect>
where
    E: Error + Send + Sync + 'static,
    Fut: Future<Item = Response<Body>, Error = E> + Send + 'static,
    F: Fn(Request<Body>) -> Fut + Send + Sync + Copy + 'static,
{
    use futures::future;
    use hyper::service::service_fn;

    proxy_client(move || future::ok::<_, Never>(service_fn(handler)))
}

/// Creates a hyper client whose requests are converted to responses by being
/// passed through the given handler function.
///
/// See [`proxy_client_fn`] if errors or asynchronous processing are required.
///
/// [`proxy_client_fn`]: fn.proxy_client_fn.html
pub fn proxy_client_fn_ok<F>(handler: F) -> Client<impl Connect>
where
    F: Fn(Request<Body>) -> Response<Body> + Send + Sync + Copy + 'static,
{
    use futures::future;

    proxy_client_fn(move |req| future::ok::<_, Never>(handler(req)))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ok() {
        use futures::prelude::*;
        use tokio::runtime::current_thread::Runtime;

        let client = proxy_client_fn_ok(|req| {
            let query = req.uri().query().unwrap().to_string();
            Response::new(query.into())
        });

        Runtime::new()
            .unwrap()
            .block_on({
                client
                    .get("https://example.com?foo=bar".parse().unwrap())
                    .and_then(|res| res.into_body().concat2())
                    .map(|bytes| {
                        let body = String::from_utf8(bytes.to_vec()).unwrap();
                        assert_eq!(body, "foo=bar");
                    })
                    .map_err(|err| panic!("{:?}", err))
            })
            .unwrap();
    }

    #[test]
    fn test_err() {
        use futures::future::{self, FutureResult};
        use futures::prelude::*;
        use std::fmt::{self, Display, Formatter};
        use tokio::runtime::current_thread::Runtime;

        #[derive(Debug)]
        struct NewServiceError;

        impl Display for NewServiceError {
            fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
                write!(fmt, "correct error for test")
            }
        }

        impl Error for NewServiceError {
            fn description(&self) -> &str {
                "It broke"
            }
        }

        impl Service for Never {
            type ReqBody = Body;
            type ResBody = Body;
            type Error = Self;
            type Future = FutureResult<Response<Self::ResBody>, Self>;

            fn call(&mut self, _: Request<Self::ReqBody>) -> Self::Future {
                unreachable!()
            }
        }

        let client = proxy_client(|| future::err::<Never, _>(NewServiceError));

        let _ = Runtime::new().unwrap().block_on({
            client
                .get("https://example.com".parse().unwrap())
                .map(|res| panic!("didn't error: {:?}", res))
                .map_err(|err| assert!(err.to_string().contains("correct error for test")))
        });
    }
}