-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cors: Don't overwrite vary header set by the inner service
- Loading branch information
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use std::convert::Infallible; | ||
|
||
use http::{header, HeaderValue, Request, Response}; | ||
use hyper::Body; | ||
use tower::{service_fn, util::ServiceExt, Layer}; | ||
|
||
use crate::cors::CorsLayer; | ||
|
||
#[tokio::test] | ||
#[allow( | ||
clippy::declare_interior_mutable_const, | ||
clippy::borrow_interior_mutable_const | ||
)] | ||
async fn vary_set_by_inner_service() { | ||
const CUSTOM_VARY_HEADERS: HeaderValue = HeaderValue::from_static("accept, accept-encoding"); | ||
const PERMISSIVE_CORS_VARY_HEADERS: HeaderValue = HeaderValue::from_static( | ||
"origin, access-control-request-method, access-control-request-headers", | ||
); | ||
|
||
async fn inner_svc(_: Request<Body>) -> Result<Response<Body>, Infallible> { | ||
Ok(Response::builder() | ||
.header(header::VARY, CUSTOM_VARY_HEADERS) | ||
.body(Body::empty()) | ||
.unwrap()) | ||
} | ||
|
||
let svc = CorsLayer::permissive().layer(service_fn(inner_svc)); | ||
let res = svc.oneshot(Request::new(Body::empty())).await.unwrap(); | ||
let mut vary_headers = res.headers().get_all(header::VARY).into_iter(); | ||
assert_eq!(vary_headers.next(), Some(&CUSTOM_VARY_HEADERS)); | ||
assert_eq!(vary_headers.next(), Some(&PERMISSIVE_CORS_VARY_HEADERS)); | ||
assert_eq!(vary_headers.next(), None); | ||
} |