Skip to content

Commit

Permalink
Automatic gzip encoding working with test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Schein committed Feb 21, 2017
1 parent 09a6d38 commit 7a55ec3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 64 deletions.
32 changes: 0 additions & 32 deletions examples/gzip.rs

This file was deleted.

32 changes: 15 additions & 17 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ impl fmt::Debug for RequestBuilder {
}

/// A Response to a submitted `Request`.
#[derive(Debug)]
pub struct Response {
inner: Decoder,
}
Expand Down Expand Up @@ -400,6 +401,7 @@ impl Decoder {
}
}
}

impl Read for Decoder {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self {
Expand All @@ -413,14 +415,20 @@ impl Read for Decoder {
}
}


impl ::std::fmt::Debug for Decoder {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
if let &Decoder::PlainText(ref hyper_response) = self {
return hyper_response.fmt(f);
impl fmt::Debug for Decoder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
return match self {
&Decoder::PlainText(ref hyper_response) => {
hyper_response.fmt(f)
},
&Decoder::Gzip{ref status, ref version, ref headers, ..} => {
f.debug_struct("Gzip")
.field("status", &status)
.field("version", &version)
.field("headers", &headers)
.finish()
}
}

return write!(f, "blank cause lazy")
}
}

Expand All @@ -433,16 +441,6 @@ impl Read for Response {
}
}

impl fmt::Debug for Response {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Response")
.field("status", self.status())
.field("headers", self.headers())
.field("version", self.version())
.finish()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
26 changes: 17 additions & 9 deletions tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,23 @@ fn test_accept_header_is_not_changed_if_set() {

#[test]
fn test_gzip_response() {
let mut e = ::flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::Default);
e.write(b"test request");
let mut encoder = ::flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::Default);
match encoder.write(b"test request") {
Ok(n) => assert!(n > 0, "Failed to write to encoder."),
_ => panic!("Failed to gzip encode string.")
};

let gzipped_content = encoder.finish().unwrap();

let gzipped_content = e.finish().unwrap();
let mut response_d = b"\
let mut response = format!("\
HTTP/1.1 200 OK\r\n\
Server: test-accept\r\n\
Content-Encoding: gzip\r\n\
Content-Length: 0\r\n\
\r\n".to_vec();
Content-Length: {}\r\n\
\r\n", &gzipped_content.len())
.into_bytes();
response.extend(&gzipped_content);

response_d.extend(&gzipped_content);
let server = server! {
request: b"\
GET /gzip HTTP/1.1\r\n\
Expand All @@ -273,13 +278,16 @@ fn test_gzip_response() {
Accept: */*\r\n\
\r\n\
",
response: response_d
response: response
};
let mut res = reqwest::get(&format!("http://{}/gzip", server.addr()))
.unwrap();

let mut body = ::std::string::String::new();
res.read_to_string(&mut body);
match res.read_to_string(&mut body) {
Ok(n) => assert!(n > 0, "Failed to write to buffer."),
_ => panic!("Failed to write to buffer.")
};

assert_eq!(body, "test request");
}
13 changes: 7 additions & 6 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,30 @@ impl Server {
}
}

static DEFAULT_USER_AGENT: &'static str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
static DEFAULT_USER_AGENT: &'static str =
concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));

pub fn spawn(txns: Vec<(Vec<u8>, Vec<u8>)>) -> Server {
let listener = net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = listener.local_addr().unwrap();
thread::spawn(move || {
for (mut expected, reply) in txns {
let (mut socket, _addr) = listener.accept().unwrap();
replace_expected_vars(&mut expected, addr.to_string().as_ref(), DEFAULT_USER_AGENT.as_ref());
replace_expected_vars(&mut expected,
addr.to_string().as_ref(),
DEFAULT_USER_AGENT.as_ref());
let mut buf = [0; 4096];
let n = socket.read(&mut buf).unwrap();

match (::std::str::from_utf8(&expected), ::std::str::from_utf8(&buf[..n])) {
(Ok(expected), Ok(received)) => assert_eq!(expected, received),
_ => assert_eq!(expected, &buf[..n])
_ => assert_eq!(expected, &buf[..n]),
}
socket.write_all(&reply).unwrap();
}
});

Server {
addr: addr,
}
Server { addr: addr }
}

fn replace_expected_vars(bytes: &mut Vec<u8>, host: &[u8], ua: &[u8]) {
Expand Down

0 comments on commit 7a55ec3

Please sign in to comment.