Skip to content

Commit

Permalink
fix server tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Jan 13, 2017
1 parent 4927471 commit 722661e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 41 deletions.
7 changes: 3 additions & 4 deletions src/http/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ impl<I: Io, T: Http1Transaction, K: KeepAlive> Conn<I, T, K> {
match self.state.reading {
Reading::Init |
Reading::Body(..) => self.io.poll_read().is_ready(),
Reading::KeepAlive => true,
Reading::Closed => false,
Reading::KeepAlive | Reading::Closed => true,
}
}

Expand Down Expand Up @@ -115,14 +114,14 @@ impl<I: Io, T: Http1Transaction, K: KeepAlive> Conn<I, T, K> {
return Ok(Async::Ready(Some(Frame::Error { error: e })));
}
};
self.state.busy();
let wants_keep_alive = head.should_keep_alive();
self.state.keep_alive &= wants_keep_alive;
let (body, reading) = if decoder.is_eof() {
(false, Reading::KeepAlive)
} else {
(true, Reading::Body(decoder))
};
self.state.busy();
self.state.reading = reading;
return Ok(Async::Ready(Some(Frame::Message { message: head, body: body })));
},
Expand Down Expand Up @@ -470,7 +469,7 @@ impl KeepAlive for KA {

impl<K: KeepAlive> State<K> {
fn close(&mut self) {
trace!("State::close");
trace!("State::close()");
self.reading = Reading::Closed;
self.writing = Writing::Closed;
self.keep_alive.disable();
Expand Down
44 changes: 7 additions & 37 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
extern crate hyper;
extern crate futures;
extern crate spmc;
extern crate pretty_env_logger;

use futures::Future;
use futures::stream::Stream;

use std::net::{TcpStream, SocketAddr};
use std::io::{Read, Write};
use std::sync::mpsc;
use std::thread;
use std::time::Duration;

use hyper::server::{Server, Request, Response, Service, NewService};
Expand All @@ -25,12 +27,6 @@ impl Serve {
self.listening.as_ref().unwrap().addr()
}

/*
fn head(&self) -> Request {
unimplemented!()
}
*/

fn body(&self) -> Vec<u8> {
let mut buf = vec![];
while let Ok(Msg::Chunk(msg)) = self.msg_rx.try_recv() {
Expand Down Expand Up @@ -152,7 +148,7 @@ fn serve() -> Serve {
}

fn serve_with_timeout(dur: Option<Duration>) -> Serve {
use std::thread;
let _ = pretty_env_logger::init();

let (thread_tx, thread_rx) = mpsc::channel();
let (spawn_tx, spawn_rx) = mpsc::channel();
Expand Down Expand Up @@ -195,7 +191,7 @@ fn server_get_should_ignore_body() {
req.write_all(b"\
GET / HTTP/1.1\r\n\
Host: example.domain\r\n\
Connection: close\r\n
Connection: close\r\n\
\r\n\
I shouldn't be read.\r\n\
").unwrap();
Expand Down Expand Up @@ -223,7 +219,6 @@ fn server_get_with_body() {

#[test]
fn server_get_fixed_response() {

let foo_bar = b"foo bar baz";
let server = serve();
server.reply()
Expand Down Expand Up @@ -256,7 +251,7 @@ fn server_get_chunked_response() {
req.write_all(b"\
GET / HTTP/1.1\r\n\
Host: example.domain\r\n\
Connection: close\r\n
Connection: close\r\n\
\r\n\
").unwrap();
let mut body = String::new();
Expand Down Expand Up @@ -358,8 +353,8 @@ fn server_empty_response_chunked() {
req.write_all(b"\
GET / HTTP/1.1\r\n\
Host: example.domain\r\n\
Content-Length: 0\r\n
Connection: close\r\n
Content-Length: 0\r\n\
Connection: close\r\n\
\r\n\
").unwrap();

Expand Down Expand Up @@ -462,28 +457,3 @@ fn server_keep_alive() {
}
}
}

/*
#[test]
fn server_get_with_body_three_listeners() {
let server = serve_n(3);
let addrs = server.addrs();
assert_eq!(addrs.len(), 3);
for (i, addr) in addrs.iter().enumerate() {
let mut req = TcpStream::connect(addr).unwrap();
write!(req, "\
GET / HTTP/1.1\r\n\
Host: example.domain\r\n\
Content-Length: 17\r\n\
\r\n\
I'm sending to {}.\r\n\
", i).unwrap();
req.read(&mut [0; 256]).unwrap();
// note: doesnt include trailing \r\n, cause Content-Length wasn't 19
let comparison = format!("I'm sending to {}.", i).into_bytes();
assert_eq!(server.body(), comparison);
}
}
*/

0 comments on commit 722661e

Please sign in to comment.