about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs36
-rw-r--r--src/tests.rs14
2 files changed, 25 insertions, 25 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 080e7d9c6670..d25153388927 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -14,7 +14,7 @@
 //! ```rust
 //! use crimp::{Method, Request};
 //!
-//! let response = Request::new(Method::Get, "http://httpbin.org/get").unwrap()
+//! let response = Request::new(Method::Get, "http://httpbin.org/get")
 //!     .user_agent("crimp test suite").unwrap()
 //!     .send().unwrap();
 //!
@@ -65,6 +65,8 @@ pub enum Method {
 /// request its parameters are modified using the various builder
 /// methods until it is consumed by `send()`.
 pub struct Request<'a> {
+    url: &'a str,
+    method: Method,
     handle: Easy,
     headers: List,
     body: Body<'a>,
@@ -101,23 +103,14 @@ pub struct Response<T> {
 
 impl <'a> Request<'a> {
     /// Initiate an HTTP request with the given method and URL.
-    pub fn new(method: Method, url: &str) -> Result<Self, curl::Error> {
-        let mut handle = Easy::new();
-        handle.url(url)?;
-
-        match method {
-            Method::Get    => handle.get(true)?,
-            Method::Post   => handle.post(true)?,
-            Method::Put    => handle.put(true)?,
-            Method::Patch  => handle.custom_request("PATCH")?,
-            Method::Delete => handle.custom_request("DELETE")?,
-        }
-
-        Ok(Request {
-            handle,
+    pub fn new(method: Method, url: &'a str) -> Self {
+        Request {
+            url,
+            method,
+            handle: Easy::new(),
             headers: List::new(),
             body: Body::NoBody,
-        })
+        }
     }
 
     /// Add an HTTP header to a request.
@@ -151,6 +144,17 @@ impl <'a> Request<'a> {
     /// Send the HTTP request and return a response structure
     /// containing the raw body.
     pub fn send(mut self) -> Result<Response<Vec<u8>>, curl::Error> {
+        // Configure request basics:
+        self.handle.url(self.url)?;
+
+        match self.method {
+            Method::Get    => self.handle.get(true)?,
+            Method::Post   => self.handle.post(true)?,
+            Method::Put    => self.handle.put(true)?,
+            Method::Patch  => self.handle.custom_request("PATCH")?,
+            Method::Delete => self.handle.custom_request("DELETE")?,
+        }
+
         // Create structures in which to store the response data:
         let mut headers = HashMap::new();
         let mut body = vec![];
diff --git a/src/tests.rs b/src/tests.rs
index f4e3a393eaae..8067e7cf66e4 100644
--- a/src/tests.rs
+++ b/src/tests.rs
@@ -7,9 +7,7 @@ use serde_json::{Value, json};
 #[test]
 fn test_http_get() {
     let resp = Request::new(Method::Get, "https://httpbin.org/get")
-        .expect("failed to create request")
-        .send()
-        .expect("failed to send request");
+        .send().expect("failed to send request");
 
     assert_eq!(200, resp.status, "response status should be 200 OK");
 }
@@ -17,7 +15,6 @@ fn test_http_get() {
 #[test]
 fn test_http_delete() {
     let resp = Request::new(Method::Delete, "https://httpbin.org/delete")
-        .expect("failed to create request")
         .send().expect("failed to send request");
 
     assert_eq!(200, resp.status, "response status should be 200 OK");
@@ -26,7 +23,6 @@ fn test_http_delete() {
 #[test]
 fn test_http_put() {
     let resp = Request::new(Method::Put, "https://httpbin.org/put")
-        .expect("failed to create request")
         .send().expect("failed to send request");
 
     assert_eq!(200, resp.status, "response status should be 200 OK");
@@ -35,17 +31,18 @@ fn test_http_put() {
 #[test]
 fn test_http_patch() {
     let resp = Request::new(Method::Patch, "https://httpbin.org/patch")
-        .expect("failed to create request")
         .send().expect("failed to send request");
 
     assert_eq!(200, resp.status, "response status should be 200 OK");
 }
 
+// These tests perform various requests with different body payloads
+// and verify that those were received correctly by the remote side.
+
 #[test]
 fn test_http_post() {
     let body = "test body";
     let response = Request::new(Method::Post, "https://httpbin.org/post")
-        .expect("failed to create request")
         .user_agent("crimp test suite").expect("failed to set user-agent")
         .body("text/plain", &body.as_bytes())
         .send().expect("failed to send request")
@@ -65,14 +62,13 @@ fn test_http_post() {
     );
 }
 
-#[test]
+#[cfg(feature = "json")] #[test]
 fn test_http_post_json() {
     let body = json!({
         "purpose": "testing!"
     });
 
     let response = Request::new(Method::Post, "https://httpbin.org/post")
-        .expect("failed to create request")
         .user_agent("crimp test suite").expect("failed to set user-agent")
         .json(&body).expect("request serialization failed")
         .send().expect("failed to send request")