mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-27 10:39:03 +02:00
more tests
This commit is contained in:
@ -261,7 +261,8 @@ where
|
||||
type Future = Result<Response, Error>;
|
||||
|
||||
fn respond_to(self, _: &HttpRequest) -> Self::Future {
|
||||
Err(self.into())
|
||||
let err: Error = self.into();
|
||||
Ok(err.into())
|
||||
}
|
||||
}
|
||||
|
||||
@ -289,12 +290,13 @@ where
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use actix_service::Service;
|
||||
use bytes::Bytes;
|
||||
use bytes::{Bytes, BytesMut};
|
||||
|
||||
use super::*;
|
||||
use crate::dev::{Body, ResponseBody};
|
||||
use crate::http::StatusCode;
|
||||
use crate::test::{init_service, TestRequest};
|
||||
use crate::{web, App};
|
||||
use crate::http::{header::CONTENT_TYPE, HeaderValue, StatusCode};
|
||||
use crate::test::{block_on, init_service, TestRequest};
|
||||
use crate::{error, web, App, HttpResponse};
|
||||
|
||||
#[test]
|
||||
fn test_option_responder() {
|
||||
@ -319,4 +321,87 @@ mod tests {
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
|
||||
trait BodyTest {
|
||||
fn bin_ref(&self) -> &[u8];
|
||||
fn body(&self) -> &Body;
|
||||
}
|
||||
|
||||
impl BodyTest for ResponseBody<Body> {
|
||||
fn bin_ref(&self) -> &[u8] {
|
||||
match self {
|
||||
ResponseBody::Body(ref b) => match b {
|
||||
Body::Bytes(ref bin) => &bin,
|
||||
_ => panic!(),
|
||||
},
|
||||
ResponseBody::Other(ref b) => match b {
|
||||
Body::Bytes(ref bin) => &bin,
|
||||
_ => panic!(),
|
||||
},
|
||||
}
|
||||
}
|
||||
fn body(&self) -> &Body {
|
||||
match self {
|
||||
ResponseBody::Body(ref b) => b,
|
||||
ResponseBody::Other(ref b) => b,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_responder() {
|
||||
let req = TestRequest::default().to_http_request();
|
||||
|
||||
let resp: HttpResponse = block_on(().respond_to(&req)).unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
assert_eq!(*resp.body().body(), Body::Empty);
|
||||
|
||||
let resp: HttpResponse = block_on("test".respond_to(&req)).unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
assert_eq!(resp.body().bin_ref(), b"test");
|
||||
assert_eq!(
|
||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||
HeaderValue::from_static("text/plain; charset=utf-8")
|
||||
);
|
||||
|
||||
let resp: HttpResponse = block_on(b"test".respond_to(&req)).unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
assert_eq!(resp.body().bin_ref(), b"test");
|
||||
assert_eq!(
|
||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||
HeaderValue::from_static("application/octet-stream")
|
||||
);
|
||||
|
||||
let resp: HttpResponse = block_on("test".to_string().respond_to(&req)).unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
assert_eq!(resp.body().bin_ref(), b"test");
|
||||
assert_eq!(
|
||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||
HeaderValue::from_static("text/plain; charset=utf-8")
|
||||
);
|
||||
|
||||
let resp: HttpResponse =
|
||||
block_on(Bytes::from_static(b"test").respond_to(&req)).unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
assert_eq!(resp.body().bin_ref(), b"test");
|
||||
assert_eq!(
|
||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||
HeaderValue::from_static("application/octet-stream")
|
||||
);
|
||||
|
||||
let resp: HttpResponse =
|
||||
block_on(BytesMut::from(b"test".as_ref()).respond_to(&req)).unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
assert_eq!(resp.body().bin_ref(), b"test");
|
||||
assert_eq!(
|
||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||
HeaderValue::from_static("application/octet-stream")
|
||||
);
|
||||
|
||||
let resp: HttpResponse =
|
||||
error::InternalError::new("err", StatusCode::BAD_REQUEST)
|
||||
.respond_to(&req)
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
|
@ -182,8 +182,8 @@ where
|
||||
/// Host name is used by application router aa a hostname for url
|
||||
/// generation. Check [ConnectionInfo](./dev/struct.ConnectionInfo.
|
||||
/// html#method.host) documentation for more information.
|
||||
pub fn server_hostname(mut self, val: String) -> Self {
|
||||
self.host = Some(val);
|
||||
pub fn server_hostname<T: AsRef<str>>(mut self, val: T) -> Self {
|
||||
self.host = Some(val.as_ref().to_owned());
|
||||
self
|
||||
}
|
||||
|
||||
|
13
src/test.rs
13
src/test.rs
@ -12,7 +12,7 @@ use actix_server_config::ServerConfig;
|
||||
use actix_service::{IntoNewService, NewService, Service};
|
||||
use bytes::Bytes;
|
||||
use cookie::Cookie;
|
||||
use futures::Future;
|
||||
use futures::future::{lazy, Future};
|
||||
|
||||
use crate::config::{AppConfig, AppConfigInner};
|
||||
use crate::rmap::ResourceMap;
|
||||
@ -42,6 +42,17 @@ where
|
||||
RT.with(move |rt| rt.borrow_mut().block_on(f))
|
||||
}
|
||||
|
||||
/// Runs the provided function, with runtime enabled.
|
||||
///
|
||||
/// Note that this function is intended to be used only for testing purpose.
|
||||
/// This function panics on nested call.
|
||||
pub fn run_on<F, I, E>(f: F) -> Result<I, E>
|
||||
where
|
||||
F: Fn() -> Result<I, E>,
|
||||
{
|
||||
RT.with(move |rt| rt.borrow_mut().block_on(lazy(|| f())))
|
||||
}
|
||||
|
||||
/// This method accepts application builder instance, and constructs
|
||||
/// service.
|
||||
///
|
||||
|
Reference in New Issue
Block a user