mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 09:42:57 +01:00
rename test methods; update tests
This commit is contained in:
parent
15c5a3bcfb
commit
34695f4bce
@ -1,5 +1,12 @@
|
||||
# Changes
|
||||
|
||||
### Changed
|
||||
|
||||
* Renamed `TestRequest::to_service()` to `TestRequest::to_srv_request()`
|
||||
|
||||
* Renamed `TestRequest::to_response()` to `TestRequest::to_srv_response()`
|
||||
|
||||
|
||||
### Removed
|
||||
|
||||
* Removed unused `actix_web::web::md()`
|
||||
|
@ -848,8 +848,8 @@ mod tests {
|
||||
#[test]
|
||||
fn validate_origin_allows_all_origins() {
|
||||
let mut cors = Cors::new().finish(test::ok_service());
|
||||
let req =
|
||||
TestRequest::with_header("Origin", "https://www.example.com").to_service();
|
||||
let req = TestRequest::with_header("Origin", "https://www.example.com")
|
||||
.to_srv_request();
|
||||
|
||||
let resp = test::call_success(&mut cors, req);
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
@ -867,7 +867,7 @@ mod tests {
|
||||
|
||||
let req = TestRequest::with_header("Origin", "https://www.example.com")
|
||||
.method(Method::OPTIONS)
|
||||
.to_service();
|
||||
.to_srv_request();
|
||||
|
||||
assert!(cors.inner.validate_allowed_method(&req).is_err());
|
||||
assert!(cors.inner.validate_allowed_headers(&req).is_err());
|
||||
@ -877,7 +877,7 @@ mod tests {
|
||||
let req = TestRequest::with_header("Origin", "https://www.example.com")
|
||||
.header(header::ACCESS_CONTROL_REQUEST_METHOD, "put")
|
||||
.method(Method::OPTIONS)
|
||||
.to_service();
|
||||
.to_srv_request();
|
||||
|
||||
assert!(cors.inner.validate_allowed_method(&req).is_err());
|
||||
assert!(cors.inner.validate_allowed_headers(&req).is_err());
|
||||
@ -889,7 +889,7 @@ mod tests {
|
||||
"AUTHORIZATION,ACCEPT",
|
||||
)
|
||||
.method(Method::OPTIONS)
|
||||
.to_service();
|
||||
.to_srv_request();
|
||||
|
||||
let resp = test::call_success(&mut cors, req);
|
||||
assert_eq!(
|
||||
@ -935,7 +935,7 @@ mod tests {
|
||||
"AUTHORIZATION,ACCEPT",
|
||||
)
|
||||
.method(Method::OPTIONS)
|
||||
.to_service();
|
||||
.to_srv_request();
|
||||
|
||||
let resp = test::call_success(&mut cors, req);
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
@ -960,7 +960,7 @@ mod tests {
|
||||
|
||||
let req = TestRequest::with_header("Origin", "https://www.unknown.com")
|
||||
.method(Method::GET)
|
||||
.to_service();
|
||||
.to_srv_request();
|
||||
cors.inner.validate_origin(&req).unwrap();
|
||||
cors.inner.validate_allowed_method(&req).unwrap();
|
||||
cors.inner.validate_allowed_headers(&req).unwrap();
|
||||
@ -974,7 +974,7 @@ mod tests {
|
||||
|
||||
let req = TestRequest::with_header("Origin", "https://www.example.com")
|
||||
.method(Method::GET)
|
||||
.to_service();
|
||||
.to_srv_request();
|
||||
|
||||
let resp = test::call_success(&mut cors, req);
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
@ -984,7 +984,7 @@ mod tests {
|
||||
fn test_no_origin_response() {
|
||||
let mut cors = Cors::new().disable_preflight().finish(test::ok_service());
|
||||
|
||||
let req = TestRequest::default().method(Method::GET).to_service();
|
||||
let req = TestRequest::default().method(Method::GET).to_srv_request();
|
||||
let resp = test::call_success(&mut cors, req);
|
||||
assert!(resp
|
||||
.headers()
|
||||
@ -993,7 +993,7 @@ mod tests {
|
||||
|
||||
let req = TestRequest::with_header("Origin", "https://www.example.com")
|
||||
.method(Method::OPTIONS)
|
||||
.to_service();
|
||||
.to_srv_request();
|
||||
let resp = test::call_success(&mut cors, req);
|
||||
assert_eq!(
|
||||
&b"https://www.example.com"[..],
|
||||
@ -1019,7 +1019,7 @@ mod tests {
|
||||
|
||||
let req = TestRequest::with_header("Origin", "https://www.example.com")
|
||||
.method(Method::OPTIONS)
|
||||
.to_service();
|
||||
.to_srv_request();
|
||||
|
||||
let resp = test::call_success(&mut cors, req);
|
||||
assert_eq!(
|
||||
@ -1066,7 +1066,7 @@ mod tests {
|
||||
}));
|
||||
let req = TestRequest::with_header("Origin", "https://www.example.com")
|
||||
.method(Method::OPTIONS)
|
||||
.to_service();
|
||||
.to_srv_request();
|
||||
let resp = test::call_success(&mut cors, req);
|
||||
assert_eq!(
|
||||
&b"Accept, Origin"[..],
|
||||
@ -1082,7 +1082,7 @@ mod tests {
|
||||
let req = TestRequest::with_header("Origin", "https://www.example.com")
|
||||
.method(Method::OPTIONS)
|
||||
.header(header::ACCESS_CONTROL_REQUEST_METHOD, "POST")
|
||||
.to_service();
|
||||
.to_srv_request();
|
||||
let resp = test::call_success(&mut cors, req);
|
||||
|
||||
let origins_str = resp
|
||||
@ -1105,7 +1105,7 @@ mod tests {
|
||||
|
||||
let req = TestRequest::with_header("Origin", "https://example.com")
|
||||
.method(Method::GET)
|
||||
.to_service();
|
||||
.to_srv_request();
|
||||
|
||||
let resp = test::call_success(&mut cors, req);
|
||||
assert_eq!(
|
||||
@ -1118,7 +1118,7 @@ mod tests {
|
||||
|
||||
let req = TestRequest::with_header("Origin", "https://example.org")
|
||||
.method(Method::GET)
|
||||
.to_service();
|
||||
.to_srv_request();
|
||||
|
||||
let resp = test::call_success(&mut cors, req);
|
||||
assert_eq!(
|
||||
@ -1141,7 +1141,7 @@ mod tests {
|
||||
let req = TestRequest::with_header("Origin", "https://example.com")
|
||||
.header(header::ACCESS_CONTROL_REQUEST_METHOD, "GET")
|
||||
.method(Method::OPTIONS)
|
||||
.to_service();
|
||||
.to_srv_request();
|
||||
|
||||
let resp = test::call_success(&mut cors, req);
|
||||
assert_eq!(
|
||||
@ -1155,7 +1155,7 @@ mod tests {
|
||||
let req = TestRequest::with_header("Origin", "https://example.org")
|
||||
.header(header::ACCESS_CONTROL_REQUEST_METHOD, "GET")
|
||||
.method(Method::OPTIONS)
|
||||
.to_service();
|
||||
.to_srv_request();
|
||||
|
||||
let resp = test::call_success(&mut cors, req);
|
||||
assert_eq!(
|
||||
|
@ -166,11 +166,11 @@ mod tests {
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let req = TestRequest::default().to_service();
|
||||
let req = TestRequest::default().to_srv_request();
|
||||
let resp = block_on(mw.call(req)).unwrap();
|
||||
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001");
|
||||
|
||||
let req = TestRequest::default().to_service();
|
||||
let req = TestRequest::default().to_srv_request();
|
||||
let srv = FnService::new(|req: ServiceRequest<_>| {
|
||||
req.into_response(HttpResponse::Ok().header(CONTENT_TYPE, "0002").finish())
|
||||
});
|
||||
@ -192,7 +192,7 @@ mod tests {
|
||||
let mut mw =
|
||||
block_on(DefaultHeaders::new().content_type().new_transform(srv)).unwrap();
|
||||
|
||||
let req = TestRequest::default().to_service();
|
||||
let req = TestRequest::default().to_srv_request();
|
||||
let resp = block_on(mw.call(req)).unwrap();
|
||||
assert_eq!(
|
||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||
|
@ -180,7 +180,7 @@ mod tests {
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let resp = test::call_success(&mut mw, TestRequest::default().to_service());
|
||||
let resp = test::call_success(&mut mw, TestRequest::default().to_srv_request());
|
||||
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001");
|
||||
}
|
||||
|
||||
@ -206,7 +206,7 @@ mod tests {
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let resp = test::call_success(&mut mw, TestRequest::default().to_service());
|
||||
let resp = test::call_success(&mut mw, TestRequest::default().to_srv_request());
|
||||
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001");
|
||||
}
|
||||
}
|
||||
|
@ -469,44 +469,40 @@ mod tests {
|
||||
header::USER_AGENT,
|
||||
header::HeaderValue::from_static("ACTIX-WEB"),
|
||||
)
|
||||
.to_service();
|
||||
.to_srv_request();
|
||||
let _res = block_on(srv.call(req));
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn test_default_format() {
|
||||
// let format = Format::default();
|
||||
#[test]
|
||||
fn test_default_format() {
|
||||
let mut format = Format::default();
|
||||
|
||||
// let req = TestRequest::with_header(
|
||||
// header::USER_AGENT,
|
||||
// header::HeaderValue::from_static("ACTIX-WEB"),
|
||||
// )
|
||||
// .finish();
|
||||
// let resp = HttpResponse::build(StatusCode::OK).force_close().finish();
|
||||
// let entry_time = time::now();
|
||||
let req = TestRequest::with_header(
|
||||
header::USER_AGENT,
|
||||
header::HeaderValue::from_static("ACTIX-WEB"),
|
||||
)
|
||||
.to_srv_request();
|
||||
|
||||
// let render = |fmt: &mut Formatter| {
|
||||
// for unit in &format.0 {
|
||||
// unit.render(fmt, &req, &resp, entry_time)?;
|
||||
// }
|
||||
// Ok(())
|
||||
// };
|
||||
// let s = format!("{}", FormatDisplay(&render));
|
||||
// assert!(s.contains("GET / HTTP/1.1"));
|
||||
// assert!(s.contains("200 0"));
|
||||
// assert!(s.contains("ACTIX-WEB"));
|
||||
let now = time::now();
|
||||
for unit in &mut format.0 {
|
||||
unit.render_request(now, &req);
|
||||
}
|
||||
|
||||
// let req = TestRequest::with_uri("/?test").finish();
|
||||
// let resp = HttpResponse::build(StatusCode::OK).force_close().finish();
|
||||
// let entry_time = time::now();
|
||||
let resp = HttpResponse::build(StatusCode::OK).force_close().finish();
|
||||
for unit in &mut format.0 {
|
||||
unit.render_response(&resp);
|
||||
}
|
||||
|
||||
// let render = |fmt: &mut Formatter| {
|
||||
// for unit in &format.0 {
|
||||
// unit.render(fmt, &req, &resp, entry_time)?;
|
||||
// }
|
||||
// Ok(())
|
||||
// };
|
||||
// let s = format!("{}", FormatDisplay(&render));
|
||||
// assert!(s.contains("GET /?test HTTP/1.1"));
|
||||
// }
|
||||
let entry_time = time::now();
|
||||
let render = |fmt: &mut Formatter| {
|
||||
for unit in &format.0 {
|
||||
unit.render(fmt, 1024, entry_time)?;
|
||||
}
|
||||
Ok(())
|
||||
};
|
||||
let s = format!("{}", FormatDisplay(&render));
|
||||
assert!(s.contains("GET / HTTP/1.1"));
|
||||
assert!(s.contains("200 1024"));
|
||||
assert!(s.contains("ACTIX-WEB"));
|
||||
}
|
||||
}
|
||||
|
@ -449,3 +449,30 @@ impl<B: MessageBody> fmt::Debug for ServiceResponse<B> {
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::test::TestRequest;
|
||||
use crate::HttpResponse;
|
||||
|
||||
#[test]
|
||||
fn test_fmt_debug() {
|
||||
let req = TestRequest::get()
|
||||
.uri("/index.html?test=1")
|
||||
.header("x-test", "111")
|
||||
.to_srv_request();
|
||||
let s = format!("{:?}", req);
|
||||
assert!(s.contains("ServiceRequest"));
|
||||
assert!(s.contains("test=1"));
|
||||
assert!(s.contains("x-test"));
|
||||
|
||||
let res = HttpResponse::Ok().header("x-test", "111").finish();
|
||||
let res = TestRequest::post()
|
||||
.uri("/index.html?test=1")
|
||||
.to_srv_response(res);
|
||||
|
||||
let s = format!("{:?}", res);
|
||||
assert!(s.contains("ServiceResponse"));
|
||||
assert!(s.contains("x-test"));
|
||||
}
|
||||
}
|
||||
|
12
src/test.rs
12
src/test.rs
@ -320,7 +320,7 @@ impl TestRequest {
|
||||
}
|
||||
|
||||
/// Complete request creation and generate `ServiceRequest` instance
|
||||
pub fn to_service(mut self) -> ServiceRequest<PayloadStream> {
|
||||
pub fn to_srv_request(mut self) -> ServiceRequest<PayloadStream> {
|
||||
let req = self.req.finish();
|
||||
|
||||
ServiceRequest::new(
|
||||
@ -331,16 +331,16 @@ impl TestRequest {
|
||||
)
|
||||
}
|
||||
|
||||
/// Complete request creation and generate `ServiceResponse` instance
|
||||
pub fn to_srv_response<B>(self, res: HttpResponse<B>) -> ServiceResponse<B> {
|
||||
self.to_srv_request().into_response(res)
|
||||
}
|
||||
|
||||
/// Complete request creation and generate `Request` instance
|
||||
pub fn to_request(mut self) -> Request<PayloadStream> {
|
||||
self.req.finish()
|
||||
}
|
||||
|
||||
/// Complete request creation and generate `ServiceResponse` instance
|
||||
pub fn to_response<B>(self, res: HttpResponse<B>) -> ServiceResponse<B> {
|
||||
self.to_service().into_response(res)
|
||||
}
|
||||
|
||||
/// Complete request creation and generate `HttpRequest` instance
|
||||
pub fn to_http_request(mut self) -> HttpRequest {
|
||||
let req = self.req.finish();
|
||||
|
@ -2,8 +2,11 @@ use net2::TcpBuilder;
|
||||
use std::sync::mpsc;
|
||||
use std::{net, thread, time::Duration};
|
||||
|
||||
#[cfg(feature = "ssl")]
|
||||
use openssl::ssl::SslAcceptorBuilder;
|
||||
|
||||
use actix_http::Response;
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use actix_web::{test, web, App, HttpServer};
|
||||
|
||||
fn unused_addr() -> net::SocketAddr {
|
||||
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
|
||||
@ -76,3 +79,77 @@ fn test_start() {
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
let _ = sys.stop();
|
||||
}
|
||||
|
||||
#[cfg(feature = "ssl")]
|
||||
fn ssl_acceptor() -> std::io::Result<SslAcceptorBuilder> {
|
||||
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
||||
// load ssl keys
|
||||
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
||||
builder
|
||||
.set_private_key_file("tests/key.pem", SslFiletype::PEM)
|
||||
.unwrap();
|
||||
builder
|
||||
.set_certificate_chain_file("tests/cert.pem")
|
||||
.unwrap();
|
||||
Ok(builder)
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "ssl")]
|
||||
fn test_start_ssl() {
|
||||
let addr = unused_addr();
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
thread::spawn(move || {
|
||||
let sys = actix_rt::System::new("test");
|
||||
let builder = ssl_acceptor().unwrap();
|
||||
|
||||
let srv = HttpServer::new(|| {
|
||||
App::new().service(
|
||||
web::resource("/").route(web::to(|| Response::Ok().body("test"))),
|
||||
)
|
||||
})
|
||||
.workers(1)
|
||||
.shutdown_timeout(1)
|
||||
.system_exit()
|
||||
.disable_signals()
|
||||
.bind_ssl(format!("{}", addr), builder)
|
||||
.unwrap()
|
||||
.start();
|
||||
|
||||
let _ = tx.send((srv, actix_rt::System::current()));
|
||||
let _ = sys.run();
|
||||
});
|
||||
let (srv, sys) = rx.recv().unwrap();
|
||||
|
||||
let client = test::run_on(|| {
|
||||
use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
|
||||
let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
|
||||
builder.set_verify(SslVerifyMode::NONE);
|
||||
let _ = builder
|
||||
.set_alpn_protos(b"\x02h2\x08http/1.1")
|
||||
.map_err(|e| log::error!("Can not set alpn protocol: {:?}", e));
|
||||
|
||||
Ok::<_, ()>(
|
||||
awc::Client::build()
|
||||
.connector(
|
||||
awc::Connector::new()
|
||||
.ssl(builder.build())
|
||||
.timeout(Duration::from_millis(100))
|
||||
.service(),
|
||||
)
|
||||
.finish(),
|
||||
)
|
||||
})
|
||||
.unwrap();
|
||||
let host = format!("https://{}", addr);
|
||||
|
||||
let response = test::block_on(client.get(host.clone()).send()).unwrap();
|
||||
assert!(response.status().is_success());
|
||||
|
||||
// stop
|
||||
let _ = srv.stop(false);
|
||||
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
let _ = sys.stop();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user