Skip to content

Commit

Permalink
Allow disabling default features for HTTP clients
Browse files Browse the repository at this point in the history
By doing this, `pretend` users can specify
the features they want.
  • Loading branch information
SfietKonstantin committed Aug 13, 2021
1 parent 6d4be15 commit e4c9d2c
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 11 deletions.
7 changes: 5 additions & 2 deletions pretend-awc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ description = "awc based client for pretend."
authors = ["Lucien XU <[email protected]>"]
license = "MIT"
homepage = "https://github.com/SfietKonstantin/pretend"
documentation = "https://docs.rs/pretend-isahc/latest/pretend_awc/"
documentation = "https://docs.rs/pretend-awc/latest/pretend_awc/"
repository = "https://github.com/SfietKonstantin/pretend"
keywords = ["http", "client", "web", "async", "declarative"]
categories = ["web-programming::http-client"]
readme = "README.md"

[dependencies]
pretend = { path = "../pretend", version = "0.2.0", features = ["local-error"] }
awc = "2.0"
awc = { version = "2", default-features = false }

[features]
default = ["awc/default"]
5 changes: 4 additions & 1 deletion pretend-isahc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ readme = "README.md"

[dependencies]
pretend = { path = "../pretend", version = "0.2.0" }
isahc = "1.3"
isahc = { version = "1", default-features = false }

[features]
default = ["isahc/default"]
4 changes: 2 additions & 2 deletions pretend-reqwest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ readme = "README.md"

[dependencies]
pretend = { path = "../pretend", version = "0.2.0" }
reqwest = { version = "0.11" }
reqwest = { version = "0.11", default-features = false }

[features]
default = []
default = ["reqwest/default"]
blocking = ["reqwest/blocking"]
9 changes: 6 additions & 3 deletions pretend-ureq/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ description = "ureq based client for pretend."
authors = ["Lucien XU <[email protected]>"]
license = "MIT"
homepage = "https://github.com/SfietKonstantin/pretend"
documentation = "https://docs.rs/pretend-reqwest/latest/pretend_ureq/"
documentation = "https://docs.rs/pretend-ureq/latest/pretend_ureq/"
repository = "https://github.com/SfietKonstantin/pretend"
keywords = ["http", "client", "web", "async", "declarative"]
keywords = ["http", "client", "web", "declarative"]
categories = ["web-programming::http-client"]
readme = "README.md"

[dependencies]
pretend = { path = "../pretend", version = "0.2.0" }
ureq = "2.1"
ureq = { version = "2", default-features = false }

[features]
default = ["ureq/default"]
28 changes: 28 additions & 0 deletions pretend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,34 @@
//! - [`awc`](https://crates.io/crates/pretend-awc) (local async)
//! - [`ureq`](https://crates.io/crates/pretend-ureq) (blocking)
//!
//! These client implementations depends on the latest major release of each HTTP client at
//! time of the release. The `default` feature for each of the HTTP client crate is also mapped
//! to the `pretend-*` crate. To enable HTTP client features, you should add it as a dependency
//! and enable them here. If needed, you can play with the `default-features` option on the
//! `pretend-*` crate.
//!
//! The following snippet will enable `reqwest` default features
//!
//! ```toml
//! [dependencies]
//! pretend-reqwest = "0.2.2"
//! ```
//!
//! In the following snippet, no feature of `reqwest` will be enabled
//!
//! ```toml
//! [dependencies]
//! pretend-reqwest = { version = "0.2.2", default-features = false }
//! ```
//!
//! To use `reqwest` with rustls instead of the native-tls, you can do the following:
//!
//! ```toml
//! [dependencies]
//! pretend-reqwest = { version = "0.2.2", default-features = false }
//! reqwest = { version = "*", default-features = false, features = ["rustls-tls"] }
//! ```
//!
//! # Implementing a `pretend` HTTP client
//!
//! `pretend` clients wraps HTTP clients from other crates. They allow [`Pretend`] to execute
Expand Down
8 changes: 5 additions & 3 deletions tests/default-features-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ edition = "2018"
pretend = { path = "../../pretend" }
pretend-codegen = { path = "../../pretend-codegen" }

pretend-isahc = { path = "../../pretend-isahc" }
pretend-reqwest = { path = "../../pretend-reqwest" }
pretend-ureq = { path = "../../pretend-ureq" }
pretend-isahc = { path = "../../pretend-isahc", default-features = false }
pretend-reqwest = { path = "../../pretend-reqwest", default-features = false }
pretend-ureq = { path = "../../pretend-ureq", default-features = false }

[dev-dependencies]
anyhow = "1.0"
thiserror = "1.0"
tokio = { version = "1.5", features = ["macros", "rt-multi-thread"] }
trybuild = "1.0"

[workspace]
3 changes: 3 additions & 0 deletions tests/default-features-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
//! to verify that they effectively supports these bounds. It also compiles a test
//! client with an error that does not support these bounds, to make sure that it
//! fails to compile.
//!
//! It also test if, by default, pretend-* crates are compiled without any feature
//! in the client crate.
42 changes: 42 additions & 0 deletions tests/default-features-build/tests/test_no_https.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use pretend::{pretend, request, Pretend, Result, Url};

#[pretend]
trait HttpBin {
#[request(method = "GET", path = "/get")]
async fn get(&self) -> Result<()>;
}

#[pretend]
trait HttpBinSync {
#[request(method = "GET", path = "/get")]
fn get(&self) -> Result<()>;
}

#[tokio::test]
async fn test_reqwest_no_https_feature() {
// With default-features = false
// reqwest will not use any TLS crate,
// and thus, cannot handle https calls

let url = Url::parse("https://httpbin.org").unwrap();
let pretend = Pretend::for_client(pretend_reqwest::Client::default()).with_url(url);
let result = pretend.get().await;

let error = anyhow::Error::from(result.unwrap_err());
assert!(format!("{:?}", error).contains("scheme is not http"));
}

#[test]
fn test_ureq_no_https_feature() {
// With default-features = false
// ureq will not use any TLS crate,
// and thus, cannot handle https calls

let url = Url::parse("https://httpbin.org").unwrap();
let pretend =
Pretend::for_client(pretend_ureq::Client::new(pretend_ureq::ureq::agent())).with_url(url);
let result = pretend.get();

let error = anyhow::Error::from(result.unwrap_err());
assert!(format!("{:?}", error).contains("ureq was build without HTTP support"));
}

0 comments on commit e4c9d2c

Please sign in to comment.