1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 16:02:59 +01:00

Add insert and remove() to response builder (#707)

This commit is contained in:
Kornel 2019-02-27 12:52:42 +00:00 committed by Douman
parent 0059a55dfb
commit 69d710dbce

View File

@ -366,7 +366,7 @@ impl HttpResponseBuilder {
self self
} }
/// Set a header. /// Append a header.
/// ///
/// ```rust /// ```rust
/// # extern crate actix_web; /// # extern crate actix_web;
@ -394,7 +394,7 @@ impl HttpResponseBuilder {
self self
} }
/// Set a header. /// Append a header.
/// ///
/// ```rust /// ```rust
/// # extern crate actix_web; /// # extern crate actix_web;
@ -426,6 +426,65 @@ impl HttpResponseBuilder {
} }
self self
} }
/// Set or replace a header with a single value.
///
/// ```rust
/// # extern crate actix_web;
/// use actix_web::{http, HttpRequest, HttpResponse};
///
/// fn index(req: HttpRequest) -> HttpResponse {
/// HttpResponse::Ok()
/// .insert("X-TEST", "value")
/// .insert(http::header::CONTENT_TYPE, "application/json")
/// .finish()
/// }
/// fn main() {}
/// ```
pub fn insert<K, V>(&mut self, key: K, value: V) -> &mut Self
where
HeaderName: HttpTryFrom<K>,
V: IntoHeaderValue,
{
if let Some(parts) = parts(&mut self.response, &self.err) {
match HeaderName::try_from(key) {
Ok(key) => match value.try_into() {
Ok(value) => {
parts.headers.insert(key, value);
}
Err(e) => self.err = Some(e.into()),
},
Err(e) => self.err = Some(e.into()),
};
}
self
}
/// Remove all instances of a header already set on this `HttpResponseBuilder`.
///
/// ```rust
/// # extern crate actix_web;
/// use actix_web::{http, HttpRequest, HttpResponse};
///
/// fn index(req: HttpRequest) -> HttpResponse {
/// HttpResponse::Ok()
/// .header(http::header::CONTENT_TYPE, "nevermind") // won't be used
/// .remove(http::header::CONTENT_TYPE)
/// .finish()
/// }
/// ```
pub fn remove<K>(&mut self, key: K) -> &mut Self
where HeaderName: HttpTryFrom<K>
{
if let Some(parts) = parts(&mut self.response, &self.err) {
match HeaderName::try_from(key) {
Ok(key) => {
parts.headers.remove(key);
},
Err(e) => self.err = Some(e.into()),
};
}
self
}
/// Set the custom reason for the response. /// Set the custom reason for the response.
#[inline] #[inline]
@ -1128,6 +1187,40 @@ mod tests {
assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.status(), StatusCode::OK);
} }
#[test]
fn test_insert() {
let resp = HttpResponse::Ok()
.insert("deleteme", "old value")
.insert("deleteme", "new value")
.finish();
assert_eq!("new value", resp.headers().get("deleteme").expect("new value"));
}
#[test]
fn test_remove() {
let resp = HttpResponse::Ok()
.header("deleteme", "value")
.remove("deleteme")
.finish();
assert!(resp.headers().get("deleteme").is_none())
}
#[test]
fn test_remove_replace() {
let resp = HttpResponse::Ok()
.header("some-header", "old_value1")
.header("some-header", "old_value2")
.remove("some-header")
.header("some-header", "new_value1")
.header("some-header", "new_value2")
.remove("unrelated-header")
.finish();
let mut v = resp.headers().get_all("some-header").into_iter();
assert_eq!("new_value1", v.next().unwrap());
assert_eq!("new_value2", v.next().unwrap());
assert_eq!(None, v.next());
}
#[test] #[test]
fn test_upgrade() { fn test_upgrade() {
let resp = HttpResponse::build(StatusCode::OK).upgrade().finish(); let resp = HttpResponse::build(StatusCode::OK).upgrade().finish();