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 {
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"[..]));
}
}

@ -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<T>: Sized {
fn from_httparse(raw: T) -> Result<Self>;
}
/*
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];
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<Self> {
@ -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());
}

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

Loading…
Cancel
Save