4

My application uses the hyper crate to serve some data over HTTP. The core is a handler function, like this:

struct HttpHandler {}

impl hyper::server::Handler for HttpHandler {
    fn handle(&self, req: hyper::server::Request, res: hyper::server::Response) {
        res.send(b"Hello").unwrap();
    }
}

Hyper will call this function for each HTTP request, providing the Request req and Response res variables.

I want to unit test my handle function, so I call the function, providing a Request and Response, and assert that the Response has been used to send the expected data ("Hello").

I'm trying to instantiate a Request and a Response object, to pass into the handle function. For this, several dependencies are needed, which I need to create. For this, I ended up implementing a mock NetworkStream:

mod tests {
    use std::io;
    use std::io::prelude::*;
    use std::net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4};
    use std::time::Duration;
    use hyper::server::Handler;

    use super::*;

    struct MockNetworkStream {}

    impl Read for MockNetworkStream {
        fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
            Ok(1)
        }
    }

    impl Write for MockNetworkStream {
        fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
            Ok(1)
        }

        fn flush(&mut self) -> io::Result<()> {
            Ok(())
        }
    }

    impl hyper::net::NetworkStream for MockNetworkStream {
        fn peer_addr(&mut self) -> Result<SocketAddr, io::Error> {
            Ok(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080)))
        }

        fn set_read_timeout(&self, dur: Option<Duration>) -> Result<(), io::Error> {
            Ok(())
        }

        fn set_write_timeout(&self, dur: Option<Duration>) -> Result<(), io::Error> {
            Ok(())
        }
    }

    #[test]
    fn test_handle() {
        let handler = HttpHandler {};

        let mut request_mock_network_stream = MockNetworkStream {};

        let mut reader = hyper::buffer::BufReader::new(&mut request_mock_network_stream as
                                                       &mut hyper::net::NetworkStream);

        let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

        // The following fails with
        //    'tests::test_handle' panicked at 'called `Result::unwrap()` on an `Err` value: Header'
        let request = hyper::server::Request::new(&mut reader, socket).unwrap();

        let mut headers = hyper::header::Headers::new();
        let mut response_mock_network_stream = MockNetworkStream {};
        let response = hyper::server::Response::new(&mut response_mock_network_stream,
                                                    &mut headers);

        handler.handle(request, response);

        // I would like to do some assert like this:
        // assert_eq!(result, b"Hello");
    }
}

Full runnable playground example

However, instantiating the Request panics:

// The following fails with
//    'tests::test_handle' panicked at 'called `Result::unwrap()` on an `Err` value: Header'
let request = hyper::server::Request::new(&mut reader, socket).unwrap();

Where is the mistake in my mock setup? Is there a more straightforward way to test such a handler function without so much boilerplate code?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
pixelistik
  • 7,541
  • 3
  • 32
  • 42
  • Those are no good impl of `Read` and `Write`... That could lead to other issues. – E_net4 May 10 '17 at 23:32
  • 1
    It's ugly, but you could just copy the internal implementation of [`MockStream`](https://github.com/hyperium/hyper/blob/0.10.x/src/mock.rs). – Shepmaster May 12 '17 at 16:16

2 Answers2

3

The request decoder will expect to find a HTTP request, which is provided by the reader.

Your reader provides... nothing. Obviously this will cause the parser to fail.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • I understand the problem, thank you. However, if I try `let request = hyper::server::Request::new(b"GET / HTTP/1.0\r\n\r\n", socket).unwrap();`, the compiler will complain about mismatched types: expected type `&mut hyper::buffer::BufReader<&mut hyper::net::NetworkStream + 'static>` found type `&'static [u8; 18]`. I will try to modify my mock to actually return valid HTTP data. – pixelistik May 11 '17 at 20:11
  • @pixelistik: Yes sorry. It looks like it's more specific than that, I'll remove the suggestion. – Matthieu M. May 12 '17 at 06:36
2

Answering my own question, building on the input by @Matthieu and @Shepmaster.

I copied the MockStream implementation from the Hyper code, instead of building my own.

Using this, I can now do what I wanted: check if my HTTP response contains the expected term:

#[test]
fn test_handle() {
    let handler = HttpHandler {};

    // Create a minimal HTTP request
    let mut request_mock_network_stream = MockStream::with_input(b"GET / HTTP/1.0\r\n\r\n");

    let mut reader = hyper::buffer::BufReader::new(&mut request_mock_network_stream as
                                                   &mut hyper::net::NetworkStream);

    let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

    let request = hyper::server::Request::new(&mut reader, socket).unwrap();

    let mut headers = hyper::header::Headers::new();
    let mut response_mock_network_stream = MockStream::new();

    {
        let response = hyper::server::Response::new(&mut response_mock_network_stream,
                                                    &mut headers);

        handler.handle(request, response);
    }

    let result = str::from_utf8(&response_mock_network_stream.write).unwrap();

    assert!(result.contains("Hello"));
}

Full runnable code

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
pixelistik
  • 7,541
  • 3
  • 32
  • 42