Skip to content

Commit

Permalink
Remove is_test() (for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
algesten committed Jul 8, 2024
1 parent 47370ff commit 8886d80
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 35 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ features = []
hoot = { git = "https://github.com/algesten/hoot", rev = "2e2eac5" }
http = "1.1.0"
log = "0.4.22"
once_cell = "1.19.0"
thiserror = "1.0.61"
22 changes: 7 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
// #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

use std::convert::TryFrom;
use std::sync::atomic::{AtomicBool, Ordering};

use body::AsBody;
/// Re-exported http-crate.
pub use http;

use http::{Method, Request, Response, Uri};
use once_cell::sync::Lazy;
use recv::RecvBody;
use request::RequestBuilder;

Expand Down Expand Up @@ -61,18 +59,12 @@ where
builder(Method::POST, uri)
}

// is_test returns false so long as it has only ever been called with false.
// If it has ever been called with true, it will always return true after that.
// This is a public but hidden function used to allow doctests to use the test_agent.
// Note that we use this approach for doctests rather the #[cfg(test)], because
// doctests are run against a copy of the crate build without cfg(test) set.
// We also can't use #[cfg(doctest)] to do this, because cfg(doctest) is only set
// when collecting doctests, not when building the crate.
#[doc(hidden)]
pub fn is_test(is: bool) -> bool {
static IS_TEST: Lazy<AtomicBool> = Lazy::new(|| AtomicBool::new(false));
if is {
IS_TEST.store(true, Ordering::SeqCst);
#[cfg(test)]
mod test {
use super::*;

#[test]
fn simple_get() {
get("https://httpbin.org/get").call().unwrap();
}
IS_TEST.load(Ordering::SeqCst)
}
15 changes: 3 additions & 12 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@ impl RequestBuilder {
/// # Examples
///
/// ```
/// # fn main() -> Result<(), ureq::Error> {
/// # ureq::is_test(true);
/// let res = ureq::get("https://host.test/my-path")
/// .header("Accept", "text/html")
/// .header("X-Custom-Foo", "bar")
/// .call()?;
/// # Ok(())
/// # }
/// # Ok::<_, ureq::Error>(())
/// ```
pub fn header<K, V>(mut self, key: K, value: V) -> RequestBuilder
where
Expand All @@ -56,12 +53,9 @@ impl RequestBuilder {
/// Content-Length nor Transfer-Encoding.
///
/// ```
/// # fn main() -> Result<(), ureq::Error> {
/// # ureq::is_test(true);
/// let resp = ureq::get("http://httpbin.org/get")
/// .call()?;
/// # Ok(())
/// # }
/// # Ok::<_, ureq::Error>(())
/// ```
pub fn call(self) -> Result<Response<RecvBody>, Error> {
let request = self.builder.body(()).unwrap();
Expand All @@ -73,12 +67,9 @@ impl RequestBuilder {
/// The `Content-Length` header is implicitly set to the length of the serialized value.
///
/// ```
/// # fn main() -> Result<(), ureq::Error> {
/// # ureq::is_test(true);
/// let resp = ureq::post("http://httpbin.org/put")
/// .send_bytes(&[0; 1000])?;
/// # Ok(())
/// # }
/// # Ok::<_, ureq::Error>(())
/// ```
pub fn send_bytes(self, data: &[u8]) -> Result<Response<RecvBody>, Error> {
let request = self.builder.body(()).unwrap();
Expand Down

0 comments on commit 8886d80

Please sign in to comment.