| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
"""Test basic usage.""" |
| 16 |
|
| 17 |
|
| 18 |
async def test_main(): |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
import aiohttp_cors |
| 23 |
from aiohttp import web |
| 24 |
|
| 25 |
async def handler(request): |
| 26 |
return web.Response( |
| 27 |
text="Hello!", |
| 28 |
headers={ |
| 29 |
"X-Custom-Server-Header": "Custom data", |
| 30 |
}, |
| 31 |
) |
| 32 |
|
| 33 |
app = web.Application() |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
cors = aiohttp_cors.setup(app) |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
resource = cors.add(app.router.add_resource("/hello")) |
| 44 |
route = cors.add( |
| 45 |
resource.add_route("GET", handler), |
| 46 |
{ |
| 47 |
"http://client.example.org": aiohttp_cors.ResourceOptions( |
| 48 |
allow_credentials=True, |
| 49 |
expose_headers=("X-Custom-Server-Header",), |
| 50 |
allow_headers=("X-Requested-With", "Content-Type"), |
| 51 |
max_age=3600, |
| 52 |
) |
| 53 |
}, |
| 54 |
) |
| 55 |
|
| 56 |
assert route is not None |
| 57 |
|
| 58 |
|
| 59 |
async def test_defaults(): |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
import aiohttp_cors |
| 64 |
from aiohttp import web |
| 65 |
|
| 66 |
async def handler(request): |
| 67 |
return web.Response( |
| 68 |
text="Hello!", |
| 69 |
headers={ |
| 70 |
"X-Custom-Server-Header": "Custom data", |
| 71 |
}, |
| 72 |
) |
| 73 |
|
| 74 |
handler_post = handler |
| 75 |
handler_put = handler |
| 76 |
|
| 77 |
app = web.Application() |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
cors = aiohttp_cors.setup( |
| 82 |
app, |
| 83 |
defaults={ |
| 84 |
|
| 85 |
|
| 86 |
"http://client.example.org": aiohttp_cors.ResourceOptions(), |
| 87 |
}, |
| 88 |
) |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
hello_resource = cors.add(app.router.add_resource("/hello")) |
| 95 |
cors.add(hello_resource.add_route("POST", handler_post)) |
| 96 |
cors.add(hello_resource.add_route("PUT", handler_put)) |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
cors.add( |
| 101 |
hello_resource.add_route("GET", handler), |
| 102 |
{ |
| 103 |
"http://other-client.example.org": aiohttp_cors.ResourceOptions(), |
| 104 |
}, |
| 105 |
) |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
app.router.add_route("GET", "/private", handler) |