| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
"""aiohttp_cors.cors_config unit tests.""" |
| 16 |
|
| 17 |
import pytest |
| 18 |
|
| 19 |
from aiohttp import web |
| 20 |
from aiohttp_cors import CorsConfig, CorsViewMixin, ResourceOptions |
| 21 |
|
| 22 |
|
| 23 |
async def _handler(request): |
| 24 |
return web.Response(text="Done") |
| 25 |
|
| 26 |
|
| 27 |
class _View(web.View, CorsViewMixin): |
| 28 |
|
| 29 |
async def get(self): |
| 30 |
return web.Response(text="Done") |
| 31 |
|
| 32 |
|
| 33 |
@pytest.fixture |
| 34 |
def app(): |
| 35 |
return web.Application() |
| 36 |
|
| 37 |
|
| 38 |
@pytest.fixture |
| 39 |
def cors(app): |
| 40 |
return CorsConfig(app, defaults={"*": ResourceOptions()}) |
| 41 |
|
| 42 |
|
| 43 |
@pytest.fixture |
| 44 |
def get_route(app): |
| 45 |
return app.router.add_route("GET", "/get_path", _handler) |
| 46 |
|
| 47 |
|
| 48 |
@pytest.fixture |
| 49 |
def options_route(app): |
| 50 |
return app.router.add_route("OPTIONS", "/options_path", _handler) |
| 51 |
|
| 52 |
|
| 53 |
def test_add_options_route(app, cors, options_route): |
| 54 |
"""Test configuring OPTIONS route""" |
| 55 |
with pytest.raises(ValueError, match="already has OPTIONS handler"): |
| 56 |
cors.add(options_route.resource) |
| 57 |
|
| 58 |
|
| 59 |
def test_plain_named_route(app, cors): |
| 60 |
"""Test adding plain named route.""" |
| 61 |
|
| 62 |
assert len(app.router.keys()) == 0 |
| 63 |
route = app.router.add_route("GET", "/{name}", _handler, name="dynamic_named_route") |
| 64 |
assert len(app.router.keys()) == 1 |
| 65 |
cors.add(route) |
| 66 |
assert len(app.router.keys()) == 1 |
| 67 |
|
| 68 |
|
| 69 |
def test_dynamic_named_route(app, cors): |
| 70 |
"""Test adding dynamic named route.""" |
| 71 |
assert len(app.router.keys()) == 0 |
| 72 |
route = app.router.add_route("GET", "/{name}", _handler, name="dynamic_named_route") |
| 73 |
assert len(app.router.keys()) == 1 |
| 74 |
cors.add(route) |
| 75 |
assert len(app.router.keys()) == 1 |
| 76 |
|
| 77 |
|
| 78 |
def test_static_named_route(app, cors): |
| 79 |
"""Test adding dynamic named route.""" |
| 80 |
assert len(app.router.keys()) == 0 |
| 81 |
route = app.router.add_static("/file", "/", name="dynamic_named_route") |
| 82 |
assert len(app.router.keys()) == 1 |
| 83 |
cors.add(route) |
| 84 |
assert len(app.router.keys()) == 1 |
| 85 |
|
| 86 |
|
| 87 |
def test_static_resource(app, cors): |
| 88 |
"""Test adding static resource.""" |
| 89 |
assert len(app.router.keys()) == 0 |
| 90 |
app.router.add_static("/file", "/", name="dynamic_named_route") |
| 91 |
assert len(app.router.keys()) == 1 |
| 92 |
for resource in list(app.router.resources()): |
| 93 |
if isinstance(resource, web.StaticResource): |
| 94 |
cors.add(resource) |
| 95 |
assert len(app.router.keys()) == 1 |
| 96 |
|
| 97 |
|
| 98 |
def test_web_view_resource(app, cors): |
| 99 |
"""Test adding resource with web.View as handler""" |
| 100 |
assert len(app.router.keys()) == 0 |
| 101 |
route = app.router.add_route("GET", "/{name}", _View, name="dynamic_named_route") |
| 102 |
assert len(app.router.keys()) == 1 |
| 103 |
cors.add(route) |
| 104 |
assert len(app.router.keys()) == 1 |
| 105 |
|
| 106 |
|
| 107 |
def test_web_view_warning(app, cors): |
| 108 |
"""Test adding resource with web.View as handler""" |
| 109 |
route = app.router.add_route("*", "/", _View) |
| 110 |
with pytest.warns(DeprecationWarning): |
| 111 |
cors.add(route, webview=True) |
| 112 |
|
| 113 |
|
| 114 |
def test_disable_bare_view(app, cors): |
| 115 |
class View(web.View): |
| 116 |
pass |
| 117 |
|
| 118 |
route = app.router.add_route("*", "/", View) |
| 119 |
with pytest.raises(ValueError): |
| 120 |
cors.add(route) |