Debian Codegraph @

tests/unit/test_urldispatcher_router_adapter.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 """aiohttp_cors.urldispatcher_router_adapter unit tests."""
16
17 from unittest import mock
18
19 import pytest
20
21 from aiohttp import web
22 from aiohttp_cors import ResourceOptions
23 from aiohttp_cors.urldispatcher_router_adapter import (
24 ResourcesUrlDispatcherRouterAdapter,
25 )
26
27
28 async def _handler(request):
29 return web.Response(text="Done")
30
31
32 @pytest.fixture
33 def app():
34 return web.Application()
35
36
37 @pytest.fixture
38 def adapter(app):
39 return ResourcesUrlDispatcherRouterAdapter(
40 app.router, defaults={"*": ResourceOptions()}
41 )
42
43
44 @pytest.fixture
45 def get_route(app):
46 return app.router.add_route("GET", "/get_path", _handler)
47
48
49 @pytest.fixture
50 def options_route(app):
51 return app.router.add_route("OPTIONS", "/options_path", _handler)
52
53
54 def test_add_get_route(adapter, get_route):
55 """Test configuring GET route"""
56 result = adapter.add_preflight_handler(get_route.resource, _handler)
57 assert result is None
58
59 assert len(adapter._resource_config) == 0
60 assert len(adapter._resources_with_preflight_handlers) == 1
61 assert len(adapter._preflight_routes) == 1
62
63
64 def test_add_options_route(adapter, options_route):
65 """Test configuring OPTIONS route"""
66
67 adapter.add_preflight_handler(options_route, _handler)
68
69 assert not adapter._resources_with_preflight_handlers
70 assert not adapter._preflight_routes
71
72
73 def test_get_non_preflight_request_config(adapter, get_route):
74 adapter.add_preflight_handler(get_route.resource, _handler)
75 adapter.set_config_for_routing_entity(
76 get_route.resource,
77 {
78 "http://example.org": ResourceOptions(),
79 },
80 )
81
82 adapter.add_preflight_handler(get_route, _handler)
83 adapter.set_config_for_routing_entity(
84 get_route,
85 {
86 "http://test.example.org": ResourceOptions(),
87 },
88 )
89
90 request = mock.Mock()
91
92 with mock.patch(
93 "aiohttp_cors.urldispatcher_router_adapter."
94 "ResourcesUrlDispatcherRouterAdapter."
95 "is_cors_enabled_on_request"
96 ) as is_cors_enabled_on_request, mock.patch(
97 "aiohttp_cors.urldispatcher_router_adapter."
98 "ResourcesUrlDispatcherRouterAdapter."
99 "_request_resource"
100 ) as _request_resource:
101 is_cors_enabled_on_request.return_value = True
102 _request_resource.return_value = get_route.resource
103
104 assert adapter.get_non_preflight_request_config(request) == {
105 "*": ResourceOptions(),
106 "http://example.org": ResourceOptions(),
107 }
108
109 request.method = "GET"
110
111 assert adapter.get_non_preflight_request_config(request) == {
112 "*": ResourceOptions(),
113 "http://example.org": ResourceOptions(),
114 "http://test.example.org": ResourceOptions(),
115 }