Skip to content

Commit

Permalink
rename test methods; update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Apr 1, 2019
1 parent 15c5a3b commit 34695f4
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 66 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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()`
Expand Down
34 changes: 17 additions & 17 deletions src/middleware/cors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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!(
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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()
Expand All @@ -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"[..],
Expand All @@ -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!(
Expand Down Expand Up @@ -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"[..],
Expand All @@ -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
Expand All @@ -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!(
Expand All @@ -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!(
Expand All @@ -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!(
Expand All @@ -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!(
Expand Down
6 changes: 3 additions & 3 deletions src/middleware/defaultheaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
});
Expand All @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/errhandlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand All @@ -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");
}
}
70 changes: 33 additions & 37 deletions src/middleware/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

// 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 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 req = TestRequest::with_uri("/?test").finish();
// let resp = HttpResponse::build(StatusCode::OK).force_close().finish();
// let entry_time = time::now();

// 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"));
// }
#[test]
fn test_default_format() {
let mut format = Format::default();

let req = TestRequest::with_header(
header::USER_AGENT,
header::HeaderValue::from_static("ACTIX-WEB"),
)
.to_srv_request();

let now = time::now();
for unit in &mut format.0 {
unit.render_request(now, &req);
}

let resp = HttpResponse::build(StatusCode::OK).force_close().finish();
for unit in &mut format.0 {
unit.render_response(&resp);
}

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"));
}
}
27 changes: 27 additions & 0 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 6 additions & 6 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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();
Expand Down
Loading

0 comments on commit 34695f4

Please sign in to comment.