feat(net): add bearer_auth helper to MockNet
This commit is contained in:
parent
17dc1dbe30
commit
4cb841f9f4
2 changed files with 58 additions and 0 deletions
|
@ -451,6 +451,18 @@ impl<'net> ReqBuilder<'net> {
|
||||||
self.header(http::header::AUTHORIZATION.to_string(), value)
|
self.header(http::header::AUTHORIZATION.to_string(), value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Enable HTTP bearer authentication.
|
||||||
|
#[must_use]
|
||||||
|
pub fn bearer_auth<T>(self, token: T) -> Self
|
||||||
|
where
|
||||||
|
T: std::fmt::Display,
|
||||||
|
{
|
||||||
|
self.header(
|
||||||
|
http::header::AUTHORIZATION.to_string(),
|
||||||
|
format!("Bearer {token}"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Add query parameter
|
/// Add query parameter
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn query(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
|
pub fn query(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
|
||||||
|
@ -808,6 +820,15 @@ impl<'net> WhenRequest<'net, WhenBuildRequest> {
|
||||||
self.header(http::header::AUTHORIZATION.to_string(), value)
|
self.header(http::header::AUTHORIZATION.to_string(), value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Specifies bearer authentication the mock will match against.
|
||||||
|
#[must_use]
|
||||||
|
pub fn bearer_auth(self, token: impl Into<String>) -> Self {
|
||||||
|
self.header(
|
||||||
|
http::header::AUTHORIZATION.to_string(),
|
||||||
|
format!("Bearer {}", token.into()),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Specifies user agent the mock will match against.
|
/// Specifies user agent the mock will match against.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn user_agent(self, agent: impl Into<String>) -> Self {
|
pub fn user_agent(self, agent: impl Into<String>) -> Self {
|
||||||
|
|
37
tests/net.rs
37
tests/net.rs
|
@ -569,6 +569,43 @@ async fn test_get_with_basic_auth() {
|
||||||
assert_eq!(valid.status(), StatusCode::OK);
|
assert_eq!(valid.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_get_with_bearer_auth() {
|
||||||
|
//given
|
||||||
|
let mock_net = kxio::net::mock();
|
||||||
|
let url = "https://www.example.com/path";
|
||||||
|
|
||||||
|
mock_net
|
||||||
|
.on()
|
||||||
|
.get(url)
|
||||||
|
.bearer_auth("token")
|
||||||
|
.respond(StatusCode::OK)
|
||||||
|
.mock()
|
||||||
|
.expect("mock");
|
||||||
|
mock_net
|
||||||
|
.on()
|
||||||
|
.get(url)
|
||||||
|
.bearer_auth("invalid")
|
||||||
|
.respond(StatusCode::FORBIDDEN)
|
||||||
|
.mock()
|
||||||
|
.expect("mock");
|
||||||
|
let net = Net::from(mock_net);
|
||||||
|
|
||||||
|
//when
|
||||||
|
let invalid = net.get(url).bearer_auth("invalid").send().await;
|
||||||
|
let valid = net
|
||||||
|
.get(url)
|
||||||
|
.bearer_auth("token")
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.expect("valid response");
|
||||||
|
|
||||||
|
//then
|
||||||
|
let_assert!(Err(Error::ResponseError { response }) = invalid);
|
||||||
|
assert_eq!(response.status(), StatusCode::FORBIDDEN);
|
||||||
|
assert_eq!(valid.status(), StatusCode::OK);
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_get_with_user_agent() {
|
async fn test_get_with_user_agent() {
|
||||||
//given
|
//given
|
||||||
|
|
Loading…
Reference in a new issue