feat(net): add user_agent helper to MockNet
This commit is contained in:
parent
41973abe18
commit
3b8b260b65
2 changed files with 49 additions and 0 deletions
|
@ -434,6 +434,12 @@ impl<'net> ReqBuilder<'net> {
|
|||
self
|
||||
}
|
||||
|
||||
/// Sets the User-Agent header to be used by this client.
|
||||
#[must_use]
|
||||
pub fn user_agent(self, user_agent: impl Into<String>) -> Self {
|
||||
self.header(http::header::USER_AGENT.to_string(), user_agent)
|
||||
}
|
||||
|
||||
/// Add query parameter
|
||||
#[must_use]
|
||||
pub fn query(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
|
||||
|
@ -756,6 +762,12 @@ impl<'net> WhenRequest<'net, WhenBuildRequest> {
|
|||
self
|
||||
}
|
||||
|
||||
/// Specifies user agent the mock will match against.
|
||||
#[must_use]
|
||||
pub fn user_agent(self, agent: impl Into<String>) -> Self {
|
||||
self.header(http::header::USER_AGENT.to_string(), agent)
|
||||
}
|
||||
|
||||
/// Specifies the body that the mock will match against.
|
||||
///
|
||||
/// Any request that does not have this body will not match the mock.
|
||||
|
|
37
tests/net.rs
37
tests/net.rs
|
@ -531,3 +531,40 @@ async fn test_get_with_duplicate_query_keys() {
|
|||
"key:value-3,value-4"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_with_user_agent() {
|
||||
//given
|
||||
let mock_net = kxio::net::mock();
|
||||
let url = "https://www.example.com/path";
|
||||
|
||||
mock_net
|
||||
.on()
|
||||
.get(url)
|
||||
.user_agent("007")
|
||||
.respond(StatusCode::OK)
|
||||
.mock()
|
||||
.expect("mock");
|
||||
mock_net
|
||||
.on()
|
||||
.get(url)
|
||||
.user_agent("orange")
|
||||
.respond(StatusCode::FORBIDDEN)
|
||||
.mock()
|
||||
.expect("mock");
|
||||
let net = Net::from(mock_net);
|
||||
|
||||
//when
|
||||
let invalid = net.get(url).user_agent("orange").send().await;
|
||||
let valid = net
|
||||
.get(url)
|
||||
.user_agent("007")
|
||||
.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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue