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
use routing::Router;
use core::{Request, Response};
use proto::ArcService;
use hyper::{Body, Headers, HttpVersion, Method, Uri};
use serde::ser::Serialize;
use anymap::AnyMap;
use std::str::FromStr;
use serde_json::to_vec;
use tokio_core::reactor::Core;

/// Fake reactor allows for testing your application's endpoints
///
/// Do note that Ip addresses won't be present on the request struct when testing for obvious reasons
pub struct FakeReactor {
	pub routes: Router,
}

impl FakeReactor {
	/// post a request to the fake reactor,
	/// returns a `Result<Response, Response>`
	/// or panics if the route wasn't found
	pub fn post<T>(
		&self,
		route: &str,
		body: Option<T>,
		headers: Option<Headers>,
	) -> Result<Response, Response>
	where
		T: Serialize,
	{
		self.build(Method::Post, route, body, headers)
	}

	/// send a get request to the `FakeReactor`
	pub fn get(&self, route: &str, headers: Option<Headers>) -> Result<Response, Response> {
		self.build(Method::Get, route, None::<u8>, headers)
	}

	/// send a put request to the `FakeReactor`
	pub fn put<T>(
		&self,
		route: &str,
		body: Option<T>,
		headers: Option<Headers>,
	) -> Result<Response, Response>
	where
		T: Serialize,
	{
		self.build(Method::Put, route, body, headers)
	}

	/// send a patch request to the `FakeReactor`
	pub fn patch<T>(
		&self,
		route: &str,
		body: Option<T>,
		headers: Option<Headers>,
	) -> Result<Response, Response>
	where
		T: Serialize,
	{
		self.build(Method::Patch, route, body, headers)
	}

	/// send a delete request to the `FakeReactor`
	pub fn delete<T>(
		&self,
		route: &str,
		body: Option<T>,
		headers: Option<Headers>,
	) -> Result<Response, Response>
	where
		T: Serialize,
	{
		self.build(Method::Delete, route, body, headers)
	}

	 fn build<T>(
		&self,
		method: Method,
		route: &str,
		body: Option<T>,
		mut headers: Option<Headers>,
	) -> Result<Response, Response>
	where
		T: Serialize,
	{
		let body = match body {
			Some(b) => Some(to_vec(&b).unwrap().into()),
			None => Some(Body::default()),
		};

		if headers.is_none() {
			headers = Some(Headers::default());
		}

		let headers = headers.unwrap();
		let mut reactor = Core::new().expect("Could not start event loop");

		let req = Request {
			method: method.clone(),
			uri: Uri::from_str(route).unwrap(),
			version: HttpVersion::Http11,
			headers,
			body,
			remote: None,
			anyMap: AnyMap::new(),
			handle: Some(reactor.handle()),
		};

		let matched = self
			.routes
			.matchRoute(req.path(), &method)
			.expect(&format!("No Handler registered for Method::{}", method));

		return reactor.run(ArcService::call(matched.handler, req, Response::new()));
	}
}

#[cfg(test)]
mod tests {
	use impl_service::*;
	use hyper::StatusCode;
	use futures::{Future, Stream};
	use futures::prelude::async_block;
	use proto::ArcService;
	use core::{Request, Response};
	use routing::*;
	use super::*;

	#[service]
	fn AsyncService(_req: Request, res: Response) {
		let res = res
			.with_status(StatusCode::Ok)
			.with_body("Hello World".as_bytes());
		Result::Ok(res)
	}

	#[test]
	fn it_matches_the_correct_route_and_returns_the_correct_body() {
		let routes = Router::new().get("/hello", AsyncService);

		let fakereactor = FakeReactor { routes };

		// assert it returns Ok
		let result = fakereactor
			.get("/hello?lol=p", None)
			.expect("Should return ok");
		let body = result.body().concat2().wait().expect("Body should be Ok");
		assert_eq!(&body[..], b"Hello World");
	}
}