| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
"""Test generic usage""" |
| 16 |
|
| 17 |
import pathlib |
| 18 |
|
| 19 |
import pytest |
| 20 |
|
| 21 |
from aiohttp import hdrs, web |
| 22 |
from aiohttp_cors import CorsViewMixin, ResourceOptions, setup as _setup |
| 23 |
|
| 24 |
TEST_BODY = "Hello, world" |
| 25 |
SERVER_CUSTOM_HEADER_NAME = "X-Server-Custom-Header" |
| 26 |
SERVER_CUSTOM_HEADER_VALUE = "some value" |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
async def handler(request: web.Request) -> web.StreamResponse: |
| 31 |
"""Dummy request handler, returning `TEST_BODY`.""" |
| 32 |
response = web.Response(text=TEST_BODY) |
| 33 |
|
| 34 |
response.headers[SERVER_CUSTOM_HEADER_NAME] = SERVER_CUSTOM_HEADER_VALUE |
| 35 |
|
| 36 |
return response |
| 37 |
|
| 38 |
|
| 39 |
class WebViewHandler(web.View, CorsViewMixin): |
| 40 |
|
| 41 |
async def get(self) -> web.StreamResponse: |
| 42 |
"""Dummy request handler, returning `TEST_BODY`.""" |
| 43 |
response = web.Response(text=TEST_BODY) |
| 44 |
|
| 45 |
response.headers[SERVER_CUSTOM_HEADER_NAME] = SERVER_CUSTOM_HEADER_VALUE |
| 46 |
|
| 47 |
return response |
| 48 |
|
| 49 |
|
| 50 |
@pytest.fixture(params=["resource", "view", "route"]) |
| 51 |
def make_app(request): |
| 52 |
def inner(defaults, route_config): |
| 53 |
app = web.Application() |
| 54 |
cors = _setup(app, defaults=defaults) |
| 55 |
|
| 56 |
if request.param == "resource": |
| 57 |
resource = cors.add(app.router.add_resource("/resource")) |
| 58 |
cors.add(resource.add_route("GET", handler), route_config) |
| 59 |
elif request.param == "view": |
| 60 |
WebViewHandler.cors_config = route_config |
| 61 |
cors.add(app.router.add_route("*", "/resource", WebViewHandler)) |
| 62 |
elif request.param == "route": |
| 63 |
cors.add(app.router.add_route("GET", "/resource", handler), route_config) |
| 64 |
else: |
| 65 |
raise RuntimeError(f"unknown parameter {request.param}") |
| 66 |
|
| 67 |
return app |
| 68 |
|
| 69 |
return inner |
| 70 |
|
| 71 |
|
| 72 |
async def test_message_roundtrip(aiohttp_client): |
| 73 |
"""Test that aiohttp server is correctly setup in the base class.""" |
| 74 |
|
| 75 |
app = web.Application() |
| 76 |
app.router.add_route("GET", "/", handler) |
| 77 |
|
| 78 |
client = await aiohttp_client(app) |
| 79 |
|
| 80 |
resp = await client.get("/") |
| 81 |
assert resp.status == 200 |
| 82 |
data = await resp.text() |
| 83 |
|
| 84 |
assert data == TEST_BODY |
| 85 |
|
| 86 |
|
| 87 |
async def test_dummy_setup(aiohttp_server): |
| 88 |
"""Test a dummy configuration.""" |
| 89 |
app = web.Application() |
| 90 |
_setup(app) |
| 91 |
|
| 92 |
await aiohttp_server(app) |
| 93 |
|
| 94 |
|
| 95 |
async def test_dummy_setup_roundtrip(aiohttp_client): |
| 96 |
"""Test a dummy configuration with a message round-trip.""" |
| 97 |
app = web.Application() |
| 98 |
_setup(app) |
| 99 |
|
| 100 |
app.router.add_route("GET", "/", handler) |
| 101 |
|
| 102 |
client = await aiohttp_client(app) |
| 103 |
|
| 104 |
resp = await client.get("/") |
| 105 |
assert resp.status == 200 |
| 106 |
data = await resp.text() |
| 107 |
|
| 108 |
assert data == TEST_BODY |
| 109 |
|
| 110 |
|
| 111 |
async def test_dummy_setup_roundtrip_resource(aiohttp_client): |
| 112 |
"""Test a dummy configuration with a message round-trip.""" |
| 113 |
app = web.Application() |
| 114 |
_setup(app) |
| 115 |
|
| 116 |
app.router.add_resource("/").add_route("GET", handler) |
| 117 |
|
| 118 |
client = await aiohttp_client(app) |
| 119 |
|
| 120 |
resp = await client.get("/") |
| 121 |
assert resp.status == 200 |
| 122 |
data = await resp.text() |
| 123 |
|
| 124 |
assert data == TEST_BODY |
| 125 |
|
| 126 |
|
| 127 |
async def test_simple_no_origin(aiohttp_client, make_app): |
| 128 |
app = make_app(None, {"http://client1.example.org": ResourceOptions()}) |
| 129 |
|
| 130 |
client = await aiohttp_client(app) |
| 131 |
|
| 132 |
resp = await client.get("/resource") |
| 133 |
assert resp.status == 200 |
| 134 |
resp_text = await resp.text() |
| 135 |
assert resp_text == TEST_BODY |
| 136 |
|
| 137 |
for header_name in { |
| 138 |
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN, |
| 139 |
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS, |
| 140 |
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS, |
| 141 |
}: |
| 142 |
assert header_name not in resp.headers |
| 143 |
|
| 144 |
|
| 145 |
async def test_simple_allowed_origin(aiohttp_client, make_app): |
| 146 |
app = make_app(None, {"http://client1.example.org": ResourceOptions()}) |
| 147 |
|
| 148 |
client = await aiohttp_client(app) |
| 149 |
|
| 150 |
resp = await client.get( |
| 151 |
"/resource", headers={hdrs.ORIGIN: "http://client1.example.org"} |
| 152 |
) |
| 153 |
assert resp.status == 200 |
| 154 |
resp_text = await resp.text() |
| 155 |
assert resp_text == TEST_BODY |
| 156 |
|
| 157 |
for hdr, val in { |
| 158 |
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN: "http://client1.example.org", |
| 159 |
}.items(): |
| 160 |
assert resp.headers.get(hdr) == val |
| 161 |
|
| 162 |
for header_name in { |
| 163 |
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS, |
| 164 |
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS, |
| 165 |
}: |
| 166 |
assert header_name not in resp.headers |
| 167 |
|
| 168 |
|
| 169 |
async def test_simple_not_allowed_origin(aiohttp_client, make_app): |
| 170 |
app = make_app(None, {"http://client1.example.org": ResourceOptions()}) |
| 171 |
|
| 172 |
client = await aiohttp_client(app) |
| 173 |
|
| 174 |
resp = await client.get( |
| 175 |
"/resource", headers={hdrs.ORIGIN: "http://client2.example.org"} |
| 176 |
) |
| 177 |
assert resp.status == 200 |
| 178 |
resp_text = await resp.text() |
| 179 |
assert resp_text == TEST_BODY |
| 180 |
|
| 181 |
for header_name in { |
| 182 |
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN, |
| 183 |
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS, |
| 184 |
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS, |
| 185 |
}: |
| 186 |
assert header_name not in resp.headers |
| 187 |
|
| 188 |
|
| 189 |
async def test_simple_explicit_port(aiohttp_client, make_app): |
| 190 |
app = make_app(None, {"http://client1.example.org": ResourceOptions()}) |
| 191 |
|
| 192 |
client = await aiohttp_client(app) |
| 193 |
|
| 194 |
resp = await client.get( |
| 195 |
"/resource", headers={hdrs.ORIGIN: "http://client1.example.org:80"} |
| 196 |
) |
| 197 |
assert resp.status == 200 |
| 198 |
resp_text = await resp.text() |
| 199 |
assert resp_text == TEST_BODY |
| 200 |
|
| 201 |
for header_name in { |
| 202 |
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN, |
| 203 |
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS, |
| 204 |
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS, |
| 205 |
}: |
| 206 |
assert header_name not in resp.headers |
| 207 |
|
| 208 |
|
| 209 |
async def test_simple_different_scheme(aiohttp_client, make_app): |
| 210 |
app = make_app(None, {"http://client1.example.org": ResourceOptions()}) |
| 211 |
|
| 212 |
client = await aiohttp_client(app) |
| 213 |
|
| 214 |
resp = await client.get( |
| 215 |
"/resource", headers={hdrs.ORIGIN: "https://client1.example.org"} |
| 216 |
) |
| 217 |
assert resp.status == 200 |
| 218 |
resp_text = await resp.text() |
| 219 |
assert resp_text == TEST_BODY |
| 220 |
|
| 221 |
for header_name in { |
| 222 |
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN, |
| 223 |
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS, |
| 224 |
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS, |
| 225 |
}: |
| 226 |
assert header_name not in resp.headers |
| 227 |
|
| 228 |
|
| 229 |
@pytest.fixture( |
| 230 |
params=[ |
| 231 |
(None, {"http://client1.example.org": ResourceOptions(allow_credentials=True)}), |
| 232 |
({"http://client1.example.org": ResourceOptions(allow_credentials=True)}, None), |
| 233 |
] |
| 234 |
) |
| 235 |
def app_for_credentials(make_app, request): |
| 236 |
return make_app(*request.param) |
| 237 |
|
| 238 |
|
| 239 |
async def test_cred_no_origin(aiohttp_client, app_for_credentials): |
| 240 |
app = app_for_credentials |
| 241 |
|
| 242 |
client = await aiohttp_client(app) |
| 243 |
|
| 244 |
resp = await client.get("/resource") |
| 245 |
assert resp.status == 200 |
| 246 |
resp_text = await resp.text() |
| 247 |
assert resp_text == TEST_BODY |
| 248 |
|
| 249 |
for header_name in { |
| 250 |
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN, |
| 251 |
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS, |
| 252 |
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS, |
| 253 |
}: |
| 254 |
assert header_name not in resp.headers |
| 255 |
|
| 256 |
|
| 257 |
async def test_cred_allowed_origin(aiohttp_client, app_for_credentials): |
| 258 |
app = app_for_credentials |
| 259 |
|
| 260 |
client = await aiohttp_client(app) |
| 261 |
|
| 262 |
resp = await client.get( |
| 263 |
"/resource", headers={hdrs.ORIGIN: "http://client1.example.org"} |
| 264 |
) |
| 265 |
assert resp.status == 200 |
| 266 |
resp_text = await resp.text() |
| 267 |
assert resp_text == TEST_BODY |
| 268 |
|
| 269 |
for hdr, val in { |
| 270 |
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN: "http://client1.example.org", |
| 271 |
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS: "true", |
| 272 |
}.items(): |
| 273 |
assert resp.headers.get(hdr) == val |
| 274 |
|
| 275 |
for header_name in { |
| 276 |
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS, |
| 277 |
}: |
| 278 |
assert header_name not in resp.headers |
| 279 |
|
| 280 |
|
| 281 |
async def test_cred_disallowed_origin(aiohttp_client, app_for_credentials): |
| 282 |
app = app_for_credentials |
| 283 |
|
| 284 |
client = await aiohttp_client(app) |
| 285 |
|
| 286 |
resp = await client.get( |
| 287 |
"/resource", headers={hdrs.ORIGIN: "http://client2.example.org"} |
| 288 |
) |
| 289 |
assert resp.status == 200 |
| 290 |
resp_text = await resp.text() |
| 291 |
assert resp_text == TEST_BODY |
| 292 |
|
| 293 |
for header_name in { |
| 294 |
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN, |
| 295 |
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS, |
| 296 |
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS, |
| 297 |
}: |
| 298 |
assert header_name not in resp.headers |
| 299 |
|
| 300 |
|
| 301 |
async def test_simple_expose_headers_no_origin(aiohttp_client, make_app): |
| 302 |
app = make_app( |
| 303 |
None, |
| 304 |
{ |
| 305 |
"http://client1.example.org": ResourceOptions( |
| 306 |
expose_headers=(SERVER_CUSTOM_HEADER_NAME,) |
| 307 |
) |
| 308 |
}, |
| 309 |
) |
| 310 |
|
| 311 |
client = await aiohttp_client(app) |
| 312 |
|
| 313 |
resp = await client.get("/resource") |
| 314 |
assert resp.status == 200 |
| 315 |
resp_text = await resp.text() |
| 316 |
assert resp_text == TEST_BODY |
| 317 |
|
| 318 |
for header_name in { |
| 319 |
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN, |
| 320 |
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS, |
| 321 |
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS, |
| 322 |
}: |
| 323 |
assert header_name not in resp.headers |
| 324 |
|
| 325 |
|
| 326 |
async def test_simple_expose_headers_allowed_origin(aiohttp_client, make_app): |
| 327 |
app = make_app( |
| 328 |
None, |
| 329 |
{ |
| 330 |
"http://client1.example.org": ResourceOptions( |
| 331 |
expose_headers=(SERVER_CUSTOM_HEADER_NAME,) |
| 332 |
) |
| 333 |
}, |
| 334 |
) |
| 335 |
|
| 336 |
client = await aiohttp_client(app) |
| 337 |
|
| 338 |
resp = await client.get( |
| 339 |
"/resource", headers={hdrs.ORIGIN: "http://client1.example.org"} |
| 340 |
) |
| 341 |
assert resp.status == 200 |
| 342 |
resp_text = await resp.text() |
| 343 |
assert resp_text == TEST_BODY |
| 344 |
|
| 345 |
for hdr, val in { |
| 346 |
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN: "http://client1.example.org", |
| 347 |
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS: SERVER_CUSTOM_HEADER_NAME, |
| 348 |
}.items(): |
| 349 |
assert resp.headers.get(hdr) == val |
| 350 |
|
| 351 |
for header_name in { |
| 352 |
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS, |
| 353 |
}: |
| 354 |
assert header_name not in resp.headers |
| 355 |
|
| 356 |
|
| 357 |
async def test_simple_expose_headers_not_allowed_origin(aiohttp_client, make_app): |
| 358 |
app = make_app( |
| 359 |
None, |
| 360 |
{ |
| 361 |
"http://client1.example.org": ResourceOptions( |
| 362 |
expose_headers=(SERVER_CUSTOM_HEADER_NAME,) |
| 363 |
) |
| 364 |
}, |
| 365 |
) |
| 366 |
|
| 367 |
client = await aiohttp_client(app) |
| 368 |
|
| 369 |
resp = await client.get( |
| 370 |
"/resource", headers={hdrs.ORIGIN: "http://client2.example.org"} |
| 371 |
) |
| 372 |
assert resp.status == 200 |
| 373 |
resp_text = await resp.text() |
| 374 |
assert resp_text == TEST_BODY |
| 375 |
|
| 376 |
for header_name in { |
| 377 |
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN, |
| 378 |
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS, |
| 379 |
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS, |
| 380 |
}: |
| 381 |
assert header_name not in resp.headers |
| 382 |
|
| 383 |
|
| 384 |
async def test_preflight_default_no_origin(aiohttp_client, make_app): |
| 385 |
app = make_app(None, {"http://client1.example.org": ResourceOptions()}) |
| 386 |
|
| 387 |
client = await aiohttp_client(app) |
| 388 |
|
| 389 |
resp = await client.options("/resource") |
| 390 |
assert resp.status == 403 |
| 391 |
resp_text = await resp.text() |
| 392 |
assert "origin header is not specified" in resp_text |
| 393 |
|
| 394 |
for header_name in { |
| 395 |
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN, |
| 396 |
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS, |
| 397 |
hdrs.ACCESS_CONTROL_MAX_AGE, |
| 398 |
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS, |
| 399 |
hdrs.ACCESS_CONTROL_ALLOW_METHODS, |
| 400 |
hdrs.ACCESS_CONTROL_ALLOW_HEADERS, |
| 401 |
}: |
| 402 |
assert header_name not in resp.headers |
| 403 |
|
| 404 |
|
| 405 |
async def test_preflight_default_no_method(aiohttp_client, make_app): |
| 406 |
|
| 407 |
app = make_app(None, {"http://client1.example.org": ResourceOptions()}) |
| 408 |
|
| 409 |
client = await aiohttp_client(app) |
| 410 |
|
| 411 |
resp = await client.options( |
| 412 |
"/resource", |
| 413 |
headers={ |
| 414 |
hdrs.ORIGIN: "http://client1.example.org", |
| 415 |
}, |
| 416 |
) |
| 417 |
assert resp.status == 403 |
| 418 |
resp_text = await resp.text() |
| 419 |
assert "'Access-Control-Request-Method' header is not specified" in resp_text |
| 420 |
|
| 421 |
for header_name in { |
| 422 |
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN, |
| 423 |
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS, |
| 424 |
hdrs.ACCESS_CONTROL_MAX_AGE, |
| 425 |
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS, |
| 426 |
hdrs.ACCESS_CONTROL_ALLOW_METHODS, |
| 427 |
hdrs.ACCESS_CONTROL_ALLOW_HEADERS, |
| 428 |
}: |
| 429 |
assert header_name not in resp.headers |
| 430 |
|
| 431 |
|
| 432 |
async def test_preflight_default_origin_and_method(aiohttp_client, make_app): |
| 433 |
|
| 434 |
app = make_app(None, {"http://client1.example.org": ResourceOptions()}) |
| 435 |
|
| 436 |
client = await aiohttp_client(app) |
| 437 |
|
| 438 |
resp = await client.options( |
| 439 |
"/resource", |
| 440 |
headers={ |
| 441 |
hdrs.ORIGIN: "http://client1.example.org", |
| 442 |
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "GET", |
| 443 |
}, |
| 444 |
) |
| 445 |
assert resp.status == 200 |
| 446 |
resp_text = await resp.text() |
| 447 |
assert "" == resp_text |
| 448 |
|
| 449 |
for hdr, val in { |
| 450 |
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN: "http://client1.example.org", |
| 451 |
hdrs.ACCESS_CONTROL_ALLOW_METHODS: "GET", |
| 452 |
}.items(): |
| 453 |
assert resp.headers.get(hdr) == val |
| 454 |
|
| 455 |
for header_name in { |
| 456 |
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS, |
| 457 |
hdrs.ACCESS_CONTROL_MAX_AGE, |
| 458 |
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS, |
| 459 |
hdrs.ACCESS_CONTROL_ALLOW_HEADERS, |
| 460 |
}: |
| 461 |
assert header_name not in resp.headers |
| 462 |
|
| 463 |
|
| 464 |
async def test_preflight_default_disallowed_origin(aiohttp_client, make_app): |
| 465 |
|
| 466 |
app = make_app(None, {"http://client1.example.org": ResourceOptions()}) |
| 467 |
|
| 468 |
client = await aiohttp_client(app) |
| 469 |
|
| 470 |
resp = await client.options( |
| 471 |
"/resource", |
| 472 |
headers={ |
| 473 |
hdrs.ORIGIN: "http://client2.example.org", |
| 474 |
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "GET", |
| 475 |
}, |
| 476 |
) |
| 477 |
assert resp.status == 403 |
| 478 |
resp_text = await resp.text() |
| 479 |
assert "origin 'http://client2.example.org' is not allowed" in resp_text |
| 480 |
|
| 481 |
for header_name in { |
| 482 |
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN, |
| 483 |
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS, |
| 484 |
hdrs.ACCESS_CONTROL_MAX_AGE, |
| 485 |
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS, |
| 486 |
hdrs.ACCESS_CONTROL_ALLOW_METHODS, |
| 487 |
hdrs.ACCESS_CONTROL_ALLOW_HEADERS, |
| 488 |
}: |
| 489 |
assert header_name not in resp.headers |
| 490 |
|
| 491 |
|
| 492 |
async def test_preflight_default_disallowed_method(aiohttp_client, make_app): |
| 493 |
|
| 494 |
app = make_app(None, {"http://client1.example.org": ResourceOptions()}) |
| 495 |
|
| 496 |
client = await aiohttp_client(app) |
| 497 |
|
| 498 |
resp = await client.options( |
| 499 |
"/resource", |
| 500 |
headers={ |
| 501 |
hdrs.ORIGIN: "http://client1.example.org", |
| 502 |
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "POST", |
| 503 |
}, |
| 504 |
) |
| 505 |
assert resp.status == 403 |
| 506 |
resp_text = await resp.text() |
| 507 |
assert ( |
| 508 |
"request method 'POST' is not allowed for " |
| 509 |
"'http://client1.example.org' origin" in resp_text |
| 510 |
) |
| 511 |
|
| 512 |
for header_name in { |
| 513 |
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN, |
| 514 |
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS, |
| 515 |
hdrs.ACCESS_CONTROL_MAX_AGE, |
| 516 |
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS, |
| 517 |
hdrs.ACCESS_CONTROL_ALLOW_METHODS, |
| 518 |
hdrs.ACCESS_CONTROL_ALLOW_HEADERS, |
| 519 |
}: |
| 520 |
assert header_name not in resp.headers |
| 521 |
|
| 522 |
|
| 523 |
async def test_preflight_req_multiple_routes_with_one_options(aiohttp_client): |
| 524 |
"""Test CORS preflight handling on resource that is available through |
| 525 |
several routes. |
| 526 |
""" |
| 527 |
app = web.Application() |
| 528 |
cors = _setup( |
| 529 |
app, |
| 530 |
defaults={ |
| 531 |
"*": ResourceOptions( |
| 532 |
allow_credentials=True, |
| 533 |
expose_headers="*", |
| 534 |
allow_headers="*", |
| 535 |
) |
| 536 |
}, |
| 537 |
) |
| 538 |
|
| 539 |
cors.add(app.router.add_route("GET", "/{name}", handler)) |
| 540 |
cors.add(app.router.add_route("PUT", "/{name}", handler)) |
| 541 |
|
| 542 |
client = await aiohttp_client(app) |
| 543 |
|
| 544 |
resp = await client.options( |
| 545 |
"/user", |
| 546 |
headers={ |
| 547 |
hdrs.ORIGIN: "http://example.org", |
| 548 |
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "PUT", |
| 549 |
}, |
| 550 |
) |
| 551 |
assert resp.status == 200 |
| 552 |
|
| 553 |
data = await resp.text() |
| 554 |
assert data == "" |
| 555 |
|
| 556 |
|
| 557 |
async def test_preflight_request_mult_routes_with_one_options_resource(aiohttp_client): |
| 558 |
"""Test CORS preflight handling on resource that is available through |
| 559 |
several routes. |
| 560 |
""" |
| 561 |
app = web.Application() |
| 562 |
cors = _setup( |
| 563 |
app, |
| 564 |
defaults={ |
| 565 |
"*": ResourceOptions( |
| 566 |
allow_credentials=True, |
| 567 |
expose_headers="*", |
| 568 |
allow_headers="*", |
| 569 |
) |
| 570 |
}, |
| 571 |
) |
| 572 |
|
| 573 |
resource = cors.add(app.router.add_resource("/{name}")) |
| 574 |
cors.add(resource.add_route("GET", handler)) |
| 575 |
cors.add(resource.add_route("PUT", handler)) |
| 576 |
|
| 577 |
client = await aiohttp_client(app) |
| 578 |
|
| 579 |
resp = await client.options( |
| 580 |
"/user", |
| 581 |
headers={ |
| 582 |
hdrs.ORIGIN: "http://example.org", |
| 583 |
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "PUT", |
| 584 |
}, |
| 585 |
) |
| 586 |
assert resp.status == 200 |
| 587 |
|
| 588 |
data = await resp.text() |
| 589 |
assert data == "" |
| 590 |
|
| 591 |
|
| 592 |
async def test_preflight_request_max_age_resource(aiohttp_client): |
| 593 |
"""Test CORS preflight handling on resource that is available through |
| 594 |
several routes. |
| 595 |
""" |
| 596 |
app = web.Application() |
| 597 |
cors = _setup( |
| 598 |
app, |
| 599 |
defaults={ |
| 600 |
"*": ResourceOptions( |
| 601 |
allow_credentials=True, |
| 602 |
expose_headers="*", |
| 603 |
allow_headers="*", |
| 604 |
max_age=1200, |
| 605 |
) |
| 606 |
}, |
| 607 |
) |
| 608 |
|
| 609 |
resource = cors.add(app.router.add_resource("/{name}")) |
| 610 |
cors.add(resource.add_route("GET", handler)) |
| 611 |
|
| 612 |
client = await aiohttp_client(app) |
| 613 |
|
| 614 |
resp = await client.options( |
| 615 |
"/user", |
| 616 |
headers={ |
| 617 |
hdrs.ORIGIN: "http://example.org", |
| 618 |
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "GET", |
| 619 |
}, |
| 620 |
) |
| 621 |
assert resp.status == 200 |
| 622 |
assert resp.headers[hdrs.ACCESS_CONTROL_MAX_AGE].upper() == "1200" |
| 623 |
|
| 624 |
data = await resp.text() |
| 625 |
assert data == "" |
| 626 |
|
| 627 |
|
| 628 |
async def test_preflight_request_max_age_webview(aiohttp_client): |
| 629 |
"""Test CORS preflight handling on resource that is available through |
| 630 |
several routes. |
| 631 |
""" |
| 632 |
app = web.Application() |
| 633 |
cors = _setup( |
| 634 |
app, |
| 635 |
defaults={ |
| 636 |
"*": ResourceOptions( |
| 637 |
allow_credentials=True, |
| 638 |
expose_headers="*", |
| 639 |
allow_headers="*", |
| 640 |
max_age=1200, |
| 641 |
) |
| 642 |
}, |
| 643 |
) |
| 644 |
|
| 645 |
class TestView(web.View, CorsViewMixin): |
| 646 |
async def get(self): |
| 647 |
resp = web.Response(text=TEST_BODY) |
| 648 |
|
| 649 |
resp.headers[SERVER_CUSTOM_HEADER_NAME] = SERVER_CUSTOM_HEADER_VALUE |
| 650 |
|
| 651 |
return resp |
| 652 |
|
| 653 |
cors.add(app.router.add_route("*", "/{name}", TestView)) |
| 654 |
|
| 655 |
client = await aiohttp_client(app) |
| 656 |
|
| 657 |
resp = await client.options( |
| 658 |
"/user", |
| 659 |
headers={ |
| 660 |
hdrs.ORIGIN: "http://example.org", |
| 661 |
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "GET", |
| 662 |
}, |
| 663 |
) |
| 664 |
assert resp.status == 200 |
| 665 |
assert resp.headers[hdrs.ACCESS_CONTROL_MAX_AGE].upper() == "1200" |
| 666 |
|
| 667 |
data = await resp.text() |
| 668 |
assert data == "" |
| 669 |
|
| 670 |
|
| 671 |
async def test_preflight_request_mult_routes_with_one_options_webview(aiohttp_client): |
| 672 |
"""Test CORS preflight handling on resource that is available through |
| 673 |
several routes. |
| 674 |
""" |
| 675 |
app = web.Application() |
| 676 |
cors = _setup( |
| 677 |
app, |
| 678 |
defaults={ |
| 679 |
"*": ResourceOptions( |
| 680 |
allow_credentials=True, |
| 681 |
expose_headers="*", |
| 682 |
allow_headers="*", |
| 683 |
) |
| 684 |
}, |
| 685 |
) |
| 686 |
|
| 687 |
class TestView(web.View, CorsViewMixin): |
| 688 |
async def get(self): |
| 689 |
resp = web.Response(text=TEST_BODY) |
| 690 |
|
| 691 |
resp.headers[SERVER_CUSTOM_HEADER_NAME] = SERVER_CUSTOM_HEADER_VALUE |
| 692 |
|
| 693 |
return resp |
| 694 |
|
| 695 |
put = get |
| 696 |
|
| 697 |
cors.add(app.router.add_route("*", "/{name}", TestView)) |
| 698 |
|
| 699 |
client = await aiohttp_client(app) |
| 700 |
|
| 701 |
resp = await client.options( |
| 702 |
"/user", |
| 703 |
headers={ |
| 704 |
hdrs.ORIGIN: "http://example.org", |
| 705 |
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "PUT", |
| 706 |
}, |
| 707 |
) |
| 708 |
assert resp.status == 200 |
| 709 |
|
| 710 |
data = await resp.text() |
| 711 |
assert data == "" |
| 712 |
|
| 713 |
|
| 714 |
async def test_preflight_request_headers_webview(aiohttp_client): |
| 715 |
"""Test CORS preflight request handlers handling.""" |
| 716 |
app = web.Application() |
| 717 |
cors = _setup( |
| 718 |
app, |
| 719 |
defaults={ |
| 720 |
"*": ResourceOptions( |
| 721 |
allow_credentials=True, |
| 722 |
expose_headers="*", |
| 723 |
allow_headers=("Content-Type", "X-Header"), |
| 724 |
) |
| 725 |
}, |
| 726 |
) |
| 727 |
|
| 728 |
class TestView(web.View, CorsViewMixin): |
| 729 |
async def put(self): |
| 730 |
response = web.Response(text=TEST_BODY) |
| 731 |
|
| 732 |
response.headers[SERVER_CUSTOM_HEADER_NAME] = SERVER_CUSTOM_HEADER_VALUE |
| 733 |
|
| 734 |
return response |
| 735 |
|
| 736 |
cors.add(app.router.add_route("*", "/", TestView)) |
| 737 |
|
| 738 |
client = await aiohttp_client(app) |
| 739 |
|
| 740 |
resp = await client.options( |
| 741 |
"/", |
| 742 |
headers={ |
| 743 |
hdrs.ORIGIN: "http://example.org", |
| 744 |
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "PUT", |
| 745 |
hdrs.ACCESS_CONTROL_REQUEST_HEADERS: "content-type", |
| 746 |
}, |
| 747 |
) |
| 748 |
assert (await resp.text()) == "" |
| 749 |
assert resp.status == 200 |
| 750 |
|
| 751 |
|
| 752 |
assert ( |
| 753 |
resp.headers[hdrs.ACCESS_CONTROL_ALLOW_HEADERS].upper() |
| 754 |
== "content-type".upper() |
| 755 |
) |
| 756 |
|
| 757 |
resp = await client.options( |
| 758 |
"/", |
| 759 |
headers={ |
| 760 |
hdrs.ORIGIN: "http://example.org", |
| 761 |
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "PUT", |
| 762 |
hdrs.ACCESS_CONTROL_REQUEST_HEADERS: "X-Header,content-type", |
| 763 |
}, |
| 764 |
) |
| 765 |
assert resp.status == 200 |
| 766 |
|
| 767 |
|
| 768 |
assert frozenset( |
| 769 |
resp.headers[hdrs.ACCESS_CONTROL_ALLOW_HEADERS].upper().split(",") |
| 770 |
) == {"X-Header".upper(), "content-type".upper()} |
| 771 |
assert (await resp.text()) == "" |
| 772 |
|
| 773 |
resp = await client.options( |
| 774 |
"/", |
| 775 |
headers={ |
| 776 |
hdrs.ORIGIN: "http://example.org", |
| 777 |
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "PUT", |
| 778 |
hdrs.ACCESS_CONTROL_REQUEST_HEADERS: "content-type,Test", |
| 779 |
}, |
| 780 |
) |
| 781 |
assert resp.status == 403 |
| 782 |
assert hdrs.ACCESS_CONTROL_ALLOW_HEADERS not in resp.headers |
| 783 |
assert "headers are not allowed: TEST" in (await resp.text()) |
| 784 |
|
| 785 |
|
| 786 |
async def test_preflight_request_headers_resource(aiohttp_client): |
| 787 |
"""Test CORS preflight request handlers handling.""" |
| 788 |
app = web.Application() |
| 789 |
cors = _setup( |
| 790 |
app, |
| 791 |
defaults={ |
| 792 |
"*": ResourceOptions( |
| 793 |
allow_credentials=True, |
| 794 |
expose_headers="*", |
| 795 |
allow_headers=("Content-Type", "X-Header"), |
| 796 |
) |
| 797 |
}, |
| 798 |
) |
| 799 |
|
| 800 |
cors.add(app.router.add_route("PUT", "/", handler)) |
| 801 |
|
| 802 |
client = await aiohttp_client(app) |
| 803 |
|
| 804 |
resp = await client.options( |
| 805 |
"/", |
| 806 |
headers={ |
| 807 |
hdrs.ORIGIN: "http://example.org", |
| 808 |
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "PUT", |
| 809 |
hdrs.ACCESS_CONTROL_REQUEST_HEADERS: "content-type", |
| 810 |
}, |
| 811 |
) |
| 812 |
assert (await resp.text()) == "" |
| 813 |
assert resp.status == 200 |
| 814 |
|
| 815 |
|
| 816 |
assert ( |
| 817 |
resp.headers[hdrs.ACCESS_CONTROL_ALLOW_HEADERS].upper() |
| 818 |
== "content-type".upper() |
| 819 |
) |
| 820 |
|
| 821 |
resp = await client.options( |
| 822 |
"/", |
| 823 |
headers={ |
| 824 |
hdrs.ORIGIN: "http://example.org", |
| 825 |
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "PUT", |
| 826 |
hdrs.ACCESS_CONTROL_REQUEST_HEADERS: "X-Header,content-type", |
| 827 |
}, |
| 828 |
) |
| 829 |
assert resp.status == 200 |
| 830 |
|
| 831 |
|
| 832 |
assert frozenset( |
| 833 |
resp.headers[hdrs.ACCESS_CONTROL_ALLOW_HEADERS].upper().split(",") |
| 834 |
) == {"X-Header".upper(), "content-type".upper()} |
| 835 |
assert (await resp.text()) == "" |
| 836 |
|
| 837 |
resp = await client.options( |
| 838 |
"/", |
| 839 |
headers={ |
| 840 |
hdrs.ORIGIN: "http://example.org", |
| 841 |
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "PUT", |
| 842 |
hdrs.ACCESS_CONTROL_REQUEST_HEADERS: "content-type,Test", |
| 843 |
}, |
| 844 |
) |
| 845 |
assert resp.status == 403 |
| 846 |
assert hdrs.ACCESS_CONTROL_ALLOW_HEADERS not in resp.headers |
| 847 |
assert "headers are not allowed: TEST" in (await resp.text()) |
| 848 |
|
| 849 |
|
| 850 |
async def test_preflight_request_headers(aiohttp_client): |
| 851 |
"""Test CORS preflight request handlers handling.""" |
| 852 |
app = web.Application() |
| 853 |
cors = _setup( |
| 854 |
app, |
| 855 |
defaults={ |
| 856 |
"*": ResourceOptions( |
| 857 |
allow_credentials=True, |
| 858 |
expose_headers="*", |
| 859 |
allow_headers=("Content-Type", "X-Header"), |
| 860 |
) |
| 861 |
}, |
| 862 |
) |
| 863 |
|
| 864 |
resource = cors.add(app.router.add_resource("/")) |
| 865 |
cors.add(resource.add_route("PUT", handler)) |
| 866 |
|
| 867 |
client = await aiohttp_client(app) |
| 868 |
|
| 869 |
resp = await client.options( |
| 870 |
"/", |
| 871 |
headers={ |
| 872 |
hdrs.ORIGIN: "http://example.org", |
| 873 |
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "PUT", |
| 874 |
hdrs.ACCESS_CONTROL_REQUEST_HEADERS: "content-type", |
| 875 |
}, |
| 876 |
) |
| 877 |
assert (await resp.text()) == "" |
| 878 |
assert resp.status == 200 |
| 879 |
|
| 880 |
|
| 881 |
assert ( |
| 882 |
resp.headers[hdrs.ACCESS_CONTROL_ALLOW_HEADERS].upper() |
| 883 |
== "content-type".upper() |
| 884 |
) |
| 885 |
|
| 886 |
resp = await client.options( |
| 887 |
"/", |
| 888 |
headers={ |
| 889 |
hdrs.ORIGIN: "http://example.org", |
| 890 |
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "PUT", |
| 891 |
hdrs.ACCESS_CONTROL_REQUEST_HEADERS: "X-Header,content-type", |
| 892 |
}, |
| 893 |
) |
| 894 |
assert resp.status == 200 |
| 895 |
|
| 896 |
|
| 897 |
assert frozenset( |
| 898 |
resp.headers[hdrs.ACCESS_CONTROL_ALLOW_HEADERS].upper().split(",") |
| 899 |
) == {"X-Header".upper(), "content-type".upper()} |
| 900 |
assert (await resp.text()) == "" |
| 901 |
|
| 902 |
resp = await client.options( |
| 903 |
"/", |
| 904 |
headers={ |
| 905 |
hdrs.ORIGIN: "http://example.org", |
| 906 |
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "PUT", |
| 907 |
hdrs.ACCESS_CONTROL_REQUEST_HEADERS: "content-type,Test", |
| 908 |
}, |
| 909 |
) |
| 910 |
assert resp.status == 403 |
| 911 |
assert hdrs.ACCESS_CONTROL_ALLOW_HEADERS not in resp.headers |
| 912 |
assert "headers are not allowed: TEST" in (await resp.text()) |
| 913 |
|
| 914 |
|
| 915 |
async def test_static_route(aiohttp_client): |
| 916 |
"""Test a static route with CORS.""" |
| 917 |
app = web.Application() |
| 918 |
cors = _setup( |
| 919 |
app, |
| 920 |
defaults={ |
| 921 |
"*": ResourceOptions( |
| 922 |
allow_credentials=True, |
| 923 |
expose_headers="*", |
| 924 |
allow_methods="*", |
| 925 |
allow_headers=("Content-Type", "X-Header"), |
| 926 |
) |
| 927 |
}, |
| 928 |
) |
| 929 |
|
| 930 |
test_static_path = pathlib.Path(__file__).parent |
| 931 |
cors.add(app.router.add_static("/static", test_static_path, name="static")) |
| 932 |
|
| 933 |
client = await aiohttp_client(app) |
| 934 |
|
| 935 |
resp = await client.options( |
| 936 |
"/static/test_page.html", |
| 937 |
headers={ |
| 938 |
hdrs.ORIGIN: "http://example.org", |
| 939 |
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "OPTIONS", |
| 940 |
hdrs.ACCESS_CONTROL_REQUEST_HEADERS: "content-type", |
| 941 |
}, |
| 942 |
) |
| 943 |
data = await resp.text() |
| 944 |
assert resp.status == 200 |
| 945 |
assert data == "" |
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|