Skip to content

Commit

Permalink
Add CORS management with builtin plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
yohanboniface committed Sep 26, 2015
1 parent 94d8dbc commit 55a392f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
23 changes: 23 additions & 0 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 2 additions & 0 deletions utilery/config/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "*"
6 changes: 3 additions & 3 deletions utilery/plugins.py → utilery/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions utilery/plugins/builtins.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion utilery/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def serve(cls, endpoint, request, **kwargs):
raise BadRequest()
return response

def options(self):
def options(self, **kwargs):
return Response('')


Expand Down

0 comments on commit 55a392f

Please sign in to comment.