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:
Martin Algesten
2020-10-30 05:55:22 +01:00
parent e37acc85b2
commit 18201a08c5
2 changed files with 7 additions and 34 deletions

View File

@@ -7,7 +7,7 @@ use crate::resolve::{ArcResolver, StdResolver};
use std::time::Duration; use std::time::Duration;
#[cfg(feature = "cookies")] #[cfg(feature = "cookies")]
use {crate::cookies::CookieTin, cookie::Cookie, cookie_store::CookieStore, url::Url}; use {crate::cookies::CookieTin, cookie_store::CookieStore};
#[derive(Debug)] #[derive(Debug)]
pub struct AgentBuilder { pub struct AgentBuilder {
@@ -107,23 +107,6 @@ impl Agent {
Request::new(self.clone(), method.into(), path.into()) 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. /// Make a GET request from this agent.
pub fn get(&self, path: &str) -> Request { pub fn get(&self, path: &str) -> Request {
self.request("GET", path) self.request("GET", path)

View File

@@ -1,36 +1,26 @@
#[cfg(feature = "tls")] #[cfg(feature = "tls")]
#[cfg(feature = "cookies")]
#[cfg(feature = "json")] #[cfg(feature = "json")]
#[test] #[test]
fn agent_set_cookie() { fn agent_set_header() {
use serde::Deserialize; use serde::Deserialize;
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Deserialize)] #[derive(Deserialize, Debug)]
struct HttpBin { struct HttpBin {
headers: HashMap<String, String>, headers: HashMap<String, String>,
} }
let agent = ureq::Agent::new(); 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 let resp = agent
.get("https://httpbin.org/get") .get("https://httpbin.org/get")
.set("header", "value")
.set("Connection", "close") .set("Connection", "close")
.call() .call()
.unwrap(); .unwrap();
assert_eq!(resp.status(), 200); assert_eq!(resp.status(), 200);
assert_eq!( let json = resp.into_json_deserialize::<HttpBin>().unwrap();
"name=value", // println!("{:?}", json);
resp.into_json_deserialize::<HttpBin>() assert_eq!("value", json.headers.get("Header").unwrap());
.unwrap()
.headers
.get("Cookie")
.unwrap()
);
} }
#[cfg(feature = "tls")] #[cfg(feature = "tls")]