From 55a392f25305d410d18dc1ee54e6ddf5c3eb7906 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Sat, 26 Sep 2015 18:29:26 +0200 Subject: [PATCH] Add CORS management with builtin plugin --- tests/test_plugins.py | 23 +++++++++++++++++++++ tests/test_views.py | 5 +++++ utilery/config/default.py | 2 ++ utilery/{plugins.py => plugins/__init__.py} | 6 +++--- utilery/plugins/builtins.py | 9 ++++++++ utilery/views.py | 2 +- 6 files changed, 43 insertions(+), 4 deletions(-) rename utilery/{plugins.py => plugins/__init__.py} (87%) create mode 100644 utilery/plugins/builtins.py diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 49d1a3a..6a821ae 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -66,3 +66,26 @@ def on_response(self, response, request, **kwargs): resp = client.get('/default/mylayer/0/0/0.pbf') assert resp.status_code == 200 assert resp.headers['Custom'] == 'OK' + + +def test_cors_add_cors_headers(client, fetchall): + fetchall([]) + resp = client.get('/default/mylayer/0/0/0.pbf') + assert resp.status_code == 200 + assert resp.headers['Access-Control-Allow-Origin'] == '*' + + +def test_cors_can_be_changed_in_config(client, fetchall, config): + config.CORS = 'http://mydomain.org' + fetchall([]) + resp = client.get('/default/mylayer/0/0/0.pbf') + assert resp.status_code == 200 + assert resp.headers['Access-Control-Allow-Origin'] == 'http://mydomain.org' + + +def test_cors_can_be_cancelled_in_config(client, fetchall, config): + config.CORS = False + fetchall([]) + resp = client.get('/default/mylayer/0/0/0.pbf') + assert resp.status_code == 200 + assert 'Access-Control-Allow-Origin' not in resp.headers diff --git a/tests/test_views.py b/tests/test_views.py index 9caa912..16d860e 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -17,6 +17,11 @@ def check_query(query, *args, **kwargs): assert resp.status_code == 200 +def test_options(client, fetchall): + resp = client.options('/all/0/0/0.pbf') + assert resp.status_code == 200 + + def test_unknown_layer_return_400(client): resp = client.get('/unknown/0/0/0.pbf') diff --git a/utilery/config/default.py b/utilery/config/default.py index dbabb95..81cae67 100644 --- a/utilery/config/default.py +++ b/utilery/config/default.py @@ -12,9 +12,11 @@ "http://vector.myserver.org/all/{z}/{x}/{y}.pbf" ], } +BUILTIN_PLUGINS = ['utilery.plugins.builtins.CORS'] PLUGINS = [] DEBUG = False SRID = 900913 SCALE = 1 BUFFER = 0 CLIP = False +CORS = "*" diff --git a/utilery/plugins.py b/utilery/plugins/__init__.py similarity index 87% rename from utilery/plugins.py rename to utilery/plugins/__init__.py index 1c599e5..b728ba0 100644 --- a/utilery/plugins.py +++ b/utilery/plugins/__init__.py @@ -1,5 +1,5 @@ -from .utils import import_by_path -from . import config +from utilery.utils import import_by_path +from utilery import config class Plugins(object): @@ -9,7 +9,7 @@ class Plugins(object): @classmethod def load(cls): - for path in config.PLUGINS: + for path in config.BUILTIN_PLUGINS + config.PLUGINS: cls.register_plugin(import_by_path(path)()) @classmethod diff --git a/utilery/plugins/builtins.py b/utilery/plugins/builtins.py new file mode 100644 index 0000000..742ac63 --- /dev/null +++ b/utilery/plugins/builtins.py @@ -0,0 +1,9 @@ +from utilery import config + + +class CORS(object): + + def on_response(self, response, request): + if config.CORS: + response.headers["Access-Control-Allow-Origin"] = config.CORS + response.headers["Access-Control-Allow-Headers"] = "X-Requested-With" # noqa diff --git a/utilery/views.py b/utilery/views.py index e9801f3..5e52a67 100644 --- a/utilery/views.py +++ b/utilery/views.py @@ -78,7 +78,7 @@ def serve(cls, endpoint, request, **kwargs): raise BadRequest() return response - def options(self): + def options(self, **kwargs): return Response('')