| 1 |
#![deny(warnings)] |
| 2 |
use warp::{http::Method, Filter}; |
| 3 |
|
| 4 |
#[tokio::test] |
| 5 |
async fn allow_methods() { |
| 6 |
let cors = warp::cors().allow_methods(&[Method::GET, Method::POST, Method::DELETE]); |
| 7 |
|
| 8 |
let route = warp::any().map(warp::reply).with(cors); |
| 9 |
|
| 10 |
let res = warp::test::request() |
| 11 |
.method("OPTIONS") |
| 12 |
.header("origin", "warp") |
| 13 |
.header("access-control-request-method", "DELETE") |
| 14 |
.reply(&route) |
| 15 |
.await; |
| 16 |
|
| 17 |
assert_eq!(res.status(), 200); |
| 18 |
|
| 19 |
let res = warp::test::request() |
| 20 |
.method("OPTIONS") |
| 21 |
.header("origin", "warp") |
| 22 |
.header("access-control-request-method", "PUT") |
| 23 |
.reply(&route) |
| 24 |
.await; |
| 25 |
|
| 26 |
assert_eq!(res.status(), 403); |
| 27 |
} |
| 28 |
|
| 29 |
#[tokio::test] |
| 30 |
async fn origin_not_allowed() { |
| 31 |
let cors = warp::cors() |
| 32 |
.allow_methods(&[Method::DELETE]) |
| 33 |
.allow_origin("https://hyper.rs"); |
| 34 |
|
| 35 |
let route = warp::any().map(warp::reply).with(cors); |
| 36 |
|
| 37 |
let res = warp::test::request() |
| 38 |
.method("OPTIONS") |
| 39 |
.header("origin", "https://warp.rs") |
| 40 |
.header("access-control-request-method", "DELETE") |
| 41 |
.reply(&route) |
| 42 |
.await; |
| 43 |
|
| 44 |
assert_eq!(res.status(), 403); |
| 45 |
|
| 46 |
let res = warp::test::request() |
| 47 |
.header("origin", "https://warp.rs") |
| 48 |
.header("access-control-request-method", "DELETE") |
| 49 |
.reply(&route) |
| 50 |
.await; |
| 51 |
|
| 52 |
assert_eq!(res.status(), 403); |
| 53 |
} |
| 54 |
|
| 55 |
#[tokio::test] |
| 56 |
async fn headers_not_exposed() { |
| 57 |
let cors = warp::cors() |
| 58 |
.allow_any_origin() |
| 59 |
.allow_methods(&[Method::GET]); |
| 60 |
|
| 61 |
let route = warp::any().map(warp::reply).with(cors); |
| 62 |
|
| 63 |
let res = warp::test::request() |
| 64 |
.method("OPTIONS") |
| 65 |
.header("origin", "https://warp.rs") |
| 66 |
.header("access-control-request-method", "GET") |
| 67 |
.reply(&route) |
| 68 |
.await; |
| 69 |
|
| 70 |
assert_eq!( |
| 71 |
res.headers().contains_key("access-control-expose-headers"), |
| 72 |
false |
| 73 |
); |
| 74 |
|
| 75 |
let res = warp::test::request() |
| 76 |
.method("GET") |
| 77 |
.header("origin", "https://warp.rs") |
| 78 |
.reply(&route) |
| 79 |
.await; |
| 80 |
|
| 81 |
assert_eq!( |
| 82 |
res.headers().contains_key("access-control-expose-headers"), |
| 83 |
false |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
#[tokio::test] |
| 88 |
async fn headers_not_allowed() { |
| 89 |
let cors = warp::cors() |
| 90 |
.allow_methods(&[Method::DELETE]) |
| 91 |
.allow_headers(vec!["x-foo"]); |
| 92 |
|
| 93 |
let route = warp::any().map(warp::reply).with(cors); |
| 94 |
|
| 95 |
let res = warp::test::request() |
| 96 |
.method("OPTIONS") |
| 97 |
.header("origin", "https://warp.rs") |
| 98 |
.header("access-control-request-headers", "x-bar") |
| 99 |
.header("access-control-request-method", "DELETE") |
| 100 |
.reply(&route) |
| 101 |
.await; |
| 102 |
|
| 103 |
assert_eq!(res.status(), 403); |
| 104 |
} |
| 105 |
|
| 106 |
#[tokio::test] |
| 107 |
async fn success() { |
| 108 |
let cors = warp::cors() |
| 109 |
.allow_credentials(true) |
| 110 |
.allow_headers(vec!["x-foo", "x-bar"]) |
| 111 |
.allow_methods(&[Method::POST, Method::DELETE]) |
| 112 |
.expose_header("x-header1") |
| 113 |
.expose_headers(vec!["x-header2"]) |
| 114 |
.max_age(30); |
| 115 |
|
| 116 |
let route = warp::any().map(warp::reply).with(cors); |
| 117 |
|
| 118 |
// preflight |
| 119 |
let res = warp::test::request() |
| 120 |
.method("OPTIONS") |
| 121 |
.header("origin", "https://hyper.rs") |
| 122 |
.header("access-control-request-headers", "x-bar, x-foo") |
| 123 |
.header("access-control-request-method", "DELETE") |
| 124 |
.reply(&route) |
| 125 |
.await; |
| 126 |
assert_eq!(res.status(), 200); |
| 127 |
assert_eq!( |
| 128 |
res.headers()["access-control-allow-origin"], |
| 129 |
"https://hyper.rs" |
| 130 |
); |
| 131 |
assert_eq!(res.headers()["access-control-allow-credentials"], "true"); |
| 132 |
let allowed_headers = &res.headers()["access-control-allow-headers"]; |
| 133 |
assert!(allowed_headers == "x-bar, x-foo" || allowed_headers == "x-foo, x-bar"); |
| 134 |
let exposed_headers = &res.headers()["access-control-expose-headers"]; |
| 135 |
assert!(exposed_headers == "x-header1, x-header2" || exposed_headers == "x-header2, x-header1"); |
| 136 |
assert_eq!(res.headers()["access-control-max-age"], "30"); |
| 137 |
let methods = &res.headers()["access-control-allow-methods"]; |
| 138 |
assert!( |
| 139 |
// HashSet randomly orders these... |
| 140 |
methods == "DELETE, POST" || methods == "POST, DELETE", |
| 141 |
"access-control-allow-methods: {:?}", |
| 142 |
methods, |
| 143 |
); |
| 144 |
|
| 145 |
// cors request |
| 146 |
let res = warp::test::request() |
| 147 |
.method("DELETE") |
| 148 |
.header("origin", "https://hyper.rs") |
| 149 |
.header("x-foo", "hello") |
| 150 |
.header("x-bar", "world") |
| 151 |
.reply(&route) |
| 152 |
.await; |
| 153 |
assert_eq!(res.status(), 200); |
| 154 |
assert_eq!( |
| 155 |
res.headers()["access-control-allow-origin"], |
| 156 |
"https://hyper.rs" |
| 157 |
); |
| 158 |
assert_eq!(res.headers()["access-control-allow-credentials"], "true"); |
| 159 |
assert_eq!(res.headers().get("access-control-max-age"), None); |
| 160 |
assert_eq!(res.headers().get("access-control-allow-methods"), None); |
| 161 |
let exposed_headers = &res.headers()["access-control-expose-headers"]; |
| 162 |
assert!(exposed_headers == "x-header1, x-header2" || exposed_headers == "x-header2, x-header1"); |
| 163 |
} |
| 164 |
|
| 165 |
#[tokio::test] |
| 166 |
async fn with_log() { |
| 167 |
let cors = warp::cors() |
| 168 |
.allow_any_origin() |
| 169 |
.allow_methods(&[Method::GET]); |
| 170 |
|
| 171 |
let route = warp::any() |
| 172 |
.map(warp::reply) |
| 173 |
.with(cors) |
| 174 |
.with(warp::log("cors test")); |
| 175 |
|
| 176 |
let res = warp::test::request() |
| 177 |
.method("OPTIONS") |
| 178 |
.header("origin", "warp") |
| 179 |
.header("access-control-request-method", "GET") |
| 180 |
.reply(&route) |
| 181 |
.await; |
| 182 |
|
| 183 |
assert_eq!(res.status(), 200); |
| 184 |
} |