Debian Codegraph @

tests/doc/test_basic_usage.py

raw symbols SCIP
1 # Copyright 2015 Vladimir Rutsky <[email protected]>
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """Test basic usage."""
16
17
18 async def test_main():
19 # This tests corresponds to example from documentation.
20 # If you updating it, don't forget to update documentation.
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 # `aiohttp_cors.setup` returns `aiohttp_cors.CorsConfig` instance.
36 # The `cors` instance will store CORS configuration for the
37 # application.
38 cors = aiohttp_cors.setup(app)
39
40 # To enable CORS processing for specific route you need to add
41 # that route to the CORS configuration object and specify its
42 # CORS options.
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 # This tests corresponds to example from documentation.
61 # If you updating it, don't forget to update documentation.
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 # Example:
80
81 cors = aiohttp_cors.setup(
82 app,
83 defaults={
84 # Allow all to read all CORS-enabled resources from
85 # http://client.example.org.
86 "http://client.example.org": aiohttp_cors.ResourceOptions(),
87 },
88 )
89
90 # Enable CORS on routes.
91
92 # According to defaults POST and PUT will be available only to
93 # "http://client.example.org".
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 # In addition to "http://client.example.org", GET request will be
99 # allowed from "http://other-client.example.org" origin.
100 cors.add(
101 hello_resource.add_route("GET", handler),
102 {
103 "http://other-client.example.org": aiohttp_cors.ResourceOptions(),
104 },
105 )
106
107 # CORS will be enabled only on the resources added to `CorsConfig`,
108 # so following resource will be NOT CORS-enabled.
109 app.router.add_route("GET", "/private", handler)