From 2fbc20167d6656850069c6496c73969c78b0a8d2 Mon Sep 17 00:00:00 2001 From: knight42 Date: Sun, 22 Oct 2017 16:39:43 +0800 Subject: [PATCH 1/3] feat: add Response::text() --- src/response.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/response.rs b/src/response.rs index a8f81e3..d84de5a 100644 --- a/src/response.rs +++ b/src/response.rs @@ -165,6 +165,26 @@ impl Response { serde_json::from_reader(self).map_err(::error::from) } + /// Get the response text. + /// + /// # Example + /// + /// ```rust + /// # extern crate reqwest; + /// # fn run() -> Result<(), Box<::std::error::Error>> { + /// let content = reqwest::get("http://httpbin.org/range/26")?.text()?; + /// # Ok(()) + /// # } + /// ``` + pub fn text(&mut self) -> io::Result { + let len = self.headers().get::<::header::ContentLength>() + .map(|ct_len| **ct_len) + .unwrap_or(0); + let mut content = String::with_capacity(len as usize); + self.read_to_string(&mut content)?; + Ok(content) + } + /// Copy the response body into a writer. /// /// This function internally uses [`std::io::copy`] and hence will continuously read data from From 33c7ce4ce2f65587ea60c011151a5605887e97f3 Mon Sep 17 00:00:00 2001 From: knight42 Date: Sun, 22 Oct 2017 16:40:27 +0800 Subject: [PATCH 2/3] test: Response::text() --- tests/client.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/client.rs b/tests/client.rs index a302a2a..65731e9 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -5,6 +5,39 @@ mod support; use std::io::Read; +#[test] +fn test_response_text() { + let server = server! { + request: b"\ + GET /text HTTP/1.1\r\n\ + Host: $HOST\r\n\ + User-Agent: $USERAGENT\r\n\ + Accept: */*\r\n\ + Accept-Encoding: gzip\r\n\ + \r\n\ + ", + response: b"\ + HTTP/1.1 200 OK\r\n\ + Server: test\r\n\ + Content-Length: 5\r\n\ + \r\n\ + Hello\ + " + }; + + let url = format!("http://{}/text", server.addr()); + let mut res = reqwest::get(&url).unwrap(); + assert_eq!(res.url().as_str(), &url); + assert_eq!(res.status(), reqwest::StatusCode::Ok); + assert_eq!(res.headers().get(), + Some(&reqwest::header::Server::new("test".to_string()))); + assert_eq!(res.headers().get(), + Some(&reqwest::header::ContentLength(5))); + + let body = res.text().unwrap(); + assert_eq!(b"Hello", body.as_bytes()); +} + #[test] fn test_response_copy_to() { let server = server! { From 2c60511bcee3c633467b6be46f3d1e27af5f0905 Mon Sep 17 00:00:00 2001 From: knight42 Date: Fri, 27 Oct 2017 15:10:31 +0800 Subject: [PATCH 3/3] fix(response): `copy_to()` and `text()` return `reqwest::Result` --- src/response.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/response.rs b/src/response.rs index d84de5a..f2defe9 100644 --- a/src/response.rs +++ b/src/response.rs @@ -176,12 +176,12 @@ impl Response { /// # Ok(()) /// # } /// ``` - pub fn text(&mut self) -> io::Result { + pub fn text(&mut self) -> ::Result { let len = self.headers().get::<::header::ContentLength>() .map(|ct_len| **ct_len) .unwrap_or(0); let mut content = String::with_capacity(len as usize); - self.read_to_string(&mut content)?; + self.read_to_string(&mut content).map_err(::error::from)?; Ok(content) } @@ -205,10 +205,10 @@ impl Response { /// # } /// ``` #[inline] - pub fn copy_to(&mut self, w: &mut W) -> io::Result + pub fn copy_to(&mut self, w: &mut W) -> ::Result where W: io::Write { - io::copy(self, w) + io::copy(self, w).map_err(::error::from) } /// Turn a response into an error if the server returned an error.