Skip to content

Commit be1c3a9

Browse files
authored
Merge pull request #546 from rkusa/cors-for-errors
Add CORS headers to error responses
2 parents ba65ca4 + 7b30442 commit be1c3a9

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/security/cors.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,4 +379,20 @@ mod test {
379379

380380
assert_eq!(res[http_types::headers::SET_COOKIE][0], "foo=bar");
381381
}
382+
383+
#[async_std::test]
384+
async fn set_cors_headers_to_error_responses() {
385+
let mut app = crate::Server::new();
386+
app.at(ENDPOINT).get(|_| async {
387+
Err::<&str, _>(crate::Error::from_str(
388+
StatusCode::BadRequest,
389+
"bad request",
390+
))
391+
});
392+
app.middleware(CorsMiddleware::new().allow_origin(Origin::from(ALLOW_ORIGIN)));
393+
394+
let res: crate::http::Response = app.respond(request()).await.unwrap();
395+
assert_eq!(res.status(), 400);
396+
assert_eq!(res[headers::ACCESS_CONTROL_ALLOW_ORIGIN], ALLOW_ORIGIN);
397+
}
382398
}

0 commit comments

Comments
 (0)