示例#1
0
    def test_dispatch__with_middleware(self):
        calls = []

        class Middleware(object):
            def pre_request(self, request, path_args):
                calls.append('pre_request')

            def pre_dispatch(self, request, path_args):
                calls.append('pre_dispatch')
                path_args['foo'] = 'bar'

            def post_dispatch(self, request, response):
                calls.append('post_dispatch')
                return 'eek' + response

            def post_request(self, request, response):
                calls.append('post_request')
                response['test'] = 'header'
                return response

        def callback(request, **args):
            assert args['foo'] == 'bar'
            return 'boo'

        target = containers.ApiInterfaceBase(middleware=[Middleware()])
        operation = Operation(callback)
        actual = target.dispatch(operation, MockRequest())

        assert actual.body == '"eekboo"'
        assert actual.status == 200
        assert 'test' in actual.headers
        assert calls == [
            'pre_request', 'pre_dispatch', 'post_dispatch', 'post_request'
        ]
示例#2
0
    def test_dispatch__invalid_headers(self, r, status, message):
        target = containers.ApiInterfaceBase()
        operation = Operation(mock_callback)
        actual = target.dispatch(operation, r)

        assert actual.status == status
        assert actual.body == message
示例#3
0
    def test_dispatch__error_with_debug_enabled(self):
        def callback(request):
            raise ValueError()

        target = containers.ApiInterfaceBase(debug_enabled=True)
        operation = Operation(callback)

        with pytest.raises(ValueError):
            target.dispatch(operation, MockRequest())
示例#4
0
    def test_dispatch__exceptions(self, error, status):
        def callback(request):
            raise error

        target = containers.ApiInterfaceBase()
        operation = Operation(callback)
        actual = target.dispatch(operation, MockRequest())

        assert actual.status == status
示例#5
0
    def test_dispatch__http_response(self):
        def callback(request):
            return HttpResponse("eek")

        target = containers.ApiInterfaceBase()
        operation = Operation(callback)
        actual = target.dispatch(operation, MockRequest())

        assert actual.body == 'eek'
        assert actual.status == 200
示例#6
0
    def test_dispatch__encode_error_with_debug_enabled(self):
        def callback(request):
            raise api.ImmediateHttpResponse(ValueError,
                                            HTTPStatus.NOT_MODIFIED, {})

        target = containers.ApiInterfaceBase(debug_enabled=True)
        operation = Operation(callback)

        with pytest.raises(TypeError):
            target.dispatch(operation, MockRequest())
示例#7
0
    def test_op_paths(self):
        target = containers.ApiInterfaceBase(MockResourceApi())

        actual = list(target.op_paths())

        assert actual == [
            (UrlPath.parse('/api/a/b'),
             Operation(mock_callback, 'a/b', Method.GET)),
            (UrlPath.parse('/api/a/b'),
             Operation(mock_callback, 'a/b', Method.POST)),
            (UrlPath.parse('/api/d/e'),
             Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH))),
        ]
示例#8
0
    def test_dispatch__error_handled_by_middleware_raises_exception(self):
        class ErrorMiddleware(object):
            def handle_500(self, request, exception):
                assert isinstance(exception, ValueError)
                raise ValueError

        def callback(request):
            raise ValueError()

        target = containers.ApiInterfaceBase(middleware=[ErrorMiddleware()])
        operation = Operation(callback)

        actual = target.dispatch(operation, MockRequest())
        assert actual.status == 500
示例#9
0
    def test_dispatch__error_handled_by_middleware(self):
        class ErrorMiddleware(object):
            def handle_500(self, request, exception):
                assert isinstance(exception, ValueError)
                return Error.from_status(HTTPStatus.SEE_OTHER, 0,
                                         "Quick over there...")

        def callback(request):
            raise ValueError()

        target = containers.ApiInterfaceBase(middleware=[ErrorMiddleware()])
        operation = Operation(callback)

        actual = target.dispatch(operation, MockRequest())
        assert actual.status == 303
示例#10
0
    def test_op_paths__collate_methods(self):
        target = containers.ApiInterfaceBase(MockResourceApi())

        actual = target.op_paths(collate_methods=True)

        assert actual == {
            UrlPath.parse('/api/a/b'): {
                Method.GET: Operation(mock_callback, 'a/b', Method.GET),
                Method.POST: Operation(mock_callback, 'a/b', Method.POST),
            },
            UrlPath.parse('/api/d/e'): {
                Method.POST:
                Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)),
                Method.PATCH:
                Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)),
            }
        }
示例#11
0
    def test_dispatch__with_middleware_pre_request_response(self):
        """
        Test scenario where pre-request hook returns a HTTP Response object
        """
        class Middleware(object):
            def pre_request(self, request, path_args):
                return HttpResponse('eek!', status=HTTPStatus.FORBIDDEN)

        def callback(request, **args):
            assert False, "Response should have already occurred!"

        target = containers.ApiInterfaceBase(middleware=[Middleware()])
        operation = Operation(callback)
        actual = target.dispatch(operation, MockRequest())

        assert actual.body == 'eek!'
        assert actual.status == 403
示例#12
0
 def test_init_non_absolute(self):
     with pytest.raises(ValueError):
         containers.ApiInterfaceBase(path_prefix='ab/c')
示例#13
0
    def test_options(self, options, name, debug_enabled, path_prefix):
        target = containers.ApiInterfaceBase(**options)

        assert target.name == name
        assert target.debug_enabled == debug_enabled
        assert target.path_prefix == path_prefix