Remove Agent::set_cookie
Preloading an agent with cookies can be done by providing a prepared cookie store using `AgentBuilder`. At this point, we don't want direct state mutation on the `Agent`, so this fn goes away. Context: https://github.com/algesten/ureq/issues/203#issuecomment-716385310
This commit is contained in:
19
src/agent.rs
19
src/agent.rs
@@ -7,7 +7,7 @@ use crate::resolve::{ArcResolver, StdResolver};
|
||||
use std::time::Duration;
|
||||
|
||||
#[cfg(feature = "cookies")]
|
||||
use {crate::cookies::CookieTin, cookie::Cookie, cookie_store::CookieStore, url::Url};
|
||||
use {crate::cookies::CookieTin, cookie_store::CookieStore};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct AgentBuilder {
|
||||
@@ -107,23 +107,6 @@ impl Agent {
|
||||
Request::new(self.clone(), method.into(), path.into())
|
||||
}
|
||||
|
||||
/// Store a cookie in this agent.
|
||||
///
|
||||
/// ```
|
||||
/// let agent = ureq::agent();
|
||||
///
|
||||
/// let cookie = ureq::Cookie::build("name", "value")
|
||||
/// .secure(true)
|
||||
/// .finish();
|
||||
/// agent.set_cookie(cookie, &"https://example.com/".parse().unwrap());
|
||||
/// ```
|
||||
#[cfg(feature = "cookies")]
|
||||
pub fn set_cookie(&self, cookie: Cookie<'static>, url: &Url) {
|
||||
self.state
|
||||
.cookie_tin
|
||||
.store_response_cookies(Some(cookie).into_iter(), url);
|
||||
}
|
||||
|
||||
/// Make a GET request from this agent.
|
||||
pub fn get(&self, path: &str) -> Request {
|
||||
self.request("GET", path)
|
||||
|
||||
@@ -1,36 +1,26 @@
|
||||
#[cfg(feature = "tls")]
|
||||
#[cfg(feature = "cookies")]
|
||||
#[cfg(feature = "json")]
|
||||
#[test]
|
||||
fn agent_set_cookie() {
|
||||
fn agent_set_header() {
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct HttpBin {
|
||||
headers: HashMap<String, String>,
|
||||
}
|
||||
|
||||
let agent = ureq::Agent::new();
|
||||
let cookie = ureq::Cookie::build("name", "value")
|
||||
.domain("httpbin.org")
|
||||
.secure(true)
|
||||
.finish();
|
||||
agent.set_cookie(cookie, &"https://httpbin.org/".parse().unwrap());
|
||||
let resp = agent
|
||||
.get("https://httpbin.org/get")
|
||||
.set("header", "value")
|
||||
.set("Connection", "close")
|
||||
.call()
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), 200);
|
||||
assert_eq!(
|
||||
"name=value",
|
||||
resp.into_json_deserialize::<HttpBin>()
|
||||
.unwrap()
|
||||
.headers
|
||||
.get("Cookie")
|
||||
.unwrap()
|
||||
);
|
||||
let json = resp.into_json_deserialize::<HttpBin>().unwrap();
|
||||
// println!("{:?}", json);
|
||||
assert_eq!("value", json.headers.get("Header").unwrap());
|
||||
}
|
||||
|
||||
#[cfg(feature = "tls")]
|
||||
|
||||
Reference in New Issue
Block a user