Update cookie dep (#280)

This commit is contained in:
Nikolay Kim 2024-01-08 17:07:54 +06:00 committed by GitHub
parent 3f976ca71e
commit 944e704ad2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 60 additions and 48 deletions

View file

@ -6,6 +6,8 @@
* Refactor io tls filters
* Update cookie related api
## [1.0.0-b.0] - 2024-01-07
* Use "async fn" in trait for Service definition

View file

@ -87,7 +87,7 @@ percent-encoding = "2.3"
serde_json = "1.0"
serde_urlencoded = "0.7"
url-pkg = { version = "2.4", package = "url", optional = true }
coo-kie = { version = "0.17", package = "cookie", optional = true }
coo-kie = { version = "0.18", package = "cookie", optional = true }
# openssl
tls-openssl = { version="0.10", package = "openssl", optional = true }

View file

@ -286,12 +286,11 @@ impl ClientRequest {
/// async fn main() {
/// let resp = Client::new().get("https://www.rust-lang.org")
/// .cookie(
/// cookie::Cookie::build("name", "value")
/// cookie::Cookie::build(("name", "value"))
/// .domain("www.rust-lang.org")
/// .path("/")
/// .secure(true)
/// .http_only(true)
/// .finish(),
/// )
/// .send()
/// .await;
@ -299,13 +298,16 @@ impl ClientRequest {
/// println!("Response: {:?}", resp);
/// }
/// ```
pub fn cookie(mut self, cookie: Cookie<'_>) -> Self {
pub fn cookie<C>(mut self, cookie: C) -> Self
where
C: Into<Cookie<'static>>,
{
if self.cookies.is_none() {
let mut jar = CookieJar::new();
jar.add(cookie.into_owned());
jar.add(cookie.into());
self.cookies = Some(jar)
} else {
self.cookies.as_mut().unwrap().add(cookie.into_owned());
self.cookies.as_mut().unwrap().add(cookie.into());
}
self
}

View file

@ -64,8 +64,11 @@ impl TestResponse {
#[cfg(feature = "cookie")]
/// Set cookie for this response
pub fn cookie(mut self, cookie: Cookie<'_>) -> Self {
self.cookies.add(cookie.into_owned());
pub fn cookie<C>(mut self, cookie: C) -> Self
where
C: Into<Cookie<'static>>,
{
self.cookies.add(cookie.into());
self
}
@ -126,7 +129,7 @@ mod tests {
TestResponse::default()
.version(Version::HTTP_2)
.header(header::DATE, "data")
.cookie(coo_kie::Cookie::build("name", "value").finish())
.cookie(coo_kie::Cookie::build(("name", "value")))
.finish()
}
#[cfg(not(feature = "cookie"))]

View file

@ -112,9 +112,13 @@ impl<B> Response<B> {
#[cfg(feature = "cookie")]
/// Add a cookie to this response
#[inline]
pub fn add_cookie(&mut self, cookie: &Cookie<'_>) -> Result<(), HttpError> {
pub fn add_cookie<'a, C>(&mut self, cookie: C) -> Result<(), HttpError>
where
C: Into<Cookie<'a>>,
{
let h = &mut self.head.headers;
HeaderValue::from_str(&cookie.to_string())
let c = cookie.into();
HeaderValue::try_from(c.to_string())
.map(|c| {
h.append(header::SET_COOKIE, c);
})
@ -443,23 +447,25 @@ impl ResponseBuilder {
/// fn index(req: Request) -> Response {
/// Response::Ok()
/// .cookie(
/// cookie::Cookie::build("name", "value")
/// cookie::Cookie::build(("name", "value"))
/// .domain("www.rust-lang.org")
/// .path("/")
/// .secure(true)
/// .http_only(true)
/// .finish(),
/// )
/// .finish()
/// }
/// ```
pub fn cookie(&mut self, cookie: Cookie<'_>) -> &mut Self {
pub fn cookie<C>(&mut self, cookie: C) -> &mut Self
where
C: Into<Cookie<'static>>,
{
if self.cookies.is_none() {
let mut jar = CookieJar::new();
jar.add(cookie.into_owned());
jar.add(cookie.into());
self.cookies = Some(jar)
} else {
self.cookies.as_mut().unwrap().add(cookie.into_owned());
self.cookies.as_mut().unwrap().add(cookie.into());
}
self
}
@ -835,12 +841,11 @@ mod tests {
let resp = Response::Ok()
.cookie(
coo_kie::Cookie::build("name", "value")
coo_kie::Cookie::build(("name", "value"))
.domain("www.rust-lang.org")
.path("/test")
.http_only(true)
.max_age(time::Duration::days(1))
.finish(),
.max_age(time::Duration::days(1)),
)
.del_cookie(&cookies[0])
.finish();
@ -865,11 +870,11 @@ mod tests {
.cookie(coo_kie::Cookie::new("original", "val100"))
.finish();
r.add_cookie(&coo_kie::Cookie::new("cookie2", "val200"))
r.add_cookie(coo_kie::Cookie::new("cookie2", "val200"))
.unwrap();
r.add_cookie(&coo_kie::Cookie::new("cookie2", "val250"))
r.add_cookie(coo_kie::Cookie::new("cookie2", "val250"))
.unwrap();
r.add_cookie(&coo_kie::Cookie::new("cookie3", "val300"))
r.add_cookie(coo_kie::Cookie::new("cookie3", "val300"))
.unwrap();
assert_eq!(r.cookies().count(), 4);
@ -1037,7 +1042,7 @@ mod tests {
assert_eq!(resp.status(), StatusCode::OK);
#[cfg(feature = "cookie")]
resp.add_cookie(&coo_kie::Cookie::new("cookie1", "val100"))
resp.add_cookie(coo_kie::Cookie::new("cookie1", "val100"))
.unwrap();
let (resp, _) = resp.into_parts();

View file

@ -117,8 +117,11 @@ impl TestRequest {
#[cfg(feature = "cookie")]
/// Set cookie for this request
pub fn cookie(&mut self, cookie: Cookie<'_>) -> &mut Self {
parts(&mut self.0).cookies.add(cookie.into_owned());
pub fn cookie<C>(&mut self, cookie: C) -> &mut Self
where
C: Into<Cookie<'static>>,
{
parts(&mut self.0).cookies.add(cookie.into());
self
}

View file

@ -391,7 +391,10 @@ impl TestRequest {
#[cfg(feature = "cookie")]
/// Set cookie for this request
pub fn cookie(mut self, cookie: Cookie<'_>) -> Self {
pub fn cookie<C>(mut self, cookie: C) -> Self
where
C: Into<Cookie<'static>>,
{
self.req.cookie(cookie);
self
}
@ -1216,12 +1219,11 @@ mod tests {
fn test_response_cookies() {
let req = TestRequest::default()
.cookie(
coo_kie::Cookie::build("name", "value")
coo_kie::Cookie::build(("name", "value"))
.domain("www.rust-lang.org")
.path("/test")
.http_only(true)
.max_age(::time::Duration::days(1))
.finish(),
.max_age(::time::Duration::days(1)),
)
.to_http_request();

View file

@ -208,9 +208,7 @@ impl<Err: ErrorRenderer> FromRequest<Err> for String {
};
// check content-type
if let Err(e) = cfg.check_mimetype(req) {
return Err(e);
}
cfg.check_mimetype(req)?;
// check charset
let encoding = match req.encoding() {
@ -232,6 +230,7 @@ impl<Err: ErrorRenderer> FromRequest<Err> for String {
}
}
}
/// Payload configuration for request's payload.
#[derive(Clone, Debug)]
pub struct PayloadConfig {

View file

@ -339,13 +339,16 @@ where
#[cfg(feature = "cookie")]
/// Set a cookie
pub fn cookie(&mut self, cookie: Cookie<'_>) -> &mut Self {
pub fn cookie<C>(&mut self, cookie: C) -> &mut Self
where
C: Into<Cookie<'static>>,
{
if self.cookies.is_none() {
let mut jar = CookieJar::new();
jar.add(cookie.into_owned());
jar.add(cookie.into());
self.cookies = Some(jar)
} else {
self.cookies.as_mut().unwrap().add(cookie.into_owned());
self.cookies.as_mut().unwrap().add(cookie.into());
}
self
}
@ -944,7 +947,7 @@ mod tests {
.protocols(["v1", "v2"])
.set_header_if_none(header::CONTENT_TYPE, "json")
.set_header_if_none(header::CONTENT_TYPE, "text")
.cookie(Cookie::build("cookie1", "value1").finish())
.cookie(Cookie::build(("cookie1", "value1")))
.take();
assert_eq!(
builder.origin.as_ref().unwrap().to_str().unwrap(),

View file

@ -730,13 +730,12 @@ async fn test_client_deflate_encoding_large_random() {
async fn test_client_cookie_handling() {
use std::io::{Error as IoError, ErrorKind};
let cookie1 = Cookie::build("cookie1", "value1").finish();
let cookie2 = Cookie::build("cookie2", "value2")
let cookie1 = Cookie::build(("cookie1", "value1"));
let cookie2 = Cookie::build(("cookie2", "value2"))
.domain("www.example.org")
.path("/")
.secure(true)
.http_only(true)
.finish();
.http_only(true);
// Q: are all these clones really necessary? A: Yes, possibly
let cookie1b = cookie1.clone();
let cookie2b = cookie2.clone();

View file

@ -1021,20 +1021,14 @@ async fn test_server_cookies() {
let srv = test::server(|| {
App::new().service(web::resource("/").to(|| async {
HttpResponse::Ok()
.cookie(
coo_kie::CookieBuilder::new("first", "first_value")
.http_only(true)
.finish(),
)
.cookie(coo_kie::Cookie::build(("first", "first_value")).http_only(true))
.cookie(coo_kie::Cookie::new("second", "first_value"))
.cookie(coo_kie::Cookie::new("second", "second_value"))
.finish()
}))
});
let first_cookie = coo_kie::CookieBuilder::new("first", "first_value")
.http_only(true)
.finish();
let first_cookie = coo_kie::Cookie::build(("first", "first_value")).http_only(true);
let second_cookie = coo_kie::Cookie::new("second", "second_value");
let response = srv.get("/").send().await.unwrap();