mirror of
https://github.com/ntex-rs/ntex.git
synced 2025-04-03 21:07:39 +03:00
Update cookie dep (#280)
This commit is contained in:
parent
3f976ca71e
commit
944e704ad2
11 changed files with 60 additions and 48 deletions
|
@ -6,6 +6,8 @@
|
||||||
|
|
||||||
* Refactor io tls filters
|
* Refactor io tls filters
|
||||||
|
|
||||||
|
* Update cookie related api
|
||||||
|
|
||||||
## [1.0.0-b.0] - 2024-01-07
|
## [1.0.0-b.0] - 2024-01-07
|
||||||
|
|
||||||
* Use "async fn" in trait for Service definition
|
* Use "async fn" in trait for Service definition
|
||||||
|
|
|
@ -87,7 +87,7 @@ percent-encoding = "2.3"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
serde_urlencoded = "0.7"
|
serde_urlencoded = "0.7"
|
||||||
url-pkg = { version = "2.4", package = "url", optional = true }
|
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
|
# openssl
|
||||||
tls-openssl = { version="0.10", package = "openssl", optional = true }
|
tls-openssl = { version="0.10", package = "openssl", optional = true }
|
||||||
|
|
|
@ -286,12 +286,11 @@ impl ClientRequest {
|
||||||
/// async fn main() {
|
/// async fn main() {
|
||||||
/// let resp = Client::new().get("https://www.rust-lang.org")
|
/// let resp = Client::new().get("https://www.rust-lang.org")
|
||||||
/// .cookie(
|
/// .cookie(
|
||||||
/// cookie::Cookie::build("name", "value")
|
/// cookie::Cookie::build(("name", "value"))
|
||||||
/// .domain("www.rust-lang.org")
|
/// .domain("www.rust-lang.org")
|
||||||
/// .path("/")
|
/// .path("/")
|
||||||
/// .secure(true)
|
/// .secure(true)
|
||||||
/// .http_only(true)
|
/// .http_only(true)
|
||||||
/// .finish(),
|
|
||||||
/// )
|
/// )
|
||||||
/// .send()
|
/// .send()
|
||||||
/// .await;
|
/// .await;
|
||||||
|
@ -299,13 +298,16 @@ impl ClientRequest {
|
||||||
/// println!("Response: {:?}", resp);
|
/// 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() {
|
if self.cookies.is_none() {
|
||||||
let mut jar = CookieJar::new();
|
let mut jar = CookieJar::new();
|
||||||
jar.add(cookie.into_owned());
|
jar.add(cookie.into());
|
||||||
self.cookies = Some(jar)
|
self.cookies = Some(jar)
|
||||||
} else {
|
} else {
|
||||||
self.cookies.as_mut().unwrap().add(cookie.into_owned());
|
self.cookies.as_mut().unwrap().add(cookie.into());
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,8 +64,11 @@ impl TestResponse {
|
||||||
|
|
||||||
#[cfg(feature = "cookie")]
|
#[cfg(feature = "cookie")]
|
||||||
/// Set cookie for this response
|
/// Set cookie for this response
|
||||||
pub fn cookie(mut self, cookie: Cookie<'_>) -> Self {
|
pub fn cookie<C>(mut self, cookie: C) -> Self
|
||||||
self.cookies.add(cookie.into_owned());
|
where
|
||||||
|
C: Into<Cookie<'static>>,
|
||||||
|
{
|
||||||
|
self.cookies.add(cookie.into());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,7 +129,7 @@ mod tests {
|
||||||
TestResponse::default()
|
TestResponse::default()
|
||||||
.version(Version::HTTP_2)
|
.version(Version::HTTP_2)
|
||||||
.header(header::DATE, "data")
|
.header(header::DATE, "data")
|
||||||
.cookie(coo_kie::Cookie::build("name", "value").finish())
|
.cookie(coo_kie::Cookie::build(("name", "value")))
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
#[cfg(not(feature = "cookie"))]
|
#[cfg(not(feature = "cookie"))]
|
||||||
|
|
|
@ -112,9 +112,13 @@ impl<B> Response<B> {
|
||||||
#[cfg(feature = "cookie")]
|
#[cfg(feature = "cookie")]
|
||||||
/// Add a cookie to this response
|
/// Add a cookie to this response
|
||||||
#[inline]
|
#[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;
|
let h = &mut self.head.headers;
|
||||||
HeaderValue::from_str(&cookie.to_string())
|
let c = cookie.into();
|
||||||
|
HeaderValue::try_from(c.to_string())
|
||||||
.map(|c| {
|
.map(|c| {
|
||||||
h.append(header::SET_COOKIE, c);
|
h.append(header::SET_COOKIE, c);
|
||||||
})
|
})
|
||||||
|
@ -443,23 +447,25 @@ impl ResponseBuilder {
|
||||||
/// fn index(req: Request) -> Response {
|
/// fn index(req: Request) -> Response {
|
||||||
/// Response::Ok()
|
/// Response::Ok()
|
||||||
/// .cookie(
|
/// .cookie(
|
||||||
/// cookie::Cookie::build("name", "value")
|
/// cookie::Cookie::build(("name", "value"))
|
||||||
/// .domain("www.rust-lang.org")
|
/// .domain("www.rust-lang.org")
|
||||||
/// .path("/")
|
/// .path("/")
|
||||||
/// .secure(true)
|
/// .secure(true)
|
||||||
/// .http_only(true)
|
/// .http_only(true)
|
||||||
/// .finish(),
|
|
||||||
/// )
|
/// )
|
||||||
/// .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() {
|
if self.cookies.is_none() {
|
||||||
let mut jar = CookieJar::new();
|
let mut jar = CookieJar::new();
|
||||||
jar.add(cookie.into_owned());
|
jar.add(cookie.into());
|
||||||
self.cookies = Some(jar)
|
self.cookies = Some(jar)
|
||||||
} else {
|
} else {
|
||||||
self.cookies.as_mut().unwrap().add(cookie.into_owned());
|
self.cookies.as_mut().unwrap().add(cookie.into());
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -835,12 +841,11 @@ mod tests {
|
||||||
|
|
||||||
let resp = Response::Ok()
|
let resp = Response::Ok()
|
||||||
.cookie(
|
.cookie(
|
||||||
coo_kie::Cookie::build("name", "value")
|
coo_kie::Cookie::build(("name", "value"))
|
||||||
.domain("www.rust-lang.org")
|
.domain("www.rust-lang.org")
|
||||||
.path("/test")
|
.path("/test")
|
||||||
.http_only(true)
|
.http_only(true)
|
||||||
.max_age(time::Duration::days(1))
|
.max_age(time::Duration::days(1)),
|
||||||
.finish(),
|
|
||||||
)
|
)
|
||||||
.del_cookie(&cookies[0])
|
.del_cookie(&cookies[0])
|
||||||
.finish();
|
.finish();
|
||||||
|
@ -865,11 +870,11 @@ mod tests {
|
||||||
.cookie(coo_kie::Cookie::new("original", "val100"))
|
.cookie(coo_kie::Cookie::new("original", "val100"))
|
||||||
.finish();
|
.finish();
|
||||||
|
|
||||||
r.add_cookie(&coo_kie::Cookie::new("cookie2", "val200"))
|
r.add_cookie(coo_kie::Cookie::new("cookie2", "val200"))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
r.add_cookie(&coo_kie::Cookie::new("cookie2", "val250"))
|
r.add_cookie(coo_kie::Cookie::new("cookie2", "val250"))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
r.add_cookie(&coo_kie::Cookie::new("cookie3", "val300"))
|
r.add_cookie(coo_kie::Cookie::new("cookie3", "val300"))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(r.cookies().count(), 4);
|
assert_eq!(r.cookies().count(), 4);
|
||||||
|
@ -1037,7 +1042,7 @@ mod tests {
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
#[cfg(feature = "cookie")]
|
#[cfg(feature = "cookie")]
|
||||||
resp.add_cookie(&coo_kie::Cookie::new("cookie1", "val100"))
|
resp.add_cookie(coo_kie::Cookie::new("cookie1", "val100"))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let (resp, _) = resp.into_parts();
|
let (resp, _) = resp.into_parts();
|
||||||
|
|
||||||
|
|
|
@ -117,8 +117,11 @@ impl TestRequest {
|
||||||
|
|
||||||
#[cfg(feature = "cookie")]
|
#[cfg(feature = "cookie")]
|
||||||
/// Set cookie for this request
|
/// Set cookie for this request
|
||||||
pub fn cookie(&mut self, cookie: Cookie<'_>) -> &mut Self {
|
pub fn cookie<C>(&mut self, cookie: C) -> &mut Self
|
||||||
parts(&mut self.0).cookies.add(cookie.into_owned());
|
where
|
||||||
|
C: Into<Cookie<'static>>,
|
||||||
|
{
|
||||||
|
parts(&mut self.0).cookies.add(cookie.into());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -391,7 +391,10 @@ impl TestRequest {
|
||||||
|
|
||||||
#[cfg(feature = "cookie")]
|
#[cfg(feature = "cookie")]
|
||||||
/// Set cookie for this request
|
/// 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.req.cookie(cookie);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -1216,12 +1219,11 @@ mod tests {
|
||||||
fn test_response_cookies() {
|
fn test_response_cookies() {
|
||||||
let req = TestRequest::default()
|
let req = TestRequest::default()
|
||||||
.cookie(
|
.cookie(
|
||||||
coo_kie::Cookie::build("name", "value")
|
coo_kie::Cookie::build(("name", "value"))
|
||||||
.domain("www.rust-lang.org")
|
.domain("www.rust-lang.org")
|
||||||
.path("/test")
|
.path("/test")
|
||||||
.http_only(true)
|
.http_only(true)
|
||||||
.max_age(::time::Duration::days(1))
|
.max_age(::time::Duration::days(1)),
|
||||||
.finish(),
|
|
||||||
)
|
)
|
||||||
.to_http_request();
|
.to_http_request();
|
||||||
|
|
||||||
|
|
|
@ -208,9 +208,7 @@ impl<Err: ErrorRenderer> FromRequest<Err> for String {
|
||||||
};
|
};
|
||||||
|
|
||||||
// check content-type
|
// check content-type
|
||||||
if let Err(e) = cfg.check_mimetype(req) {
|
cfg.check_mimetype(req)?;
|
||||||
return Err(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// check charset
|
// check charset
|
||||||
let encoding = match req.encoding() {
|
let encoding = match req.encoding() {
|
||||||
|
@ -232,6 +230,7 @@ impl<Err: ErrorRenderer> FromRequest<Err> for String {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Payload configuration for request's payload.
|
/// Payload configuration for request's payload.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct PayloadConfig {
|
pub struct PayloadConfig {
|
||||||
|
|
|
@ -339,13 +339,16 @@ where
|
||||||
|
|
||||||
#[cfg(feature = "cookie")]
|
#[cfg(feature = "cookie")]
|
||||||
/// Set a 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() {
|
if self.cookies.is_none() {
|
||||||
let mut jar = CookieJar::new();
|
let mut jar = CookieJar::new();
|
||||||
jar.add(cookie.into_owned());
|
jar.add(cookie.into());
|
||||||
self.cookies = Some(jar)
|
self.cookies = Some(jar)
|
||||||
} else {
|
} else {
|
||||||
self.cookies.as_mut().unwrap().add(cookie.into_owned());
|
self.cookies.as_mut().unwrap().add(cookie.into());
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -944,7 +947,7 @@ mod tests {
|
||||||
.protocols(["v1", "v2"])
|
.protocols(["v1", "v2"])
|
||||||
.set_header_if_none(header::CONTENT_TYPE, "json")
|
.set_header_if_none(header::CONTENT_TYPE, "json")
|
||||||
.set_header_if_none(header::CONTENT_TYPE, "text")
|
.set_header_if_none(header::CONTENT_TYPE, "text")
|
||||||
.cookie(Cookie::build("cookie1", "value1").finish())
|
.cookie(Cookie::build(("cookie1", "value1")))
|
||||||
.take();
|
.take();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
builder.origin.as_ref().unwrap().to_str().unwrap(),
|
builder.origin.as_ref().unwrap().to_str().unwrap(),
|
||||||
|
|
|
@ -730,13 +730,12 @@ async fn test_client_deflate_encoding_large_random() {
|
||||||
async fn test_client_cookie_handling() {
|
async fn test_client_cookie_handling() {
|
||||||
use std::io::{Error as IoError, ErrorKind};
|
use std::io::{Error as IoError, ErrorKind};
|
||||||
|
|
||||||
let cookie1 = Cookie::build("cookie1", "value1").finish();
|
let cookie1 = Cookie::build(("cookie1", "value1"));
|
||||||
let cookie2 = Cookie::build("cookie2", "value2")
|
let cookie2 = Cookie::build(("cookie2", "value2"))
|
||||||
.domain("www.example.org")
|
.domain("www.example.org")
|
||||||
.path("/")
|
.path("/")
|
||||||
.secure(true)
|
.secure(true)
|
||||||
.http_only(true)
|
.http_only(true);
|
||||||
.finish();
|
|
||||||
// Q: are all these clones really necessary? A: Yes, possibly
|
// Q: are all these clones really necessary? A: Yes, possibly
|
||||||
let cookie1b = cookie1.clone();
|
let cookie1b = cookie1.clone();
|
||||||
let cookie2b = cookie2.clone();
|
let cookie2b = cookie2.clone();
|
||||||
|
|
|
@ -1021,20 +1021,14 @@ async fn test_server_cookies() {
|
||||||
let srv = test::server(|| {
|
let srv = test::server(|| {
|
||||||
App::new().service(web::resource("/").to(|| async {
|
App::new().service(web::resource("/").to(|| async {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.cookie(
|
.cookie(coo_kie::Cookie::build(("first", "first_value")).http_only(true))
|
||||||
coo_kie::CookieBuilder::new("first", "first_value")
|
|
||||||
.http_only(true)
|
|
||||||
.finish(),
|
|
||||||
)
|
|
||||||
.cookie(coo_kie::Cookie::new("second", "first_value"))
|
.cookie(coo_kie::Cookie::new("second", "first_value"))
|
||||||
.cookie(coo_kie::Cookie::new("second", "second_value"))
|
.cookie(coo_kie::Cookie::new("second", "second_value"))
|
||||||
.finish()
|
.finish()
|
||||||
}))
|
}))
|
||||||
});
|
});
|
||||||
|
|
||||||
let first_cookie = coo_kie::CookieBuilder::new("first", "first_value")
|
let first_cookie = coo_kie::Cookie::build(("first", "first_value")).http_only(true);
|
||||||
.http_only(true)
|
|
||||||
.finish();
|
|
||||||
let second_cookie = coo_kie::Cookie::new("second", "second_value");
|
let second_cookie = coo_kie::Cookie::new("second", "second_value");
|
||||||
|
|
||||||
let response = srv.get("/").send().await.unwrap();
|
let response = srv.get("/").send().await.unwrap();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue