Skip to content

Commit

Permalink
feat: Add delete method
Browse files Browse the repository at this point in the history
  • Loading branch information
drager committed Aug 8, 2018
1 parent ac30c94 commit 9154caa
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ impl HttperClient {
PayloadBuilder::new(self.request_builder(url, hyper::Method::POST), &self)
}

/// Prepares a delete request to a given url `&str`.
///
/// # Examples
/// ```
/// use httper::client::HttperClient;
///
/// let httper_client = HttperClient::new();
///
/// httper_client.delete("http://localhost:9090").send();
/// ```
pub fn delete(&self, url: &Url) -> PayloadBuilder {
PayloadBuilder::new(self.request_builder(url, hyper::Method::DELETE), &self)
}

/// Performs a put request to a given url `&str` with
/// the provided payload that can be turned into a `hyper::Body`.
///
Expand Down
51 changes: 51 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,55 @@ mod tests {

assert_eq!(data, result.unwrap());
}

#[test]
fn it_should_handle_delete_requests() {
let addr = ([127, 0, 0, 1], 9098).into();

let mut rt = Runtime::new().unwrap();

let buffer: &[u8] = br#"{"name": "Optimus Prime"}"#;

// Spin up a temporary server.
start_server(buffer, &addr);

let httper_client = HttperClient::new();

let result = rt.block_on(
httper_client
.delete(&("http://".to_string() + &addr.to_string()))
.send(),
);

assert!(result.is_ok());
assert_eq!(hyper::StatusCode::OK, result.unwrap().status());
}

#[test]
fn it_should_be_able_to_be_chained_into_json_for_delete() {
let addr = ([127, 0, 0, 1], 9099).into();

let mut rt = Runtime::new().unwrap();

let buffer: &[u8] = br#"{"name": "Optimus Prime"}"#;

// Spin up a temporary server.
start_server(buffer, &addr);

let httper_client = HttperClient::new();

let data = Data {
name: "Optimus Prime".to_string(),
};

let result = rt.block_on(
httper_client
.delete(&("http://".to_string() + &addr.to_string()))
.send()
.json::<Data>(),
);

assert_eq!(data, result.unwrap());
}

}

0 comments on commit 9154caa

Please sign in to comment.