669 lines
		
	
	
		
			19 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			669 lines
		
	
	
		
			19 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| use std::fmt;
 | |
| 
 | |
| use base64::{encode};
 | |
| use serde::Serialize;
 | |
| use serde_json;
 | |
| use serde_urlencoded;
 | |
| 
 | |
| use super::body::{Body};
 | |
| use super::multipart;
 | |
| use super::client::{Client, Pending};
 | |
| use header::{CONTENT_LENGTH, CONTENT_TYPE, HeaderMap, HeaderName, HeaderValue};
 | |
| use http::HttpTryFrom;
 | |
| use {Method, Url};
 | |
| 
 | |
| /// A request which can be executed with `Client::execute()`.
 | |
| pub struct Request {
 | |
|     method: Method,
 | |
|     url: Url,
 | |
|     headers: HeaderMap,
 | |
|     body: Option<Body>,
 | |
| }
 | |
| 
 | |
| /// A builder to construct the properties of a `Request`.
 | |
| pub struct RequestBuilder {
 | |
|     client: Client,
 | |
|     request: ::Result<Request>,
 | |
| }
 | |
| 
 | |
| impl Request {
 | |
|     /// Constructs a new request.
 | |
|     #[inline]
 | |
|     pub fn new(method: Method, url: Url) -> Self {
 | |
|         Request {
 | |
|             method,
 | |
|             url,
 | |
|             headers: HeaderMap::new(),
 | |
|             body: None,
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /// Get the method.
 | |
|     #[inline]
 | |
|     pub fn method(&self) -> &Method {
 | |
|         &self.method
 | |
|     }
 | |
| 
 | |
|     /// Get a mutable reference to the method.
 | |
|     #[inline]
 | |
|     pub fn method_mut(&mut self) -> &mut Method {
 | |
|         &mut self.method
 | |
|     }
 | |
| 
 | |
|     /// Get the url.
 | |
|     #[inline]
 | |
|     pub fn url(&self) -> &Url {
 | |
|         &self.url
 | |
|     }
 | |
| 
 | |
|     /// Get a mutable reference to the url.
 | |
|     #[inline]
 | |
|     pub fn url_mut(&mut self) -> &mut Url {
 | |
|         &mut self.url
 | |
|     }
 | |
| 
 | |
|     /// Get the headers.
 | |
|     #[inline]
 | |
|     pub fn headers(&self) -> &HeaderMap {
 | |
|         &self.headers
 | |
|     }
 | |
| 
 | |
|     /// Get a mutable reference to the headers.
 | |
|     #[inline]
 | |
|     pub fn headers_mut(&mut self) -> &mut HeaderMap {
 | |
|         &mut self.headers
 | |
|     }
 | |
| 
 | |
|     /// Get the body.
 | |
|     #[inline]
 | |
|     pub fn body(&self) -> Option<&Body> {
 | |
|         self.body.as_ref()
 | |
|     }
 | |
| 
 | |
|     /// Get a mutable reference to the body.
 | |
|     #[inline]
 | |
|     pub fn body_mut(&mut self) -> &mut Option<Body> {
 | |
|         &mut self.body
 | |
|     }
 | |
| 
 | |
|     pub(super) fn pieces(self) -> (Method, Url, HeaderMap, Option<Body>) {
 | |
|         (self.method, self.url, self.headers, self.body)
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl RequestBuilder {
 | |
|     pub(super) fn new(client: Client, request: ::Result<Request>) -> RequestBuilder {
 | |
|         RequestBuilder {
 | |
|             client,
 | |
|             request,
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /// Add a `Header` to this Request.
 | |
|     pub fn header<K, V>(mut self, key: K, value: V) -> RequestBuilder
 | |
|     where
 | |
|         HeaderName: HttpTryFrom<K>,
 | |
|         HeaderValue: HttpTryFrom<V>,
 | |
|     {
 | |
|         let mut error = None;
 | |
|         if let Ok(ref mut req) = self.request {
 | |
|             match <HeaderName as HttpTryFrom<K>>::try_from(key) {
 | |
|                 Ok(key) => {
 | |
|                     match <HeaderValue as HttpTryFrom<V>>::try_from(value) {
 | |
|                         Ok(value) => { req.headers_mut().append(key, value); }
 | |
|                         Err(e) => error = Some(::error::from(e.into())),
 | |
|                     }
 | |
|                 },
 | |
|                 Err(e) => error = Some(::error::from(e.into())),
 | |
|             };
 | |
|         }
 | |
|         if let Some(err) = error {
 | |
|             self.request = Err(err);
 | |
|         }
 | |
|         self
 | |
|     }
 | |
| 
 | |
|     /// Add a set of Headers to the existing ones on this Request.
 | |
|     ///
 | |
|     /// The headers will be merged in to any already set.
 | |
|     pub fn headers(mut self, headers: ::header::HeaderMap) -> RequestBuilder {
 | |
|         if let Ok(ref mut req) = self.request {
 | |
|             replace_headers(req.headers_mut(), headers);
 | |
|         }
 | |
|         self
 | |
|     }
 | |
| 
 | |
|     /// Set a header with a type implementing hyper v0.11's `Header` trait.
 | |
|     ///
 | |
|     /// This method is provided to ease migration, and requires the `hyper-011`
 | |
|     /// Cargo feature enabled on `reqwest`.
 | |
|     #[cfg(feature = "hyper-011")]
 | |
|     pub fn header_011<H>(self, header: H) -> RequestBuilder
 | |
|     where
 | |
|         H: ::hyper_011::header::Header,
 | |
|     {
 | |
|         let mut headers = ::hyper_011::Headers::new();
 | |
|         headers.set(header);
 | |
|         let map = ::header::HeaderMap::from(headers);
 | |
|         self.headers(map)
 | |
|     }
 | |
| 
 | |
|     /// Set multiple headers using hyper v0.11's `Headers` map.
 | |
|     ///
 | |
|     /// This method is provided to ease migration, and requires the `hyper-011`
 | |
|     /// Cargo feature enabled on `reqwest`.
 | |
|     #[cfg(feature = "hyper-011")]
 | |
|     pub fn headers_011(self, headers: ::hyper_011::Headers) -> RequestBuilder {
 | |
|         let map = ::header::HeaderMap::from(headers);
 | |
|         self.headers(map)
 | |
|     }
 | |
| 
 | |
|     /// Enable HTTP basic authentication.
 | |
|     pub fn basic_auth<U, P>(self, username: U, password: Option<P>) -> RequestBuilder
 | |
|     where
 | |
|         U: fmt::Display,
 | |
|         P: fmt::Display,
 | |
|     {
 | |
|         let auth = match password {
 | |
|             Some(password) => format!("{}:{}", username, password),
 | |
|             None => format!("{}:", username)
 | |
|         };
 | |
|         let header_value = format!("basic {}", encode(&auth));
 | |
|         self.header(::header::AUTHORIZATION, &*header_value)
 | |
|     }
 | |
| 
 | |
|     /// Set the request body.
 | |
|     pub fn body<T: Into<Body>>(mut self, body: T) -> RequestBuilder {
 | |
|         if let Ok(ref mut req) = self.request {
 | |
|             *req.body_mut() = Some(body.into());
 | |
|         }
 | |
|         self
 | |
|     }
 | |
| 
 | |
|     /// Sends a multipart/form-data body.
 | |
|     ///
 | |
|     /// ```
 | |
|     /// # extern crate futures;
 | |
|     /// # extern crate reqwest;
 | |
|     ///
 | |
|     /// # use reqwest::Error;
 | |
|     /// # use futures::future::Future;
 | |
|     ///
 | |
|     /// # fn run() -> Result<(), Error> {
 | |
|     /// let client = reqwest::async::Client::new();
 | |
|     /// let form = reqwest::async::multipart::Form::new()
 | |
|     ///     .text("key3", "value3")
 | |
|     ///     .text("key4", "value4");
 | |
|     ///
 | |
|     /// let mut rt = tokio::runtime::current_thread::Runtime::new().expect("new rt");
 | |
|     ///
 | |
|     /// let response = client.post("your url")
 | |
|     ///     .multipart(form)
 | |
|     ///     .send()
 | |
|     ///     .and_then(|_| {
 | |
|     ///       Ok(())
 | |
|     ///     });
 | |
|     ///
 | |
|     /// rt.block_on(response)
 | |
|     /// # }
 | |
|     /// ```
 | |
|     pub fn multipart(self, mut multipart: multipart::Form) -> RequestBuilder {
 | |
|         let mut builder = self.header(
 | |
|             CONTENT_TYPE,
 | |
|             format!(
 | |
|                 "multipart/form-data; boundary={}",
 | |
|                 multipart.boundary()
 | |
|             ).as_str()
 | |
|         );
 | |
| 
 | |
|         builder = match multipart.compute_length() {
 | |
|             Some(length) => builder.header(CONTENT_LENGTH, length),
 | |
|             None => builder,
 | |
|         };
 | |
| 
 | |
|         if let Ok(ref mut req) = builder.request {
 | |
|             *req.body_mut() = Some(Body::wrap(multipart.stream()))
 | |
|         }
 | |
|         builder
 | |
|     }
 | |
| 
 | |
|     /// Modify the query string of the URL.
 | |
|     ///
 | |
|     /// Modifies the URL of this request, adding the parameters provided.
 | |
|     /// This method appends and does not overwrite. This means that it can
 | |
|     /// be called multiple times and that existing query parameters are not
 | |
|     /// overwritten if the same key is used. The key will simply show up
 | |
|     /// twice in the query string.
 | |
|     /// Calling `.query([("foo", "a"), ("foo", "b")])` gives `"foo=a&foo=b"`.
 | |
|     ///
 | |
|     /// # Note
 | |
|     /// This method does not support serializing a single key-value
 | |
|     /// pair. Instead of using `.query(("key", "val"))`, use a sequence, such
 | |
|     /// as `.query(&[("key", "val")])`. It's also possible to serialize structs
 | |
|     /// and maps into a key-value pair.
 | |
|     ///
 | |
|     /// # Errors
 | |
|     /// This method will fail if the object you provide cannot be serialized
 | |
|     /// into a query string.
 | |
|     pub fn query<T: Serialize + ?Sized>(mut self, query: &T) -> RequestBuilder {
 | |
|         let mut error = None;
 | |
|         if let Ok(ref mut req) = self.request {
 | |
|             let url = req.url_mut();
 | |
|             let mut pairs = url.query_pairs_mut();
 | |
|             let serializer = serde_urlencoded::Serializer::new(&mut pairs);
 | |
| 
 | |
|             if let Err(err) = query.serialize(serializer) {
 | |
|                 error = Some(::error::from(err));
 | |
|             }
 | |
|         }
 | |
|         if let Some(err) = error {
 | |
|             self.request = Err(err);
 | |
|         }
 | |
|         self
 | |
|     }
 | |
| 
 | |
|     /// Send a form body.
 | |
|     pub fn form<T: Serialize + ?Sized>(mut self, form: &T) -> RequestBuilder {
 | |
|         let mut error = None;
 | |
|         if let Ok(ref mut req) = self.request {
 | |
|             match serde_urlencoded::to_string(form) {
 | |
|                 Ok(body) => {
 | |
|                     req.headers_mut().insert(
 | |
|                         CONTENT_TYPE,
 | |
|                         HeaderValue::from_static("application/x-www-form-urlencoded")
 | |
|                     );
 | |
|                     *req.body_mut() = Some(body.into());
 | |
|                 },
 | |
|                 Err(err) => error = Some(::error::from(err)),
 | |
|             }
 | |
|         }
 | |
|         if let Some(err) = error {
 | |
|             self.request = Err(err);
 | |
|         }
 | |
|         self
 | |
|     }
 | |
| 
 | |
|     /// Send a JSON body.
 | |
|     ///
 | |
|     /// # Errors
 | |
|     ///
 | |
|     /// Serialization can fail if `T`'s implementation of `Serialize` decides to
 | |
|     /// fail, or if `T` contains a map with non-string keys.
 | |
|     pub fn json<T: Serialize + ?Sized>(mut self, json: &T) -> RequestBuilder {
 | |
|         let mut error = None;
 | |
|         if let Ok(ref mut req) = self.request {
 | |
|             match serde_json::to_vec(json) {
 | |
|                 Ok(body) => {
 | |
|                     req.headers_mut().insert(
 | |
|                         CONTENT_TYPE,
 | |
|                         HeaderValue::from_static("application/json")
 | |
|                     );
 | |
|                     *req.body_mut() = Some(body.into());
 | |
|                 },
 | |
|                 Err(err) => error = Some(::error::from(err)),
 | |
|             }
 | |
|         }
 | |
|         if let Some(err) = error {
 | |
|             self.request = Err(err);
 | |
|         }
 | |
|         self
 | |
|     }
 | |
| 
 | |
|     /// Build a `Request`, which can be inspected, modified and executed with
 | |
|     /// `Client::execute()`.
 | |
|     pub fn build(self) -> ::Result<Request> {
 | |
|         self.request
 | |
|     }
 | |
| 
 | |
|     /// Constructs the Request and sends it the target URL, returning a Response.
 | |
|     ///
 | |
|     /// # Errors
 | |
|     ///
 | |
|     /// This method fails if there was an error while sending request,
 | |
|     /// redirect loop was detected or redirect limit was exhausted.
 | |
|     pub fn send(self) -> Pending {
 | |
|         match self.request {
 | |
|             Ok(req) => self.client.execute(req),
 | |
|             Err(err) => Pending::new_err(err),
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl fmt::Debug for Request {
 | |
|     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 | |
|         fmt_request_fields(&mut f.debug_struct("Request"), self)
 | |
|             .finish()
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl fmt::Debug for RequestBuilder {
 | |
|     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 | |
|         let mut builder = f.debug_struct("RequestBuilder");
 | |
|         match self.request {
 | |
|             Ok(ref req) => {
 | |
|                 fmt_request_fields(&mut builder, req)
 | |
|                     .finish()
 | |
|             },
 | |
|             Err(ref err) => {
 | |
|                 builder
 | |
|                     .field("error", err)
 | |
|                     .finish()
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| fn fmt_request_fields<'a, 'b>(f: &'a mut fmt::DebugStruct<'a, 'b>, req: &Request) -> &'a mut fmt::DebugStruct<'a, 'b> {
 | |
|     f.field("method", &req.method)
 | |
|         .field("url", &req.url)
 | |
|         .field("headers", &req.headers)
 | |
| }
 | |
| 
 | |
| pub(crate) fn replace_headers(dst: &mut HeaderMap, src: HeaderMap) {
 | |
| 
 | |
|     // IntoIter of HeaderMap yields (Option<HeaderName>, HeaderValue).
 | |
|     // The first time a name is yielded, it will be Some(name), and if
 | |
|     // there are more values with the same name, the next yield will be
 | |
|     // None.
 | |
|     //
 | |
|     // TODO: a complex exercise would be to optimize this to only
 | |
|     // require 1 hash/lookup of the key, but doing something fancy
 | |
|     // with header::Entry...
 | |
| 
 | |
|     let mut prev_name = None;
 | |
|     for (key, value) in src {
 | |
|         match key {
 | |
|             Some(key) => {
 | |
|                 dst.insert(key.clone(), value);
 | |
|                 prev_name = Some(key);
 | |
|             },
 | |
|             None => match prev_name {
 | |
|                 Some(ref key) => {
 | |
|                     dst.append(key.clone(), value);
 | |
|                 },
 | |
|                 None => unreachable!("HeaderMap::into_iter yielded None first"),
 | |
|             },
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| #[cfg(test)]
 | |
| mod tests {
 | |
|     use super::Client;
 | |
|     use std::collections::BTreeMap;
 | |
| 
 | |
|     #[test]
 | |
|     fn add_query_append() {
 | |
|         let client = Client::new();
 | |
|         let some_url = "https://google.com/";
 | |
|         let r = client.get(some_url);
 | |
| 
 | |
|         let r = r.query(&[("foo", "bar")]);
 | |
|         let r = r.query(&[("qux", 3)]);
 | |
| 
 | |
|         let req = r.build().expect("request is valid");
 | |
|         assert_eq!(req.url().query(), Some("foo=bar&qux=3"));
 | |
|     }
 | |
| 
 | |
|     #[test]
 | |
|     fn add_query_append_same() {
 | |
|         let client = Client::new();
 | |
|         let some_url = "https://google.com/";
 | |
|         let r = client.get(some_url);
 | |
| 
 | |
|         let r = r.query(&[("foo", "a"), ("foo", "b")]);
 | |
| 
 | |
|         let req = r.build().expect("request is valid");
 | |
|         assert_eq!(req.url().query(), Some("foo=a&foo=b"));
 | |
|     }
 | |
| 
 | |
|     #[test]
 | |
|     fn add_query_struct() {
 | |
|         #[derive(Serialize)]
 | |
|         struct Params {
 | |
|             foo: String,
 | |
|             qux: i32,
 | |
|         }
 | |
| 
 | |
|         let client = Client::new();
 | |
|         let some_url = "https://google.com/";
 | |
|         let r = client.get(some_url);
 | |
| 
 | |
|         let params = Params { foo: "bar".into(), qux: 3 };
 | |
| 
 | |
|         let r = r.query(¶ms);
 | |
| 
 | |
|         let req = r.build().expect("request is valid");
 | |
|         assert_eq!(req.url().query(), Some("foo=bar&qux=3"));
 | |
|     }
 | |
| 
 | |
|     #[test]
 | |
|     fn add_query_map() {
 | |
|         let mut params = BTreeMap::new();
 | |
|         params.insert("foo", "bar");
 | |
|         params.insert("qux", "three");
 | |
| 
 | |
|         let client = Client::new();
 | |
|         let some_url = "https://google.com/";
 | |
|         let r = client.get(some_url);
 | |
| 
 | |
|         let r = r.query(¶ms);
 | |
| 
 | |
|         let req = r.build().expect("request is valid");
 | |
|         assert_eq!(req.url().query(), Some("foo=bar&qux=three"));
 | |
|     }
 | |
| 
 | |
|     #[test]
 | |
|     fn test_replace_headers() {
 | |
|         use http::HeaderMap;
 | |
| 
 | |
|         let mut headers = HeaderMap::new();
 | |
|         headers.insert("foo", "bar".parse().unwrap());
 | |
|         headers.append("foo", "baz".parse().unwrap());
 | |
| 
 | |
|         let client = Client::new();
 | |
|         let req = client
 | |
|             .get("https://hyper.rs")
 | |
|             .header("im-a", "keeper")
 | |
|             .header("foo", "pop me")
 | |
|             .headers(headers)
 | |
|             .build()
 | |
|             .expect("request build");
 | |
| 
 | |
|         assert_eq!(req.headers()["im-a"], "keeper");
 | |
| 
 | |
|         let foo = req
 | |
|             .headers()
 | |
|             .get_all("foo")
 | |
|             .iter()
 | |
|             .collect::<Vec<_>>();
 | |
|         assert_eq!(foo.len(), 2);
 | |
|         assert_eq!(foo[0], "bar");
 | |
|         assert_eq!(foo[1], "baz");
 | |
|     }
 | |
| 
 | |
|     /*
 | |
|     use {body, Method};
 | |
|     use super::Client;
 | |
|     use header::{Host, Headers, ContentType};
 | |
|     use std::collections::HashMap;
 | |
|     use serde_urlencoded;
 | |
|     use serde_json;
 | |
| 
 | |
|     #[test]
 | |
|     fn basic_get_request() {
 | |
|         let client = Client::new().unwrap();
 | |
|         let some_url = "https://google.com/";
 | |
|         let r = client.get(some_url).unwrap().build();
 | |
| 
 | |
|         assert_eq!(r.method, Method::Get);
 | |
|         assert_eq!(r.url.as_str(), some_url);
 | |
|     }
 | |
| 
 | |
|     #[test]
 | |
|     fn basic_head_request() {
 | |
|         let client = Client::new().unwrap();
 | |
|         let some_url = "https://google.com/";
 | |
|         let r = client.head(some_url).unwrap().build();
 | |
| 
 | |
|         assert_eq!(r.method, Method::Head);
 | |
|         assert_eq!(r.url.as_str(), some_url);
 | |
|     }
 | |
| 
 | |
|     #[test]
 | |
|     fn basic_post_request() {
 | |
|         let client = Client::new().unwrap();
 | |
|         let some_url = "https://google.com/";
 | |
|         let r = client.post(some_url).unwrap().build();
 | |
| 
 | |
|         assert_eq!(r.method, Method::Post);
 | |
|         assert_eq!(r.url.as_str(), some_url);
 | |
|     }
 | |
| 
 | |
|     #[test]
 | |
|     fn basic_put_request() {
 | |
|         let client = Client::new().unwrap();
 | |
|         let some_url = "https://google.com/";
 | |
|         let r = client.put(some_url).unwrap().build();
 | |
| 
 | |
|         assert_eq!(r.method, Method::Put);
 | |
|         assert_eq!(r.url.as_str(), some_url);
 | |
|     }
 | |
| 
 | |
|     #[test]
 | |
|     fn basic_patch_request() {
 | |
|         let client = Client::new().unwrap();
 | |
|         let some_url = "https://google.com/";
 | |
|         let r = client.patch(some_url).unwrap().build();
 | |
| 
 | |
|         assert_eq!(r.method, Method::Patch);
 | |
|         assert_eq!(r.url.as_str(), some_url);
 | |
|     }
 | |
| 
 | |
|     #[test]
 | |
|     fn basic_delete_request() {
 | |
|         let client = Client::new().unwrap();
 | |
|         let some_url = "https://google.com/";
 | |
|         let r = client.delete(some_url).unwrap().build();
 | |
| 
 | |
|         assert_eq!(r.method, Method::Delete);
 | |
|         assert_eq!(r.url.as_str(), some_url);
 | |
|     }
 | |
| 
 | |
|     #[test]
 | |
|     fn add_header() {
 | |
|         let client = Client::new().unwrap();
 | |
|         let some_url = "https://google.com/";
 | |
|         let mut r = client.post(some_url).unwrap();
 | |
| 
 | |
|         let header = Host {
 | |
|             hostname: "google.com".to_string(),
 | |
|             port: None,
 | |
|         };
 | |
| 
 | |
|         // Add a copy of the header to the request builder
 | |
|         let r = r.header(header.clone()).build();
 | |
| 
 | |
|         // then check it was actually added
 | |
|         assert_eq!(r.headers.get::<Host>(), Some(&header));
 | |
|     }
 | |
| 
 | |
|     #[test]
 | |
|     fn add_headers() {
 | |
|         let client = Client::new().unwrap();
 | |
|         let some_url = "https://google.com/";
 | |
|         let mut r = client.post(some_url).unwrap();
 | |
| 
 | |
|         let header = Host {
 | |
|             hostname: "google.com".to_string(),
 | |
|             port: None,
 | |
|         };
 | |
| 
 | |
|         let mut headers = Headers::new();
 | |
|         headers.set(header);
 | |
| 
 | |
|         // Add a copy of the headers to the request builder
 | |
|         let r = r.headers(headers.clone()).build();
 | |
| 
 | |
|         // then make sure they were added correctly
 | |
|         assert_eq!(r.headers, headers);
 | |
|     }
 | |
| 
 | |
|     #[test]
 | |
|     fn add_body() {
 | |
|         let client = Client::new().unwrap();
 | |
|         let some_url = "https://google.com/";
 | |
|         let mut r = client.post(some_url).unwrap();
 | |
| 
 | |
|         let body = "Some interesting content";
 | |
| 
 | |
|         let r = r.body(body).build();
 | |
| 
 | |
|         let buf = body::read_to_string(r.body.unwrap()).unwrap();
 | |
| 
 | |
|         assert_eq!(buf, body);
 | |
|     }
 | |
| 
 | |
|     #[test]
 | |
|     fn add_form() {
 | |
|         let client = Client::new().unwrap();
 | |
|         let some_url = "https://google.com/";
 | |
|         let mut r = client.post(some_url).unwrap();
 | |
| 
 | |
|         let mut form_data = HashMap::new();
 | |
|         form_data.insert("foo", "bar");
 | |
| 
 | |
|         let r = r.form(&form_data).unwrap().build();
 | |
| 
 | |
|         // Make sure the content type was set
 | |
|         assert_eq!(r.headers.get::<ContentType>(),
 | |
|                    Some(&ContentType::form_url_encoded()));
 | |
| 
 | |
|         let buf = body::read_to_string(r.body.unwrap()).unwrap();
 | |
| 
 | |
|         let body_should_be = serde_urlencoded::to_string(&form_data).unwrap();
 | |
|         assert_eq!(buf, body_should_be);
 | |
|     }
 | |
| 
 | |
|     #[test]
 | |
|     fn add_json() {
 | |
|         let client = Client::new().unwrap();
 | |
|         let some_url = "https://google.com/";
 | |
|         let mut r = client.post(some_url).unwrap();
 | |
| 
 | |
|         let mut json_data = HashMap::new();
 | |
|         json_data.insert("foo", "bar");
 | |
| 
 | |
|         let r = r.json(&json_data).unwrap().build();
 | |
| 
 | |
|         // Make sure the content type was set
 | |
|         assert_eq!(r.headers.get::<ContentType>(), Some(&ContentType::json()));
 | |
| 
 | |
|         let buf = body::read_to_string(r.body.unwrap()).unwrap();
 | |
| 
 | |
|         let body_should_be = serde_json::to_string(&json_data).unwrap();
 | |
|         assert_eq!(buf, body_should_be);
 | |
|     }
 | |
| 
 | |
|     #[test]
 | |
|     fn add_json_fail() {
 | |
|         use serde::{Serialize, Serializer};
 | |
|         use serde::ser::Error;
 | |
|         struct MyStruct;
 | |
|         impl Serialize for MyStruct {
 | |
|             fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
 | |
|                 where S: Serializer
 | |
|                 {
 | |
|                     Err(S::Error::custom("nope"))
 | |
|                 }
 | |
|         }
 | |
| 
 | |
|         let client = Client::new().unwrap();
 | |
|         let some_url = "https://google.com/";
 | |
|         let mut r = client.post(some_url).unwrap();
 | |
|         let json_data = MyStruct{};
 | |
|         assert!(r.json(&json_data).unwrap_err().is_serialization());
 | |
|     }
 | |
|     */
 | |
| }
 |