From c6d3dd640408a0d8545305eee5616770597a0933 Mon Sep 17 00:00:00 2001 From: Alexey Galakhov Date: Sat, 11 Mar 2017 02:59:09 +0100 Subject: [PATCH] refactor: fix unit tests for new API --- src/handshake/client.rs | 10 ++++------ src/handshake/headers.rs | 19 ++++++++----------- src/handshake/server.rs | 11 ++++------- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/handshake/client.rs b/src/handshake/client.rs index d24f625..6e54cfb 100644 --- a/src/handshake/client.rs +++ b/src/handshake/client.rs @@ -187,8 +187,7 @@ fn generate_key() -> String { mod tests { use super::{Response, generate_key}; - - use std::io::Cursor; + use super::super::machine::TryParse; #[test] fn random_keys() { @@ -209,10 +208,9 @@ mod tests { #[test] fn response_parsing() { const data: &'static [u8] = b"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"; - let mut inp = Cursor::new(data); - let req = Response::parse(&mut inp).unwrap().unwrap(); - assert_eq!(req.code, 200); - assert_eq!(req.headers.find_first("Content-Type"), Some(&b"text/html"[..])); + let (_, resp) = Response::try_parse(data).unwrap().unwrap(); + assert_eq!(resp.code, 200); + assert_eq!(resp.headers.find_first("Content-Type"), Some(&b"text/html"[..])); } } diff --git a/src/handshake/headers.rs b/src/handshake/headers.rs index c9cb6c1..c765caf 100644 --- a/src/handshake/headers.rs +++ b/src/handshake/headers.rs @@ -3,8 +3,10 @@ use std::str::from_utf8; use std::slice; use httparse; +use httparse::Status; use error::Result; +use super::machine::TryParse; // Limit the number of header lines. pub const MAX_HEADERS: usize = 124; @@ -71,16 +73,15 @@ pub trait FromHttparse: Sized { fn from_httparse(raw: T) -> Result; } -/* impl TryParse for Headers { - fn httparse(buf: &[u8]) -> Result> { + fn try_parse(buf: &[u8]) -> Result> { let mut hbuffer = [httparse::EMPTY_HEADER; MAX_HEADERS]; Ok(match httparse::parse_headers(buf, &mut hbuffer)? { Status::Partial => None, Status::Complete((size, hdr)) => Some((size, Headers::from_httparse(hdr)?)), }) } -}*/ +} impl<'b: 'h, 'h> FromHttparse<&'b [httparse::Header<'h>]> for Headers { fn from_httparse(raw: &'b [httparse::Header<'h>]) -> Result { @@ -96,8 +97,7 @@ impl<'b: 'h, 'h> FromHttparse<&'b [httparse::Header<'h>]> for Headers { mod tests { use super::Headers; - - use std::io::Cursor; + use super::super::machine::TryParse; #[test] fn headers() { @@ -106,8 +106,7 @@ mod tests { Connection: Upgrade\r\n\ Upgrade: websocket\r\n\ \r\n"; - let mut inp = Cursor::new(data); - let hdr = Headers::parse(&mut inp).unwrap().unwrap(); + let (_, hdr) = Headers::try_parse(data).unwrap().unwrap(); assert_eq!(hdr.find_first("Host"), Some(&b"foo.com"[..])); assert_eq!(hdr.find_first("Upgrade"), Some(&b"websocket"[..])); assert_eq!(hdr.find_first("Connection"), Some(&b"Upgrade"[..])); @@ -126,8 +125,7 @@ mod tests { Sec-WebSocket-ExtenSIONS: permessage-unknown\r\n\ Upgrade: websocket\r\n\ \r\n"; - let mut inp = Cursor::new(data); - let hdr = Headers::parse(&mut inp).unwrap().unwrap(); + let (_, hdr) = Headers::try_parse(data).unwrap().unwrap(); let mut iter = hdr.find("Sec-WebSocket-Extensions"); assert_eq!(iter.next(), Some(&b"permessage-deflate"[..])); assert_eq!(iter.next(), Some(&b"permessage-unknown"[..])); @@ -140,8 +138,7 @@ mod tests { b"Host: foo.com\r\n\ Connection: Upgrade\r\n\ Upgrade: websocket\r\n"; - let mut inp = Cursor::new(data); - let hdr = Headers::parse(&mut inp).unwrap(); + let hdr = Headers::try_parse(data).unwrap(); assert!(hdr.is_none()); } diff --git a/src/handshake/server.rs b/src/handshake/server.rs index 880d5f4..3615bf5 100644 --- a/src/handshake/server.rs +++ b/src/handshake/server.rs @@ -93,14 +93,12 @@ impl HandshakeRole for ServerHandshake { mod tests { use super::Request; - - use std::io::Cursor; + use super::super::machine::TryParse; #[test] fn request_parsing() { const data: &'static [u8] = b"GET /script.ws HTTP/1.1\r\nHost: foo.com\r\n\r\n"; - let mut inp = Cursor::new(data); - let req = Request::parse(&mut inp).unwrap().unwrap(); + let (_, req) = Request::try_parse(data).unwrap().unwrap(); assert_eq!(req.path, "/script.ws"); assert_eq!(req.headers.find_first("Host"), Some(&b"foo.com"[..])); } @@ -115,9 +113,8 @@ mod tests { Sec-WebSocket-Version: 13\r\n\ Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\ \r\n"; - let mut inp = Cursor::new(data); - let req = Request::parse(&mut inp).unwrap().unwrap(); - let reply = req.reply().unwrap(); + let (_, req) = Request::try_parse(data).unwrap().unwrap(); + let _ = req.reply().unwrap(); } }