Skip to content

Commit

Permalink
Fix CORS middleware to retain cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
rkusa authored and jbr committed Jun 17, 2020
1 parent 6ae095d commit e258d23
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/security/cors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl<State: Send + Sync + 'static> Middleware<State> for CorsMiddleware {
return Ok(self.build_preflight_response(&origins).into());
}

let mut response: http_types::Response = next.run(req).await?.into();
let mut response = next.run(req).await?;

response.insert_header(
headers::ACCESS_CONTROL_ALLOW_ORIGIN,
Expand All @@ -177,7 +177,7 @@ impl<State: Send + Sync + 'static> Middleware<State> for CorsMiddleware {
);
}

Ok(response.into())
Ok(response)
})
}
}
Expand Down Expand Up @@ -364,4 +364,21 @@ mod test {

assert_eq!(res.status(), 401);
}

#[async_std::test]
async fn retain_cookies() {
let mut app = crate::Server::new();
app.middleware(CorsMiddleware::new().allow_origin(ALLOW_ORIGIN));
app.at(ENDPOINT).get(|_| async {
let mut res = crate::Response::new(http_types::StatusCode::Ok);
res.insert_cookie(http_types::Cookie::new("foo", "bar"));
Ok(res)
});

let mut req = http_types::Request::new(http_types::Method::Get, endpoint_url());
req.insert_header(http_types::headers::ORIGIN, ALLOW_ORIGIN);
let res: crate::http::Response = app.respond(req).await.unwrap();

assert_eq!(res[http_types::headers::SET_COOKIE][0], "foo=bar");
}
}

0 comments on commit e258d23

Please sign in to comment.