1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-25 09:59:21 +02:00

more tests

This commit is contained in:
Nikolay Kim
2017-10-22 17:33:24 -07:00
parent 26989f5591
commit 5699af9795
10 changed files with 173 additions and 9 deletions

View File

@ -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<Error>> {
self.error.as_ref()
}

View File

@ -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();

View File

@ -98,7 +98,7 @@ impl<A, S> RouteHandler<S> for RouteFactory<A, S>
// 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)
}
}

View File

@ -46,7 +46,7 @@ impl<T, A, H> HttpServer<T, A, H> where H: HttpHandler
{
/// Create new http server with vec of http handlers
pub fn new<U: IntoIterator<Item=H>>(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,

101
src/ws.rs
View File

@ -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"),
}
}
}

View File

@ -245,6 +245,7 @@ fn encode_base64(data: &[u8]) -> String {
}
#[cfg(test)]
mod test {
#![allow(unused_imports, unused_variables, dead_code)]
use super::*;