Skip to content

Commit

Permalink
Support binary responses
Browse files Browse the repository at this point in the history
Fixes #42
  • Loading branch information
kornelski authored and lipanski committed Sep 13, 2018
1 parent f3aaffc commit 5eb96d3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,8 @@ impl Mock {
/// let _m = mock("GET", "/").with_body("hello world");
/// ```
///
pub fn with_body(mut self, body: &str) -> Self {
self.response.body = body.to_owned();
pub fn with_body<StrOrBytes: AsRef<[u8]>>(mut self, body: StrOrBytes) -> Self {
self.response.body = body.as_ref().to_owned();

self
}
Expand All @@ -666,9 +666,9 @@ impl Mock {
///
pub fn with_body_from_file(mut self, path: &str) -> Self {
let mut file = File::open(path).unwrap();
let mut body = String::new();
let mut body = Vec::new();

file.read_to_string(&mut body).unwrap();
file.read_to_end(&mut body).unwrap();

self.response.body = body;

Expand Down
4 changes: 2 additions & 2 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use std::fmt;
pub struct Response {
pub status: Status,
pub headers: Vec<(String, String)>,
pub body: String,
pub body: Vec<u8>,
}

impl Default for Response {
fn default() -> Self {
Response {
status: Status::Ok,
headers: Vec::new(),
body: String::new(),
body: Vec::new(),
}
}
}
Expand Down
20 changes: 13 additions & 7 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,26 @@ fn handle_match_mock(request: Request, stream: TcpStream) {
if !found { state.unmatched_requests.push(request); }
}

fn respond<S: Display>(mut stream: TcpStream, status: S, headers: Option<&str>, body: Option<&str>) {
let mut response = format!("HTTP/1.1 {}\r\n", status);
fn respond<S: Display>(stream: TcpStream, status: S, headers: Option<&str>, body: Option<&str>) {
respond_bytes(stream, status, headers, body.map(|s| s.as_bytes()))
}

fn respond_bytes<S: Display>(mut stream: TcpStream, status: S, headers: Option<&str>, body: Option<&[u8]>) {
let mut response = Vec::from(format!("HTTP/1.1 {}\r\n", status));

if let Some(headers) = headers {
response.push_str(headers);
response.extend(headers.as_bytes());
}

if let Some(body) = body {
response.push_str(&format!("content-length: {}\r\n\r\n{}", body.len(), body));
let body = body.as_ref();
response.extend(format!("content-length: {}\r\n\r\n", body.len()).as_bytes());
response.extend(body);
} else {
response.push_str("\r\n");
response.extend(b"\r\n");
}

let _ = stream.write(response.as_bytes());
let _ = stream.write(&response);
let _ = stream.flush();
}

Expand All @@ -164,7 +170,7 @@ fn respond_with_mock(stream: TcpStream, mock: &Mock) {
headers.push_str("\r\n");
}

respond(stream, &mock.response.status, Some(&headers), Some(&mock.response.body));
respond_bytes(stream, &mock.response.status, Some(&headers), Some(&mock.response.body));
}

fn respond_with_mock_not_found(stream: TcpStream) {
Expand Down

0 comments on commit 5eb96d3

Please sign in to comment.