From 5699af9795fa29666e942b73b0cfb7395951f05c Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 22 Oct 2017 17:33:24 -0700 Subject: [PATCH] more tests --- cov.sh | 4 ++ examples/basic.rs | 4 +- examples/websocket.rs | 2 +- src/httpresponse.rs | 1 + src/logger.rs | 2 +- src/route.rs | 2 +- src/server.rs | 2 +- src/ws.rs | 101 ++++++++++++++++++++++++++++++++++++++++++ src/wsproto.rs | 1 + tests/test_server.rs | 63 ++++++++++++++++++++++++-- 10 files changed, 173 insertions(+), 9 deletions(-) create mode 100644 cov.sh diff --git a/cov.sh b/cov.sh new file mode 100644 index 000000000..8e9fd237b --- /dev/null +++ b/cov.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +for file in target/debug/actix_web-*[^\.d]; do mkdir -p "target/cov/$(basename $file)"; /usr/local/bin/kcov --exclude-pattern=/.cargo,/usr/lib --verify "target/cov/$(basename $file)" "$file"; done && +for file in target/debug/test_*[^\.d]; do mkdir -p "target/cov/$(basename $file)"; /usr/local/bin/kcov --exclude-pattern=/.cargo,/usr/lib --verify "target/cov/$(basename $file)" "$file"; done diff --git a/examples/basic.rs b/examples/basic.rs index e119ce3b3..0614f6214 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -6,13 +6,13 @@ extern crate env_logger; use actix_web::*; /// somple handle -fn index(req: &mut HttpRequest, payload: Payload, state: &()) -> HttpResponse { +fn index(req: &mut HttpRequest, _payload: Payload, state: &()) -> HttpResponse { println!("{:?}", req); httpcodes::HTTPOk.into() } /// handle with path parameters like `/name/{name}/` -fn with_param(req: &mut HttpRequest, payload: Payload, state: &()) -> HttpResponse { +fn with_param(req: &mut HttpRequest, _payload: Payload, state: &()) -> HttpResponse { println!("{:?}", req); HttpResponse::builder(StatusCode::OK) diff --git a/examples/websocket.rs b/examples/websocket.rs index 1c12cc22c..7baffaa53 100644 --- a/examples/websocket.rs +++ b/examples/websocket.rs @@ -19,7 +19,7 @@ impl Route for MyWebSocket { fn request(req: &mut HttpRequest, payload: Payload, ctx: &mut HttpContext) -> RouteResult { - let resp = ws::handshake(&req)?; + let resp = ws::handshake(req)?; ctx.start(resp); ctx.add_stream(ws::WsStream::new(payload)); Reply::async(MyWebSocket) diff --git a/src/httpresponse.rs b/src/httpresponse.rs index 11c480331..ac68b4bc7 100644 --- a/src/httpresponse.rs +++ b/src/httpresponse.rs @@ -107,6 +107,7 @@ impl HttpResponse { /// The `error` which is responsible for this response #[inline] + #[cfg_attr(feature="cargo-clippy", allow(borrowed_box))] pub fn error(&self) -> Option<&Box> { self.error.as_ref() } diff --git a/src/logger.rs b/src/logger.rs index 1936bc437..c418e4364 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -197,7 +197,7 @@ impl<'a> Iterator for FormatParser<'a> { match chr.unwrap() { // Finished parsing, parse buffer. '}' => break, - c => self.object_buffer.push(c.clone()) + c => self.object_buffer.push(c) } chr = self.chars.next(); diff --git a/src/route.rs b/src/route.rs index 02163dbba..787254ed5 100644 --- a/src/route.rs +++ b/src/route.rs @@ -98,7 +98,7 @@ impl RouteHandler for RouteFactory // handle EXPECT header if req.headers().contains_key(header::EXPECT) { - if let Err(resp) = A::expect(&req, &mut ctx) { + if let Err(resp) = A::expect(req, &mut ctx) { return Task::reply(resp) } } diff --git a/src/server.rs b/src/server.rs index aa4ec4b9c..8331b48f5 100644 --- a/src/server.rs +++ b/src/server.rs @@ -46,7 +46,7 @@ impl HttpServer where H: HttpHandler { /// Create new http server with vec of http handlers pub fn new>(handler: U) -> Self { - let apps: Vec<_> = handler.into_iter().map(|h| h.into()).collect(); + let apps: Vec<_> = handler.into_iter().collect(); HttpServer {h: Rc::new(apps), io: PhantomData, diff --git a/src/ws.rs b/src/ws.rs index 64b36f6ee..d278e14e6 100644 --- a/src/ws.rs +++ b/src/ws.rs @@ -329,3 +329,104 @@ impl WsWriter { ) } } + +#[cfg(test)] +mod tests { + use http::{Method, HeaderMap, StatusCode, Uri, Version, HttpTryFrom, header}; + use super::{HttpRequest, SEC_WEBSOCKET_VERSION, SEC_WEBSOCKET_KEY, handshake}; + + #[test] + fn test_handshake() { + let req = HttpRequest::new(Method::POST, Uri::try_from("/").unwrap(), + Version::HTTP_11, HeaderMap::new()); + match handshake(&req) { + Err(err) => assert_eq!(err.status(), StatusCode::METHOD_NOT_ALLOWED), + _ => panic!("should not happen"), + } + + let req = HttpRequest::new(Method::GET, Uri::try_from("/").unwrap(), + Version::HTTP_11, HeaderMap::new()); + match handshake(&req) { + Err(err) => assert_eq!(err.status(), StatusCode::METHOD_NOT_ALLOWED), + _ => panic!("should not happen"), + } + + let mut headers = HeaderMap::new(); + headers.insert(header::UPGRADE, + header::HeaderValue::from_static("test")); + let req = HttpRequest::new(Method::GET, Uri::try_from("/").unwrap(), + Version::HTTP_11, headers); + match handshake(&req) { + Err(err) => assert_eq!(err.status(), StatusCode::METHOD_NOT_ALLOWED), + _ => panic!("should not happen"), + } + + let mut headers = HeaderMap::new(); + headers.insert(header::UPGRADE, + header::HeaderValue::from_static("websocket")); + let req = HttpRequest::new(Method::GET, Uri::try_from("/").unwrap(), + Version::HTTP_11, headers); + match handshake(&req) { + Err(err) => assert_eq!(err.status(), StatusCode::BAD_REQUEST), + _ => panic!("should not happen"), + } + + let mut headers = HeaderMap::new(); + headers.insert(header::UPGRADE, + header::HeaderValue::from_static("websocket")); + headers.insert(header::CONNECTION, + header::HeaderValue::from_static("upgrade")); + let req = HttpRequest::new(Method::GET, Uri::try_from("/").unwrap(), + Version::HTTP_11, headers); + match handshake(&req) { + Err(err) => assert_eq!(err.status(), StatusCode::BAD_REQUEST), + _ => panic!("should not happen"), + } + + let mut headers = HeaderMap::new(); + headers.insert(header::UPGRADE, + header::HeaderValue::from_static("websocket")); + headers.insert(header::CONNECTION, + header::HeaderValue::from_static("upgrade")); + headers.insert(SEC_WEBSOCKET_VERSION, + header::HeaderValue::from_static("5")); + let req = HttpRequest::new(Method::GET, Uri::try_from("/").unwrap(), + Version::HTTP_11, headers); + match handshake(&req) { + Err(err) => assert_eq!(err.status(), StatusCode::BAD_REQUEST), + _ => panic!("should not happen"), + } + + let mut headers = HeaderMap::new(); + headers.insert(header::UPGRADE, + header::HeaderValue::from_static("websocket")); + headers.insert(header::CONNECTION, + header::HeaderValue::from_static("upgrade")); + headers.insert(SEC_WEBSOCKET_VERSION, + header::HeaderValue::from_static("13")); + let req = HttpRequest::new(Method::GET, Uri::try_from("/").unwrap(), + Version::HTTP_11, headers); + match handshake(&req) { + Err(err) => assert_eq!(err.status(), StatusCode::BAD_REQUEST), + _ => panic!("should not happen"), + } + + let mut headers = HeaderMap::new(); + headers.insert(header::UPGRADE, + header::HeaderValue::from_static("websocket")); + headers.insert(header::CONNECTION, + header::HeaderValue::from_static("upgrade")); + headers.insert(SEC_WEBSOCKET_VERSION, + header::HeaderValue::from_static("13")); + headers.insert(SEC_WEBSOCKET_KEY, + header::HeaderValue::from_static("13")); + let req = HttpRequest::new(Method::GET, Uri::try_from("/").unwrap(), + Version::HTTP_11, headers); + match handshake(&req) { + Ok(resp) => { + assert_eq!(resp.status(), StatusCode::SWITCHING_PROTOCOLS) + }, + _ => panic!("should not happen"), + } + } +} diff --git a/src/wsproto.rs b/src/wsproto.rs index 88130911b..a339246c2 100644 --- a/src/wsproto.rs +++ b/src/wsproto.rs @@ -245,6 +245,7 @@ fn encode_base64(data: &[u8]) -> String { } +#[cfg(test)] mod test { #![allow(unused_imports, unused_variables, dead_code)] use super::*; diff --git a/tests/test_server.rs b/tests/test_server.rs index 3f78e4aec..b4dd95763 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -5,10 +5,12 @@ extern crate reqwest; use std::{net, thread}; use std::str::FromStr; -use actix::*; -use actix_web::*; +use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; use tokio_core::net::TcpListener; +use actix::*; +use actix_web::*; fn create_server() -> HttpServer> { HttpServer::new( @@ -24,7 +26,6 @@ fn create_server() -> HttpServer> { fn test_serve() { thread::spawn(|| { let sys = System::new("test"); - let srv = create_server(); srv.serve::<_, ()>("127.0.0.1:58902").unwrap(); sys.run(); @@ -47,3 +48,59 @@ fn test_serve_incoming() { assert!(reqwest::get("http://localhost:58906/").unwrap().status().is_success()); } + +struct MiddlewareTest { + start: Arc, + response: Arc, + finish: Arc, +} + +impl Middleware for MiddlewareTest { + fn start(&self, _: &mut HttpRequest) -> Result<(), HttpResponse> { + self.start.store(self.start.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + Ok(()) + } + + fn response(&self, _: &mut HttpRequest, resp: HttpResponse) -> HttpResponse { + self.response.store(self.response.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + resp + } + + fn finish(&self, _: &mut HttpRequest, _: &HttpResponse) { + self.finish.store(self.finish.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + } +} + +#[test] +fn test_middlewares() { + let num1 = Arc::new(AtomicUsize::new(0)); + let num2 = Arc::new(AtomicUsize::new(0)); + let num3 = Arc::new(AtomicUsize::new(0)); + + let act_num1 = Arc::clone(&num1); + let act_num2 = Arc::clone(&num2); + let act_num3 = Arc::clone(&num3); + + thread::spawn(move || { + let sys = System::new("test"); + + HttpServer::new( + vec![Application::default("/") + .middleware(MiddlewareTest{start: act_num1, + response: act_num2, + finish: act_num3}) + .resource("/", |r| + r.handler(Method::GET, |_, _, _| { + httpcodes::HTTPOk + })) + .finish()]) + .serve::<_, ()>("127.0.0.1:58903").unwrap(); + sys.run(); + }); + + assert!(reqwest::get("http://localhost:58903/").unwrap().status().is_success()); + + assert_eq!(num1.load(Ordering::Relaxed), 1); + assert_eq!(num2.load(Ordering::Relaxed), 1); + assert_eq!(num3.load(Ordering::Relaxed), 1); +}