def test_preflight_view_uses_cors_decorator(self, pyramid_config): def view(request): pass # noop cors_policy = policy() pyramid_config.add_route('api.read_thing', '/api/thing') pyramid_config.add_view = mock.Mock() add_preflight_view(pyramid_config, 'api.read_thing', cors_policy) (_, kwargs) = pyramid_config.add_view.call_args assert kwargs['decorator'] == cors_policy
def test_it_adds_one_preflight_view_per_route(self, pyramid_config): cors_policy = policy() pyramid_config.add_route('api.read_thing', '/api/thing') pyramid_config.add_view = mock.Mock() def view(request): pass # noop add_preflight_view(pyramid_config, 'api.read_thing', cors_policy) add_preflight_view(pyramid_config, 'api.read_thing', cors_policy) assert pyramid_config.add_view.call_count == 1
def add_api_view(config, view, link_name=None, description=None, enable_preflight=True, **settings): """ Add a view configuration for an API view. This adds a new view using `config.add_view` with appropriate defaults for API methods (JSON in & out, CORS support). Additionally if `link_name` is specified it adds the view to the list of views returned by the `api.index` route. :param config: The Pyramid `Configurator` :param view: The view callable :param link_name: Dotted path of the metadata for this route in the output of the `api.index` view :param description: Description of the view to use in the `api.index` view :param enable_preflight: If `True` add support for CORS preflight requests for this view. If `True`, a `route_name` must be specified. :param settings: Arguments to pass on to `config.add_view` """ # Get the HTTP method for use in the API links metadata primary_method = settings.get("request_method", "GET") if isinstance(primary_method, tuple): # If the view matches multiple methods, assume the first one is # preferred primary_method = primary_method[0] settings.setdefault("accept", "application/json") settings.setdefault("renderer", "json") settings.setdefault("decorator", cors_policy) if link_name: link = { "name": link_name, "method": primary_method, "route_name": settings.get("route_name"), "description": description, } registry = config.registry if not hasattr(registry, "api_links"): registry.api_links = [] registry.api_links.append(link) config.add_view(view=view, **settings) if enable_preflight: cors.add_preflight_view(config, settings["route_name"], cors_policy)
def test_it_adds_preflight_view(self, pyramid_config): def view(request): pass # noop cors_policy = policy() pyramid_config.add_route('api.read_thing', '/api/thing') add_preflight_view(pyramid_config, 'api.read_thing', cors_policy) app = pyramid_config.make_wsgi_app() headers = {'Origin': 'https://custom-client.herokuapp.com', 'Access-Control-Request-Method': 'POST'} request = Request.blank('/api/thing', method='OPTIONS', headers=headers) resp = request.get_response(app) assert resp.status_code == 200 assert resp.body == b''
def test_it_adds_preflight_view(self, pyramid_config): def view(request): pass # noop cors_policy = policy() pyramid_config.add_route('api.read_thing', '/api/thing') add_preflight_view(pyramid_config, 'api.read_thing', cors_policy) app = pyramid_config.make_wsgi_app() headers = { 'Origin': 'https://custom-client.herokuapp.com', 'Access-Control-Request-Method': 'POST' } request = Request.blank('/api/thing', method='OPTIONS', headers=headers) resp = request.get_response(app) assert resp.status_code == 200 assert resp.body == b''
def test_it_adds_preflight_view(self, pyramid_config): def view(request): pass # noop cors_policy = policy() pyramid_config.add_route("api.read_thing", "/api/thing") add_preflight_view(pyramid_config, "api.read_thing", cors_policy) app = pyramid_config.make_wsgi_app() headers = { "Origin": "https://custom-client.herokuapp.com", "Access-Control-Request-Method": "POST", } request = Request.blank("/api/thing", method="OPTIONS", headers=headers) resp = request.get_response(app) assert resp.status_code == 200 assert resp.body == b""