From bd726c7d4c0b32af26d338e6c33f9fb8a7d80171 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Tue, 26 Feb 2019 16:21:50 +0100 Subject: [PATCH] feat: Add Response::is_success utility method --- src/lib.rs | 8 ++++++++ src/tests.rs | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 56d6327e4..9caedff31 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -303,6 +303,14 @@ impl <'a> Request<'a> { } } +impl Response { + /// Check whether the status code of this HTTP response is a + /// success (i.e. in the 200-299 range). + pub fn is_success(&self) -> bool { + self.status >= 200 && self.status < 300 + } +} + impl Response> { /// Attempt to parse the HTTP response body as a UTF-8 encoded /// string. diff --git a/src/tests.rs b/src/tests.rs index 8067e7cf6..cc44ca7b5 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -9,7 +9,7 @@ fn test_http_get() { let resp = Request::new(Method::Get, "https://httpbin.org/get") .send().expect("failed to send request"); - assert_eq!(200, resp.status, "response status should be 200 OK"); + assert!(resp.is_success(), "request should have succeeded"); } #[test]