Switch to Result-based API. (#132)

Gets rid of synthetic_error, and makes the various send_* methods return `Result<Response, Error>`.
Introduces a new error type "HTTP", which represents an error due to status codes 4xx or 5xx.
The HTTP error type contains a boxed Response, so users can read the actual response if they want.
Adds an `error_for_status` setting to disable the functionality of treating 4xx and 5xx as errors.
Adds .unwrap() to a lot of tests.

Fixes #128.
This commit is contained in:
Jacob Hoffman-Andrews
2020-10-17 00:40:48 -07:00
committed by GitHub
parent 257d4e54dd
commit e36c1c2aa1
19 changed files with 222 additions and 344 deletions

View File

@@ -1,18 +1,3 @@
#[cfg(all(test, any(feature = "tls", feature = "native-tls")))]
use std::io::Read;
#[cfg(any(feature = "tls", feature = "native-tls"))]
#[test]
fn tls_connection_close() {
let agent = ureq::Agent::default().build();
let resp = agent
.get("https://example.com/404")
.set("Connection", "close")
.call();
assert_eq!(resp.status(), 404);
resp.into_reader().read_to_end(&mut vec![]).unwrap();
}
#[cfg(feature = "tls")]
#[cfg(feature = "cookies")]
#[cfg(feature = "json")]
@@ -35,7 +20,8 @@ fn agent_set_cookie() {
let resp = agent
.get("https://httpbin.org/get")
.set("Connection", "close")
.call();
.call()
.unwrap();
assert_eq!(resp.status(), 200);
assert_eq!(
"name=value",
@@ -133,7 +119,8 @@ fn tls_client_certificate() {
let resp = agent
.get("https://client.badssl.com/")
.set_tls_config(std::sync::Arc::new(tls_config))
.call();
.call()
.unwrap();
assert_eq!(resp.status(), 200);
}