1use std::convert::TryFrom;
2use std::fmt;
3use std::future::Future;
4use std::time::Duration;
5
6use serde::Serialize;
7#[cfg(feature = "json")]
8use serde_json;
9
10use super::body::Body;
11use super::client::{Client, Pending};
12#[cfg(feature = "multipart")]
13use super::multipart;
14use super::response::Response;
15#[cfg(feature = "multipart")]
16use crate::header::CONTENT_LENGTH;
17use crate::header::{HeaderMap, HeaderName, HeaderValue, CONTENT_TYPE};
18use crate::{Method, Url};
19use http::{request::Parts, Request as HttpRequest, Version};
20
21/// A request which can be executed with `Client::execute()`.
22pub struct Request {
23 method: Method,
24 url: Url,
25 headers: HeaderMap,
26 body: Option<Body>,
27 timeout: Option<Duration>,
28 version: Version,
29}
30
31/// A builder to construct the properties of a `Request`.
32///
33/// To construct a `RequestBuilder`, refer to the `Client` documentation.
34#[must_use = "RequestBuilder does nothing until you 'send' it"]
35pub struct RequestBuilder {
36 client: Client,
37 request: crate::Result<Request>,
38}
39
40impl Request {
41 /// Constructs a new request.
42 #[inline]
43 pub fn new(method: Method, url: Url) -> Self {
44 Request {
45 method,
46 url,
47 headers: HeaderMap::new(),
48 body: None,
49 timeout: None,
50 version: Version::default(),
51 }
52 }
53
54 /// Get the method.
55 #[inline]
56 pub fn method(&self) -> &Method {
57 &self.method
58 }
59
60 /// Get a mutable reference to the method.
61 #[inline]
62 pub fn method_mut(&mut self) -> &mut Method {
63 &mut self.method
64 }
65
66 /// Get the url.
67 #[inline]
68 pub fn url(&self) -> &Url {
69 &self.url
70 }
71
72 /// Get a mutable reference to the url.
73 #[inline]
74 pub fn url_mut(&mut self) -> &mut Url {
75 &mut self.url
76 }
77
78 /// Get the headers.
79 #[inline]
80 pub fn headers(&self) -> &HeaderMap {
81 &self.headers
82 }
83
84 /// Get a mutable reference to the headers.
85 #[inline]
86 pub fn headers_mut(&mut self) -> &mut HeaderMap {
87 &mut self.headers
88 }
89
90 /// Get the body.
91 #[inline]
92 pub fn body(&self) -> Option<&Body> {
93 self.body.as_ref()
94 }
95
96 /// Get a mutable reference to the body.
97 #[inline]
98 pub fn body_mut(&mut self) -> &mut Option<Body> {
99 &mut self.body
100 }
101
102 /// Get the timeout.
103 #[inline]
104 pub fn timeout(&self) -> Option<&Duration> {
105 self.timeout.as_ref()
106 }
107
108 /// Get a mutable reference to the timeout.
109 #[inline]
110 pub fn timeout_mut(&mut self) -> &mut Option<Duration> {
111 &mut self.timeout
112 }
113
114 /// Get the http version.
115 #[inline]
116 pub fn version(&self) -> Version {
117 self.version
118 }
119
120 /// Get a mutable reference to the http version.
121 #[inline]
122 pub fn version_mut(&mut self) -> &mut Version {
123 &mut self.version
124 }
125
126 /// Attempt to clone the request.
127 ///
128 /// `None` is returned if the request can not be cloned, i.e. if the body is a stream.
129 pub fn try_clone(&self) -> Option<Request> {
130 let body = match self.body.as_ref() {
131 Some(body) => Some(body.try_clone()?),
132 None => None,
133 };
134 let mut req = Request::new(self.method().clone(), self.url().clone());
135 *req.timeout_mut() = self.timeout().cloned();
136 *req.headers_mut() = self.headers().clone();
137 *req.version_mut() = self.version();
138 req.body = body;
139 Some(req)
140 }
141
142 pub(super) fn pieces(
143 self,
144 ) -> (
145 Method,
146 Url,
147 HeaderMap,
148 Option<Body>,
149 Option<Duration>,
150 Version,
151 ) {
152 (
153 self.method,
154 self.url,
155 self.headers,
156 self.body,
157 self.timeout,
158 self.version,
159 )
160 }
161}
162
163impl RequestBuilder {
164 pub(super) fn new(client: Client, request: crate::Result<Request>) -> RequestBuilder {
165 let mut builder = RequestBuilder { client, request };
166
167 let auth = builder
168 .request
169 .as_mut()
170 .ok()
171 .and_then(|req| extract_authority(&mut req.url));
172
173 if let Some((username, password)) = auth {
174 builder.basic_auth(username, password)
175 } else {
176 builder
177 }
178 }
179
180 /// Assemble a builder starting from an existing `Client` and a `Request`.
181 pub fn from_parts(client: Client, request: Request) -> RequestBuilder {
182 RequestBuilder {
183 client,
184 request: crate::Result::Ok(request),
185 }
186 }
187
188 /// Add a `Header` to this Request.
189 pub fn header<K, V>(self, key: K, value: V) -> RequestBuilder
190 where
191 HeaderName: TryFrom<K>,
192 <HeaderName as TryFrom<K>>::Error: Into<http::Error>,
193 HeaderValue: TryFrom<V>,
194 <HeaderValue as TryFrom<V>>::Error: Into<http::Error>,
195 {
196 self.header_sensitive(key, value, false)
197 }
198
199 /// Add a `Header` to this Request with ability to define if header_value is sensitive.
200 fn header_sensitive<K, V>(mut self, key: K, value: V, sensitive: bool) -> RequestBuilder
201 where
202 HeaderName: TryFrom<K>,
203 <HeaderName as TryFrom<K>>::Error: Into<http::Error>,
204 HeaderValue: TryFrom<V>,
205 <HeaderValue as TryFrom<V>>::Error: Into<http::Error>,
206 {
207 let mut error = None;
208 if let Ok(ref mut req) = self.request {
209 match <HeaderName as TryFrom<K>>::try_from(key) {
210 Ok(key) => match <HeaderValue as TryFrom<V>>::try_from(value) {
211 Ok(mut value) => {
212 // We want to potentially make an unsensitive header
213 // to be sensitive, not the reverse. So, don't turn off
214 // a previously sensitive header.
215 if sensitive {
216 value.set_sensitive(true);
217 }
218 req.headers_mut().append(key, value);
219 }
220 Err(e) => error = Some(crate::error::builder(e.into())),
221 },
222 Err(e) => error = Some(crate::error::builder(e.into())),
223 };
224 }
225 if let Some(err) = error {
226 self.request = Err(err);
227 }
228 self
229 }
230
231 /// Add a set of Headers to the existing ones on this Request.
232 ///
233 /// The headers will be merged in to any already set.
234 pub fn headers(mut self, headers: crate::header::HeaderMap) -> RequestBuilder {
235 if let Ok(ref mut req) = self.request {
236 crate::util::replace_headers(req.headers_mut(), headers);
237 }
238 self
239 }
240
241 /// Enable HTTP basic authentication.
242 ///
243 /// ```rust
244 /// # use reqwest::Error;
245 ///
246 /// # async fn run() -> Result<(), Error> {
247 /// let client = reqwest::Client::new();
248 /// let resp = client.delete("http://httpbin.org/delete")
249 /// .basic_auth("admin", Some("good password"))
250 /// .send()
251 /// .await?;
252 /// # Ok(())
253 /// # }
254 /// ```
255 pub fn basic_auth<U, P>(self, username: U, password: Option<P>) -> RequestBuilder
256 where
257 U: fmt::Display,
258 P: fmt::Display,
259 {
260 let header_value = crate::util::basic_auth(username, password);
261 self.header_sensitive(crate::header::AUTHORIZATION, header_value, true)
262 }
263
264 /// Enable HTTP bearer authentication.
265 pub fn bearer_auth<T>(self, token: T) -> RequestBuilder
266 where
267 T: fmt::Display,
268 {
269 let header_value = format!("Bearer {}", token);
270 self.header_sensitive(crate::header::AUTHORIZATION, header_value, true)
271 }
272
273 /// Set the request body.
274 pub fn body<T: Into<Body>>(mut self, body: T) -> RequestBuilder {
275 if let Ok(ref mut req) = self.request {
276 *req.body_mut() = Some(body.into());
277 }
278 self
279 }
280
281 /// Enables a request timeout.
282 ///
283 /// The timeout is applied from when the request starts connecting until the
284 /// response body has finished. It affects only this request and overrides
285 /// the timeout configured using `ClientBuilder::timeout()`.
286 pub fn timeout(mut self, timeout: Duration) -> RequestBuilder {
287 if let Ok(ref mut req) = self.request {
288 *req.timeout_mut() = Some(timeout);
289 }
290 self
291 }
292
293 /// Sends a multipart/form-data body.
294 ///
295 /// ```
296 /// # use reqwest::Error;
297 ///
298 /// # async fn run() -> Result<(), Error> {
299 /// let client = reqwest::Client::new();
300 /// let form = reqwest::multipart::Form::new()
301 /// .text("key3", "value3")
302 /// .text("key4", "value4");
303 ///
304 ///
305 /// let response = client.post("your url")
306 /// .multipart(form)
307 /// .send()
308 /// .await?;
309 /// # Ok(())
310 /// # }
311 /// ```
312 #[cfg(feature = "multipart")]
313 #[cfg_attr(docsrs, doc(cfg(feature = "multipart")))]
314 pub fn multipart(self, mut multipart: multipart::Form) -> RequestBuilder {
315 let mut builder = self.header(
316 CONTENT_TYPE,
317 format!("multipart/form-data; boundary={}", multipart.boundary()).as_str(),
318 );
319
320 builder = match multipart.compute_length() {
321 Some(length) => builder.header(CONTENT_LENGTH, length),
322 None => builder,
323 };
324
325 if let Ok(ref mut req) = builder.request {
326 *req.body_mut() = Some(multipart.stream())
327 }
328 builder
329 }
330
331 /// Modify the query string of the URL.
332 ///
333 /// Modifies the URL of this request, adding the parameters provided.
334 /// This method appends and does not overwrite. This means that it can
335 /// be called multiple times and that existing query parameters are not
336 /// overwritten if the same key is used. The key will simply show up
337 /// twice in the query string.
338 /// Calling `.query(&[("foo", "a"), ("foo", "b")])` gives `"foo=a&foo=b"`.
339 ///
340 /// # Note
341 /// This method does not support serializing a single key-value
342 /// pair. Instead of using `.query(("key", "val"))`, use a sequence, such
343 /// as `.query(&[("key", "val")])`. It's also possible to serialize structs
344 /// and maps into a key-value pair.
345 ///
346 /// # Errors
347 /// This method will fail if the object you provide cannot be serialized
348 /// into a query string.
349 pub fn query<T: Serialize + ?Sized>(mut self, query: &T) -> RequestBuilder {
350 let mut error = None;
351 if let Ok(ref mut req) = self.request {
352 let url = req.url_mut();
353 let mut pairs = url.query_pairs_mut();
354 let serializer = serde_urlencoded::Serializer::new(&mut pairs);
355
356 if let Err(err) = query.serialize(serializer) {
357 error = Some(crate::error::builder(err));
358 }
359 }
360 if let Ok(ref mut req) = self.request {
361 if let Some("") = req.url().query() {
362 req.url_mut().set_query(None);
363 }
364 }
365 if let Some(err) = error {
366 self.request = Err(err);
367 }
368 self
369 }
370
371 /// Set HTTP version
372 pub fn version(mut self, version: Version) -> RequestBuilder {
373 if let Ok(ref mut req) = self.request {
374 req.version = version;
375 }
376 self
377 }
378
379 /// Send a form body.
380 ///
381 /// Sets the body to the url encoded serialization of the passed value,
382 /// and also sets the `Content-Type: application/x-www-form-urlencoded`
383 /// header.
384 ///
385 /// ```rust
386 /// # use reqwest::Error;
387 /// # use std::collections::HashMap;
388 /// #
389 /// # async fn run() -> Result<(), Error> {
390 /// let mut params = HashMap::new();
391 /// params.insert("lang", "rust");
392 ///
393 /// let client = reqwest::Client::new();
394 /// let res = client.post("http://httpbin.org")
395 /// .form(&params)
396 /// .send()
397 /// .await?;
398 /// # Ok(())
399 /// # }
400 /// ```
401 ///
402 /// # Errors
403 ///
404 /// This method fails if the passed value cannot be serialized into
405 /// url encoded format
406 pub fn form<T: Serialize + ?Sized>(mut self, form: &T) -> RequestBuilder {
407 let mut error = None;
408 if let Ok(ref mut req) = self.request {
409 match serde_urlencoded::to_string(form) {
410 Ok(body) => {
411 req.headers_mut().insert(
412 CONTENT_TYPE,
413 HeaderValue::from_static("application/x-www-form-urlencoded"),
414 );
415 *req.body_mut() = Some(body.into());
416 }
417 Err(err) => error = Some(crate::error::builder(err)),
418 }
419 }
420 if let Some(err) = error {
421 self.request = Err(err);
422 }
423 self
424 }
425
426 /// Send a JSON body.
427 ///
428 /// # Optional
429 ///
430 /// This requires the optional `json` feature enabled.
431 ///
432 /// # Errors
433 ///
434 /// Serialization can fail if `T`'s implementation of `Serialize` decides to
435 /// fail, or if `T` contains a map with non-string keys.
436 #[cfg(feature = "json")]
437 #[cfg_attr(docsrs, doc(cfg(feature = "json")))]
438 pub fn json<T: Serialize + ?Sized>(mut self, json: &T) -> RequestBuilder {
439 let mut error = None;
440 if let Ok(ref mut req) = self.request {
441 match serde_json::to_vec(json) {
442 Ok(body) => {
443 if !req.headers().contains_key(CONTENT_TYPE) {
444 req.headers_mut()
445 .insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
446 }
447 *req.body_mut() = Some(body.into());
448 }
449 Err(err) => error = Some(crate::error::builder(err)),
450 }
451 }
452 if let Some(err) = error {
453 self.request = Err(err);
454 }
455 self
456 }
457
458 /// Disable CORS on fetching the request.
459 ///
460 /// # WASM
461 ///
462 /// This option is only effective with WebAssembly target.
463 ///
464 /// The [request mode][mdn] will be set to 'no-cors'.
465 ///
466 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/mode
467 pub fn fetch_mode_no_cors(self) -> RequestBuilder {
468 self
469 }
470
471 /// Build a `Request`, which can be inspected, modified and executed with
472 /// `Client::execute()`.
473 pub fn build(self) -> crate::Result<Request> {
474 self.request
475 }
476
477 /// Build a `Request`, which can be inspected, modified and executed with
478 /// `Client::execute()`.
479 ///
480 /// This is similar to [`RequestBuilder::build()`], but also returns the
481 /// embedded `Client`.
482 pub fn build_split(self) -> (Client, crate::Result<Request>) {
483 (self.client, self.request)
484 }
485
486 /// Constructs the Request and sends it to the target URL, returning a
487 /// future Response.
488 ///
489 /// # Errors
490 ///
491 /// This method fails if there was an error while sending request,
492 /// redirect loop was detected or redirect limit was exhausted.
493 ///
494 /// # Example
495 ///
496 /// ```no_run
497 /// # use reqwest::Error;
498 /// #
499 /// # async fn run() -> Result<(), Error> {
500 /// let response = reqwest::Client::new()
501 /// .get("https://hyper.rs")
502 /// .send()
503 /// .await?;
504 /// # Ok(())
505 /// # }
506 /// ```
507 pub fn send(self) -> impl Future<Output = Result<Response, crate::Error>> {
508 match self.request {
509 Ok(req) => self.client.execute_request(req),
510 Err(err) => Pending::new_err(err),
511 }
512 }
513
514 /// Attempt to clone the RequestBuilder.
515 ///
516 /// `None` is returned if the RequestBuilder can not be cloned,
517 /// i.e. if the request body is a stream.
518 ///
519 /// # Examples
520 ///
521 /// ```
522 /// # use reqwest::Error;
523 /// #
524 /// # fn run() -> Result<(), Error> {
525 /// let client = reqwest::Client::new();
526 /// let builder = client.post("http://httpbin.org/post")
527 /// .body("from a &str!");
528 /// let clone = builder.try_clone();
529 /// assert!(clone.is_some());
530 /// # Ok(())
531 /// # }
532 /// ```
533 pub fn try_clone(&self) -> Option<RequestBuilder> {
534 self.request
535 .as_ref()
536 .ok()
537 .and_then(|req| req.try_clone())
538 .map(|req| RequestBuilder {
539 client: self.client.clone(),
540 request: Ok(req),
541 })
542 }
543}
544
545impl fmt::Debug for Request {
546 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
547 fmt_request_fields(&mut f.debug_struct("Request"), self).finish()
548 }
549}
550
551impl fmt::Debug for RequestBuilder {
552 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
553 let mut builder: DebugStruct<'_, '_> = f.debug_struct(name:"RequestBuilder");
554 match self.request {
555 Ok(ref req: &Request) => fmt_request_fields(&mut builder, req).finish(),
556 Err(ref err: &Error) => builder.field(name:"error", value:err).finish(),
557 }
558 }
559}
560
561fn fmt_request_fields<'a, 'b>(
562 f: &'a mut fmt::DebugStruct<'a, 'b>,
563 req: &Request,
564) -> &'a mut fmt::DebugStruct<'a, 'b> {
565 f.field("method", &req.method)
566 .field("url", &req.url)
567 .field(name:"headers", &req.headers)
568}
569
570/// Check the request URL for a "username:password" type authority, and if
571/// found, remove it from the URL and return it.
572pub(crate) fn extract_authority(url: &mut Url) -> Option<(String, Option<String>)> {
573 use percent_encoding::percent_decode;
574
575 if url.has_authority() {
576 let username: String = percent_decode(url.username().as_bytes())
577 .decode_utf8()
578 .ok()?
579 .into();
580 let password = url.password().and_then(|pass| {
581 percent_decode(pass.as_bytes())
582 .decode_utf8()
583 .ok()
584 .map(String::from)
585 });
586 if !username.is_empty() || password.is_some() {
587 url.set_username("")
588 .expect("has_authority means set_username shouldn't fail");
589 url.set_password(None)
590 .expect("has_authority means set_password shouldn't fail");
591 return Some((username, password));
592 }
593 }
594
595 None
596}
597
598impl<T> TryFrom<HttpRequest<T>> for Request
599where
600 T: Into<Body>,
601{
602 type Error = crate::Error;
603
604 fn try_from(req: HttpRequest<T>) -> crate::Result<Self> {
605 let (parts: Parts, body: T) = req.into_parts();
606 let Parts {
607 method: Method,
608 uri: Uri,
609 headers: HeaderMap,
610 version: Version,
611 ..
612 } = parts;
613 let url: Url = Url::parse(&uri.to_string()).map_err(op:crate::error::builder)?;
614 Ok(Request {
615 method,
616 url,
617 headers,
618 body: Some(body.into()),
619 timeout: None,
620 version,
621 })
622 }
623}
624
625impl TryFrom<Request> for HttpRequest<Body> {
626 type Error = crate::Error;
627
628 fn try_from(req: Request) -> crate::Result<Self> {
629 let Request {
630 method: Method,
631 url: Url,
632 headers: HeaderMap,
633 body: Option,
634 version: Version,
635 ..
636 } = req;
637
638 let mut req: Request = HttpRequest::builder()
639 .version(version)
640 .method(method)
641 .uri(url.as_str())
642 .body(body.unwrap_or_else(Body::empty))
643 .map_err(op:crate::error::builder)?;
644
645 *req.headers_mut() = headers;
646 Ok(req)
647 }
648}
649
650#[cfg(test)]
651mod tests {
652 use super::{Client, HttpRequest, Request, RequestBuilder, Version};
653 use crate::Method;
654 use serde::Serialize;
655 use std::collections::BTreeMap;
656 use std::convert::TryFrom;
657
658 #[test]
659 fn add_query_append() {
660 let client = Client::new();
661 let some_url = "https://google.com/";
662 let r = client.get(some_url);
663
664 let r = r.query(&[("foo", "bar")]);
665 let r = r.query(&[("qux", 3)]);
666
667 let req = r.build().expect("request is valid");
668 assert_eq!(req.url().query(), Some("foo=bar&qux=3"));
669 }
670
671 #[test]
672 fn add_query_append_same() {
673 let client = Client::new();
674 let some_url = "https://google.com/";
675 let r = client.get(some_url);
676
677 let r = r.query(&[("foo", "a"), ("foo", "b")]);
678
679 let req = r.build().expect("request is valid");
680 assert_eq!(req.url().query(), Some("foo=a&foo=b"));
681 }
682
683 #[test]
684 fn add_query_struct() {
685 #[derive(Serialize)]
686 struct Params {
687 foo: String,
688 qux: i32,
689 }
690
691 let client = Client::new();
692 let some_url = "https://google.com/";
693 let r = client.get(some_url);
694
695 let params = Params {
696 foo: "bar".into(),
697 qux: 3,
698 };
699
700 let r = r.query(&params);
701
702 let req = r.build().expect("request is valid");
703 assert_eq!(req.url().query(), Some("foo=bar&qux=3"));
704 }
705
706 #[test]
707 fn add_query_map() {
708 let mut params = BTreeMap::new();
709 params.insert("foo", "bar");
710 params.insert("qux", "three");
711
712 let client = Client::new();
713 let some_url = "https://google.com/";
714 let r = client.get(some_url);
715
716 let r = r.query(&params);
717
718 let req = r.build().expect("request is valid");
719 assert_eq!(req.url().query(), Some("foo=bar&qux=three"));
720 }
721
722 #[test]
723 fn test_replace_headers() {
724 use http::HeaderMap;
725
726 let mut headers = HeaderMap::new();
727 headers.insert("foo", "bar".parse().unwrap());
728 headers.append("foo", "baz".parse().unwrap());
729
730 let client = Client::new();
731 let req = client
732 .get("https://hyper.rs")
733 .header("im-a", "keeper")
734 .header("foo", "pop me")
735 .headers(headers)
736 .build()
737 .expect("request build");
738
739 assert_eq!(req.headers()["im-a"], "keeper");
740
741 let foo = req.headers().get_all("foo").iter().collect::<Vec<_>>();
742 assert_eq!(foo.len(), 2);
743 assert_eq!(foo[0], "bar");
744 assert_eq!(foo[1], "baz");
745 }
746
747 #[test]
748 fn normalize_empty_query() {
749 let client = Client::new();
750 let some_url = "https://google.com/";
751 let empty_query: &[(&str, &str)] = &[];
752
753 let req = client
754 .get(some_url)
755 .query(empty_query)
756 .build()
757 .expect("request build");
758
759 assert_eq!(req.url().query(), None);
760 assert_eq!(req.url().as_str(), "https://google.com/");
761 }
762
763 #[test]
764 fn try_clone_reusable() {
765 let client = Client::new();
766 let builder = client
767 .post("http://httpbin.org/post")
768 .header("foo", "bar")
769 .body("from a &str!");
770 let req = builder
771 .try_clone()
772 .expect("clone successful")
773 .build()
774 .expect("request is valid");
775 assert_eq!(req.url().as_str(), "http://httpbin.org/post");
776 assert_eq!(req.method(), Method::POST);
777 assert_eq!(req.headers()["foo"], "bar");
778 }
779
780 #[test]
781 fn try_clone_no_body() {
782 let client = Client::new();
783 let builder = client.get("http://httpbin.org/get");
784 let req = builder
785 .try_clone()
786 .expect("clone successful")
787 .build()
788 .expect("request is valid");
789 assert_eq!(req.url().as_str(), "http://httpbin.org/get");
790 assert_eq!(req.method(), Method::GET);
791 assert!(req.body().is_none());
792 }
793
794 #[test]
795 #[cfg(feature = "stream")]
796 fn try_clone_stream() {
797 let chunks: Vec<Result<_, ::std::io::Error>> = vec![Ok("hello"), Ok(" "), Ok("world")];
798 let stream = futures_util::stream::iter(chunks);
799 let client = Client::new();
800 let builder = client
801 .get("http://httpbin.org/get")
802 .body(super::Body::wrap_stream(stream));
803 let clone = builder.try_clone();
804 assert!(clone.is_none());
805 }
806
807 #[test]
808 fn convert_url_authority_into_basic_auth() {
809 let client = Client::new();
810 let some_url = "https://Aladdin:open sesame@localhost/";
811
812 let req = client.get(some_url).build().expect("request build");
813
814 assert_eq!(req.url().as_str(), "https://localhost/");
815 assert_eq!(
816 req.headers()["authorization"],
817 "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
818 );
819 }
820
821 #[test]
822 fn test_basic_auth_sensitive_header() {
823 let client = Client::new();
824 let some_url = "https://localhost/";
825
826 let req = client
827 .get(some_url)
828 .basic_auth("Aladdin", Some("open sesame"))
829 .build()
830 .expect("request build");
831
832 assert_eq!(req.url().as_str(), "https://localhost/");
833 assert_eq!(
834 req.headers()["authorization"],
835 "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
836 );
837 assert!(req.headers()["authorization"].is_sensitive());
838 }
839
840 #[test]
841 fn test_bearer_auth_sensitive_header() {
842 let client = Client::new();
843 let some_url = "https://localhost/";
844
845 let req = client
846 .get(some_url)
847 .bearer_auth("Hold my bear")
848 .build()
849 .expect("request build");
850
851 assert_eq!(req.url().as_str(), "https://localhost/");
852 assert_eq!(req.headers()["authorization"], "Bearer Hold my bear");
853 assert!(req.headers()["authorization"].is_sensitive());
854 }
855
856 #[test]
857 fn test_explicit_sensitive_header() {
858 let client = Client::new();
859 let some_url = "https://localhost/";
860
861 let mut header = http::HeaderValue::from_static("in plain sight");
862 header.set_sensitive(true);
863
864 let req = client
865 .get(some_url)
866 .header("hiding", header)
867 .build()
868 .expect("request build");
869
870 assert_eq!(req.url().as_str(), "https://localhost/");
871 assert_eq!(req.headers()["hiding"], "in plain sight");
872 assert!(req.headers()["hiding"].is_sensitive());
873 }
874
875 #[test]
876 fn convert_from_http_request() {
877 let http_request = HttpRequest::builder()
878 .method("GET")
879 .uri("http://localhost/")
880 .header("User-Agent", "my-awesome-agent/1.0")
881 .body("test test test")
882 .unwrap();
883 let req: Request = Request::try_from(http_request).unwrap();
884 assert!(req.body().is_some());
885 let test_data = b"test test test";
886 assert_eq!(req.body().unwrap().as_bytes(), Some(&test_data[..]));
887 let headers = req.headers();
888 assert_eq!(headers.get("User-Agent").unwrap(), "my-awesome-agent/1.0");
889 assert_eq!(req.method(), Method::GET);
890 assert_eq!(req.url().as_str(), "http://localhost/");
891 }
892
893 #[test]
894 fn set_http_request_version() {
895 let http_request = HttpRequest::builder()
896 .method("GET")
897 .uri("http://localhost/")
898 .header("User-Agent", "my-awesome-agent/1.0")
899 .version(Version::HTTP_11)
900 .body("test test test")
901 .unwrap();
902 let req: Request = Request::try_from(http_request).unwrap();
903 assert!(req.body().is_some());
904 let test_data = b"test test test";
905 assert_eq!(req.body().unwrap().as_bytes(), Some(&test_data[..]));
906 let headers = req.headers();
907 assert_eq!(headers.get("User-Agent").unwrap(), "my-awesome-agent/1.0");
908 assert_eq!(req.method(), Method::GET);
909 assert_eq!(req.url().as_str(), "http://localhost/");
910 assert_eq!(req.version(), Version::HTTP_11);
911 }
912
913 #[test]
914 fn builder_split_reassemble() {
915 let builder = {
916 let client = Client::new();
917 client.get("http://example.com")
918 };
919 let (client, inner) = builder.build_split();
920 let request = inner.unwrap();
921 let builder = RequestBuilder::from_parts(client, request);
922 builder.build().unwrap();
923 }
924
925 /*
926 use {body, Method};
927 use super::Client;
928 use header::{Host, Headers, ContentType};
929 use std::collections::HashMap;
930 use serde_urlencoded;
931 use serde_json;
932
933 #[test]
934 fn basic_get_request() {
935 let client = Client::new().unwrap();
936 let some_url = "https://google.com/";
937 let r = client.get(some_url).unwrap().build();
938
939 assert_eq!(r.method, Method::Get);
940 assert_eq!(r.url.as_str(), some_url);
941 }
942
943 #[test]
944 fn basic_head_request() {
945 let client = Client::new().unwrap();
946 let some_url = "https://google.com/";
947 let r = client.head(some_url).unwrap().build();
948
949 assert_eq!(r.method, Method::Head);
950 assert_eq!(r.url.as_str(), some_url);
951 }
952
953 #[test]
954 fn basic_post_request() {
955 let client = Client::new().unwrap();
956 let some_url = "https://google.com/";
957 let r = client.post(some_url).unwrap().build();
958
959 assert_eq!(r.method, Method::Post);
960 assert_eq!(r.url.as_str(), some_url);
961 }
962
963 #[test]
964 fn basic_put_request() {
965 let client = Client::new().unwrap();
966 let some_url = "https://google.com/";
967 let r = client.put(some_url).unwrap().build();
968
969 assert_eq!(r.method, Method::Put);
970 assert_eq!(r.url.as_str(), some_url);
971 }
972
973 #[test]
974 fn basic_patch_request() {
975 let client = Client::new().unwrap();
976 let some_url = "https://google.com/";
977 let r = client.patch(some_url).unwrap().build();
978
979 assert_eq!(r.method, Method::Patch);
980 assert_eq!(r.url.as_str(), some_url);
981 }
982
983 #[test]
984 fn basic_delete_request() {
985 let client = Client::new().unwrap();
986 let some_url = "https://google.com/";
987 let r = client.delete(some_url).unwrap().build();
988
989 assert_eq!(r.method, Method::Delete);
990 assert_eq!(r.url.as_str(), some_url);
991 }
992
993 #[test]
994 fn add_header() {
995 let client = Client::new().unwrap();
996 let some_url = "https://google.com/";
997 let mut r = client.post(some_url).unwrap();
998
999 let header = Host {
1000 hostname: "google.com".to_string(),
1001 port: None,
1002 };
1003
1004 // Add a copy of the header to the request builder
1005 let r = r.header(header.clone()).build();
1006
1007 // then check it was actually added
1008 assert_eq!(r.headers.get::<Host>(), Some(&header));
1009 }
1010
1011 #[test]
1012 fn add_headers() {
1013 let client = Client::new().unwrap();
1014 let some_url = "https://google.com/";
1015 let mut r = client.post(some_url).unwrap();
1016
1017 let header = Host {
1018 hostname: "google.com".to_string(),
1019 port: None,
1020 };
1021
1022 let mut headers = Headers::new();
1023 headers.set(header);
1024
1025 // Add a copy of the headers to the request builder
1026 let r = r.headers(headers.clone()).build();
1027
1028 // then make sure they were added correctly
1029 assert_eq!(r.headers, headers);
1030 }
1031
1032 #[test]
1033 fn add_headers_multi() {
1034 let client = Client::new().unwrap();
1035 let some_url = "https://google.com/";
1036 let mut r = client.post(some_url).unwrap();
1037
1038 let header = Host {
1039 hostname: "google.com".to_string(),
1040 port: None,
1041 };
1042
1043 let mut headers = Headers::new();
1044 headers.set(header);
1045
1046 // Add a copy of the headers to the request builder
1047 let r = r.headers(headers.clone()).build();
1048
1049 // then make sure they were added correctly
1050 assert_eq!(r.headers, headers);
1051 }
1052
1053 #[test]
1054 fn add_body() {
1055 let client = Client::new().unwrap();
1056 let some_url = "https://google.com/";
1057 let mut r = client.post(some_url).unwrap();
1058
1059 let body = "Some interesting content";
1060
1061 let r = r.body(body).build();
1062
1063 let buf = body::read_to_string(r.body.unwrap()).unwrap();
1064
1065 assert_eq!(buf, body);
1066 }
1067
1068 #[test]
1069 fn add_form() {
1070 let client = Client::new().unwrap();
1071 let some_url = "https://google.com/";
1072 let mut r = client.post(some_url).unwrap();
1073
1074 let mut form_data = HashMap::new();
1075 form_data.insert("foo", "bar");
1076
1077 let r = r.form(&form_data).unwrap().build();
1078
1079 // Make sure the content type was set
1080 assert_eq!(r.headers.get::<ContentType>(),
1081 Some(&ContentType::form_url_encoded()));
1082
1083 let buf = body::read_to_string(r.body.unwrap()).unwrap();
1084
1085 let body_should_be = serde_urlencoded::to_string(&form_data).unwrap();
1086 assert_eq!(buf, body_should_be);
1087 }
1088
1089 #[test]
1090 fn add_json() {
1091 let client = Client::new().unwrap();
1092 let some_url = "https://google.com/";
1093 let mut r = client.post(some_url).unwrap();
1094
1095 let mut json_data = HashMap::new();
1096 json_data.insert("foo", "bar");
1097
1098 let r = r.json(&json_data).unwrap().build();
1099
1100 // Make sure the content type was set
1101 assert_eq!(r.headers.get::<ContentType>(), Some(&ContentType::json()));
1102
1103 let buf = body::read_to_string(r.body.unwrap()).unwrap();
1104
1105 let body_should_be = serde_json::to_string(&json_data).unwrap();
1106 assert_eq!(buf, body_should_be);
1107 }
1108
1109 #[test]
1110 fn add_json_fail() {
1111 use serde::{Serialize, Serializer};
1112 use serde::ser::Error;
1113 struct MyStruct;
1114 impl Serialize for MyStruct {
1115 fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
1116 where S: Serializer
1117 {
1118 Err(S::Error::custom("nope"))
1119 }
1120 }
1121
1122 let client = Client::new().unwrap();
1123 let some_url = "https://google.com/";
1124 let mut r = client.post(some_url).unwrap();
1125 let json_data = MyStruct{};
1126 assert!(r.json(&json_data).unwrap_err().is_serialization());
1127 }
1128 */
1129}
1130