我的应用程序使用hyper crate 通过 HTTP 提供一些数据。核心是一个处理函数,如下所示:
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 将为每个 HTTP 请求调用此函数,并提供Request
req
和Response
res
变量。
我想对我handle
的函数进行单元测试,所以我调用该函数,提供一个Request
and Response
,并断言Response
已用于发送预期的数据(“Hello”)。
我正在尝试实例化一个Request
和一个Response
对象,以传递给handle
函数。为此,需要几个依赖项,我需要创建它们。为此,我最终实现了一个模拟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");
}
}
但是,实例化Request
恐慌:
// 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();
我的模拟设置中的错误在哪里?有没有更直接的方法来测试这样一个处理函数而没有这么多样板代码?