refactor: fix unit tests for new API

pull/7/head
Alexey Galakhov 8 years ago
parent b7557f1baa
commit c6d3dd6404
  1. 10
      src/handshake/client.rs
  2. 19
      src/handshake/headers.rs
  3. 11
      src/handshake/server.rs

@ -187,8 +187,7 @@ fn generate_key() -> String {
mod tests { mod tests {
use super::{Response, generate_key}; use super::{Response, generate_key};
use super::super::machine::TryParse;
use std::io::Cursor;
#[test] #[test]
fn random_keys() { fn random_keys() {
@ -209,10 +208,9 @@ mod tests {
#[test] #[test]
fn response_parsing() { fn response_parsing() {
const data: &'static [u8] = b"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"; 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 (_, resp) = Response::try_parse(data).unwrap().unwrap();
let req = Response::parse(&mut inp).unwrap().unwrap(); assert_eq!(resp.code, 200);
assert_eq!(req.code, 200); assert_eq!(resp.headers.find_first("Content-Type"), Some(&b"text/html"[..]));
assert_eq!(req.headers.find_first("Content-Type"), Some(&b"text/html"[..]));
} }
} }

@ -3,8 +3,10 @@ use std::str::from_utf8;
use std::slice; use std::slice;
use httparse; use httparse;
use httparse::Status;
use error::Result; use error::Result;
use super::machine::TryParse;
// Limit the number of header lines. // Limit the number of header lines.
pub const MAX_HEADERS: usize = 124; pub const MAX_HEADERS: usize = 124;
@ -71,16 +73,15 @@ pub trait FromHttparse<T>: Sized {
fn from_httparse(raw: T) -> Result<Self>; fn from_httparse(raw: T) -> Result<Self>;
} }
/*
impl TryParse for Headers { impl TryParse for Headers {
fn httparse(buf: &[u8]) -> Result<Option<(usize, Self)>> { fn try_parse(buf: &[u8]) -> Result<Option<(usize, Self)>> {
let mut hbuffer = [httparse::EMPTY_HEADER; MAX_HEADERS]; let mut hbuffer = [httparse::EMPTY_HEADER; MAX_HEADERS];
Ok(match httparse::parse_headers(buf, &mut hbuffer)? { Ok(match httparse::parse_headers(buf, &mut hbuffer)? {
Status::Partial => None, Status::Partial => None,
Status::Complete((size, hdr)) => Some((size, Headers::from_httparse(hdr)?)), Status::Complete((size, hdr)) => Some((size, Headers::from_httparse(hdr)?)),
}) })
} }
}*/ }
impl<'b: 'h, 'h> FromHttparse<&'b [httparse::Header<'h>]> for Headers { impl<'b: 'h, 'h> FromHttparse<&'b [httparse::Header<'h>]> for Headers {
fn from_httparse(raw: &'b [httparse::Header<'h>]) -> Result<Self> { fn from_httparse(raw: &'b [httparse::Header<'h>]) -> Result<Self> {
@ -96,8 +97,7 @@ impl<'b: 'h, 'h> FromHttparse<&'b [httparse::Header<'h>]> for Headers {
mod tests { mod tests {
use super::Headers; use super::Headers;
use super::super::machine::TryParse;
use std::io::Cursor;
#[test] #[test]
fn headers() { fn headers() {
@ -106,8 +106,7 @@ mod tests {
Connection: Upgrade\r\n\ Connection: Upgrade\r\n\
Upgrade: websocket\r\n\ Upgrade: websocket\r\n\
\r\n"; \r\n";
let mut inp = Cursor::new(data); let (_, hdr) = Headers::try_parse(data).unwrap().unwrap();
let hdr = Headers::parse(&mut inp).unwrap().unwrap();
assert_eq!(hdr.find_first("Host"), Some(&b"foo.com"[..])); assert_eq!(hdr.find_first("Host"), Some(&b"foo.com"[..]));
assert_eq!(hdr.find_first("Upgrade"), Some(&b"websocket"[..])); assert_eq!(hdr.find_first("Upgrade"), Some(&b"websocket"[..]));
assert_eq!(hdr.find_first("Connection"), Some(&b"Upgrade"[..])); assert_eq!(hdr.find_first("Connection"), Some(&b"Upgrade"[..]));
@ -126,8 +125,7 @@ mod tests {
Sec-WebSocket-ExtenSIONS: permessage-unknown\r\n\ Sec-WebSocket-ExtenSIONS: permessage-unknown\r\n\
Upgrade: websocket\r\n\ Upgrade: websocket\r\n\
\r\n"; \r\n";
let mut inp = Cursor::new(data); let (_, hdr) = Headers::try_parse(data).unwrap().unwrap();
let hdr = Headers::parse(&mut inp).unwrap().unwrap();
let mut iter = hdr.find("Sec-WebSocket-Extensions"); let mut iter = hdr.find("Sec-WebSocket-Extensions");
assert_eq!(iter.next(), Some(&b"permessage-deflate"[..])); assert_eq!(iter.next(), Some(&b"permessage-deflate"[..]));
assert_eq!(iter.next(), Some(&b"permessage-unknown"[..])); assert_eq!(iter.next(), Some(&b"permessage-unknown"[..]));
@ -140,8 +138,7 @@ mod tests {
b"Host: foo.com\r\n\ b"Host: foo.com\r\n\
Connection: Upgrade\r\n\ Connection: Upgrade\r\n\
Upgrade: websocket\r\n"; Upgrade: websocket\r\n";
let mut inp = Cursor::new(data); let hdr = Headers::try_parse(data).unwrap();
let hdr = Headers::parse(&mut inp).unwrap();
assert!(hdr.is_none()); assert!(hdr.is_none());
} }

@ -93,14 +93,12 @@ impl HandshakeRole for ServerHandshake {
mod tests { mod tests {
use super::Request; use super::Request;
use super::super::machine::TryParse;
use std::io::Cursor;
#[test] #[test]
fn request_parsing() { fn request_parsing() {
const data: &'static [u8] = b"GET /script.ws HTTP/1.1\r\nHost: foo.com\r\n\r\n"; 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::try_parse(data).unwrap().unwrap();
let req = Request::parse(&mut inp).unwrap().unwrap();
assert_eq!(req.path, "/script.ws"); assert_eq!(req.path, "/script.ws");
assert_eq!(req.headers.find_first("Host"), Some(&b"foo.com"[..])); 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-Version: 13\r\n\
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\ Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\
\r\n"; \r\n";
let mut inp = Cursor::new(data); let (_, req) = Request::try_parse(data).unwrap().unwrap();
let req = Request::parse(&mut inp).unwrap().unwrap(); let _ = req.reply().unwrap();
let reply = req.reply().unwrap();
} }
} }

Loading…
Cancel
Save