1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-21 15:46:48 +00:00

enable runtime for test:: methods

This commit is contained in:
Nikolay Kim 2019-04-17 10:28:27 -07:00
parent cc8420377e
commit b64851c5ec
3 changed files with 31 additions and 42 deletions

View file

@ -331,7 +331,7 @@ impl ClientRequest {
/// This method calls provided closure with builder reference if /// This method calls provided closure with builder reference if
/// value is `true`. /// value is `true`.
pub fn if_true<F>(mut self, value: bool, f: F) -> Self pub fn if_true<F>(self, value: bool, f: F) -> Self
where where
F: FnOnce(ClientRequest) -> ClientRequest, F: FnOnce(ClientRequest) -> ClientRequest,
{ {
@ -344,7 +344,7 @@ impl ClientRequest {
/// This method calls provided closure with builder reference if /// This method calls provided closure with builder reference if
/// value is `Some`. /// value is `Some`.
pub fn if_some<T, F>(mut self, value: Option<T>, f: F) -> Self pub fn if_some<T, F>(self, value: Option<T>, f: F) -> Self
where where
F: FnOnce(T, ClientRequest) -> ClientRequest, F: FnOnce(T, ClientRequest) -> ClientRequest,
{ {

View file

@ -307,11 +307,11 @@ pub(crate) mod tests {
); );
let req = TestRequest::with_uri("/none").to_request(); let req = TestRequest::with_uri("/none").to_request();
let resp = TestRequest::block_on(srv.call(req)).unwrap(); let resp = block_on(srv.call(req)).unwrap();
assert_eq!(resp.status(), StatusCode::NOT_FOUND); assert_eq!(resp.status(), StatusCode::NOT_FOUND);
let req = TestRequest::with_uri("/some").to_request(); let req = TestRequest::with_uri("/some").to_request();
let resp = TestRequest::block_on(srv.call(req)).unwrap(); let resp = block_on(srv.call(req)).unwrap();
assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.status(), StatusCode::OK);
match resp.response().body() { match resp.response().body() {
ResponseBody::Body(Body::Bytes(ref b)) => { ResponseBody::Body(Body::Bytes(ref b)) => {

View file

@ -58,7 +58,7 @@ where
/// This function panics on nested call. /// This function panics on nested call.
pub fn run_on<F, R>(f: F) -> R pub fn run_on<F, R>(f: F) -> R
where where
F: Fn() -> R, F: FnOnce() -> R,
{ {
RT.with(move |rt| rt.borrow_mut().block_on(lazy(|| Ok::<_, ()>(f())))) RT.with(move |rt| rt.borrow_mut().block_on(lazy(|| Ok::<_, ()>(f()))))
.unwrap() .unwrap()
@ -117,7 +117,9 @@ where
S::InitError: std::fmt::Debug, S::InitError: std::fmt::Debug,
{ {
let cfg = ServerConfig::new("127.0.0.1:8080".parse().unwrap()); let cfg = ServerConfig::new("127.0.0.1:8080".parse().unwrap());
block_on(app.into_new_service().new_service(&cfg)).unwrap() let srv = app.into_new_service();
let fut = run_on(move || srv.new_service(&cfg));
block_on(fut).unwrap()
} }
/// Calls service and waits for response future completion. /// Calls service and waits for response future completion.
@ -146,7 +148,7 @@ where
S: Service<Request = R, Response = ServiceResponse<B>, Error = E>, S: Service<Request = R, Response = ServiceResponse<B>, Error = E>,
E: std::fmt::Debug, E: std::fmt::Debug,
{ {
block_on(app.call(req)).unwrap() block_on(run_on(move || app.call(req))).unwrap()
} }
/// Helper function that returns a response body of a TestRequest /// Helper function that returns a response body of a TestRequest
@ -178,13 +180,15 @@ where
S: Service<Request = Request, Response = ServiceResponse<B>, Error = Error>, S: Service<Request = Request, Response = ServiceResponse<B>, Error = Error>,
B: MessageBody, B: MessageBody,
{ {
block_on(app.call(req).and_then(|mut resp: ServiceResponse<B>| { block_on(run_on(move || {
app.call(req).and_then(|mut resp: ServiceResponse<B>| {
resp.take_body() resp.take_body()
.fold(BytesMut::new(), move |mut body, chunk| { .fold(BytesMut::new(), move |mut body, chunk| {
body.extend_from_slice(&chunk); body.extend_from_slice(&chunk);
Ok::<_, Error>(body) Ok::<_, Error>(body)
}) })
.map(|body: BytesMut| body.freeze()) .map(|body: BytesMut| body.freeze())
})
})) }))
.unwrap_or_else(|_| panic!("read_response failed at block_on unwrap")) .unwrap_or_else(|_| panic!("read_response failed at block_on unwrap"))
} }
@ -229,7 +233,8 @@ where
B: MessageBody, B: MessageBody,
T: DeserializeOwned, T: DeserializeOwned,
{ {
block_on(app.call(req).and_then(|mut resp: ServiceResponse<B>| { block_on(run_on(move || {
app.call(req).and_then(|mut resp: ServiceResponse<B>| {
resp.take_body() resp.take_body()
.fold(BytesMut::new(), move |mut body, chunk| { .fold(BytesMut::new(), move |mut body, chunk| {
body.extend_from_slice(&chunk); body.extend_from_slice(&chunk);
@ -240,6 +245,7 @@ where
panic!("read_response_json failed during deserialization") panic!("read_response_json failed during deserialization")
})) }))
}) })
})
})) }))
.unwrap_or_else(|_| panic!("read_response_json failed at block_on unwrap")) .unwrap_or_else(|_| panic!("read_response_json failed at block_on unwrap"))
} }
@ -460,23 +466,6 @@ impl TestRequest {
req.set_route_data(Some(Rc::new(self.route_data))); req.set_route_data(Some(Rc::new(self.route_data)));
(req, payload) (req, payload)
} }
/// Runs the provided future, blocking the current thread until the future
/// completes.
///
/// This function can be used to synchronously block the current thread
/// until the provided `future` has resolved either successfully or with an
/// error. The result of the future is then returned from this function
/// call.
///
/// Note that this function is intended to be used only for testing purpose.
/// This function panics on nested call.
pub fn block_on<F>(f: F) -> Result<F::Item, F::Error>
where
F: Future,
{
block_on(f)
}
} }
#[cfg(test)] #[cfg(test)]