Ejemplo n.º 1
0
    def test_url_arg(self): # pylint: disable=invalid-name

        @action(method='GET', urls='/my_action/{a}', spec='''\
action my_action
  input
    int a
    int b
  output
    int sum
''')
        def my_action(unused_app, req):
            self.assertEqual(req['a'], 5)
            return {'sum': req['a'] + req['b']}

        app = Application()
        app.add_request(my_action)

        environ = {'wsgi.errors': StringIO()}
        status, headers, response = app.request('GET', '/my_action/5', query_string='b=7', environ=environ)
        self.assertEqual(status, '200 OK')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{"sum":12}')
        self.assertEqual(environ['wsgi.errors'].getvalue(), '')

        environ = {'wsgi.errors': StringIO()}
        status, headers, response = app.request('GET', '/my_action/5', query_string='a=3&b=7', environ=environ)
        self.assertEqual(status, '400 Bad Request')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{"error":"InvalidInput","message":"Duplicate URL argument member \'a\'"}')
        self.assertRegex(
            environ['wsgi.errors'].getvalue(),
            r"WARNING \[\d+ / \d+\] Duplicate URL argument member 'a' for action 'my_action'"
        )
Ejemplo n.º 2
0
    def test_request_url_mix(self):

        @action(urls=[(None, '/action/1')],
                spec='''\
action my_action1
  output
    int number
''')
        def my_action1(dummy_ctx, dummy_req):
            return {'number': 1}

        @action(urls=[('GET', '/action/{dummy_number}')],
                spec='''\
action my_action2
  input
    int dummy_number
  output
    int number
''')
        def my_action2(dummy_ctx, dummy_req):
            return {'number': 2}

        app = Application()
        app.add_request(my_action1)
        app.add_request(my_action2)

        status, dummy_headers, response = app.request('GET', '/action/1')
        self.assertEqual(status, '200 OK')
        self.assertEqual(response, b'{"number":2}')

        status, dummy_headers, response = app.request('POST', '/action/1', wsgi_input=b'{}')
        self.assertEqual(status, '200 OK')
        self.assertEqual(response, b'{"number":1}')
Ejemplo n.º 3
0
    def test_log_format_callable(self):

        def my_wsgi(environ, start_response):
            ctx = environ[Environ.CTX]
            ctx.log.warning('Hello log')
            start_response(HTTPStatus.OK, [('Content-Type', 'text/plain')])
            return ['Hello'.encode('utf-8')]

        class MyFormatter:

            def __init__(self, ctx):
                assert ctx is not None

            @staticmethod
            def format(record):
                return record.getMessage()

            @staticmethod
            def formatTime(record, unused_datefmt=None): # pylint: disable=invalid-name
                return record.getMessage()

            @staticmethod
            def formatException(unused_exc_info): # pylint: disable=invalid-name
                return 'Bad'

        app = Application()
        app.add_request(Request(my_wsgi))
        app.log_format = MyFormatter

        environ = {'wsgi.errors': StringIO()}
        status, headers, response = app.request('GET', '/my_wsgi', environ=environ)
        self.assertEqual(response, 'Hello'.encode('utf-8'))
        self.assertEqual(status, '200 OK')
        self.assertTrue(('Content-Type', 'text/plain') in headers)
        self.assertEqual(environ['wsgi.errors'].getvalue(), 'Hello log\n')
Ejemplo n.º 4
0
    def __init__(self, index):
        Application.__init__(self)
        self.log_level = logging.INFO
        self.index = index

        # Add requests
        self.add_request(DocAction())
        self.add_request(pypi_index)
        self.add_request(pypi_download)
        self.add_request(pypi_upload)
Ejemplo n.º 5
0
    def test_request_exception(self):

        def request1(unused_environ, unused_start_response):
            raise Exception('')

        app = Application()
        app.add_request(Request(request1))

        status, headers, response = app.request('GET', '/request1')
        self.assertEqual(status, '500 Internal Server Error')
        self.assertTrue(('Content-Type', 'text/plain') in headers)
        self.assertEqual(response, b'Internal Server Error')
Ejemplo n.º 6
0
    def test_request_url_method(self):

        @action(urls=[('GET', '/my_action'), ('POST', '/my_action/')],
                spec='''\
action my_action
''')
        def my_action(dummy_ctx, dummy_req):
            pass

        app = Application()
        app.add_request(my_action)

        status, dummy_headers, response = app.request('GET', '/my_action')
        self.assertEqual(status, '200 OK')
        self.assertEqual(response, b'{}')

        status, dummy_headers, response = app.request('POST', '/my_action/', wsgi_input=b'{}')
        self.assertEqual(status, '200 OK')
        self.assertEqual(response, b'{}')

        status, dummy_headers, response = app.request('GET', '/my_action/')
        self.assertEqual(status, '405 Method Not Allowed')
        self.assertEqual(response, b'Method Not Allowed')

        status, dummy_headers, response = app.request('POST', '/my_action', wsgi_input=b'{}')
        self.assertEqual(status, '405 Method Not Allowed')
        self.assertEqual(response, b'Method Not Allowed')

        status, dummy_headers, response = app.request('PUT', '/my_action', wsgi_input=b'{}')
        self.assertEqual(status, '405 Method Not Allowed')
        self.assertEqual(response, b'Method Not Allowed')
Ejemplo n.º 7
0
    def test_request_string_response(self):

        def string_response(environ, unused_start_response):
            ctx = environ[Environ.CTX]
            return ctx.response(HTTPStatus.OK, 'text/plain', 'Hello World')

        app = Application()
        app.add_request(Request(string_response))

        environ = {'wsgi.errors': StringIO()}
        status, headers, response = app.request('GET', '/string_response', environ=environ)
        self.assertEqual(status, '500 Internal Server Error')
        self.assertListEqual(headers, [('Content-Type', 'text/plain')])
        self.assertEqual(response, b'Internal Server Error')
        self.assertIn('response content cannot be of type str or bytes', environ['wsgi.errors'].getvalue())
Ejemplo n.º 8
0
    def test_error_none_output(self):

        @action(spec='''\
action my_action
''')
        def my_action(unused_app, unused_req):
            pass

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('POST', '/my_action', wsgi_input=b'{}')
        self.assertEqual(status, '200 OK')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{}')
Ejemplo n.º 9
0
    def test_error_unexpected_custom(self):

        @action(wsgi_response=True, spec='''\
action my_action
''')
        def my_action(unused_app, unused_req):
            raise Exception('FAIL')

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('POST', '/my_action', wsgi_input=b'{}')
        self.assertEqual(status, '500 Internal Server Error')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{"error":"UnexpectedError"}')
Ejemplo n.º 10
0
    def test_decorator_unknown_action(self):

        @action
        def my_action(dummy_app, dummy_req):
            return {}

        self.assertTrue(isinstance(my_action, Action))
        self.assertTrue(isinstance(my_action, Request))

        app = Application()
        try:
            app.add_request(my_action)
        except AssertionError as exc:
            self.assertEqual(str(exc), "No spec defined for action 'my_action'")
        else:
            self.fail()
Ejemplo n.º 11
0
    def test_decorator_other(self):

        # Action decorator with urls, custom response callback, and validate response bool
        @action(urls=('/foo',), wsgi_response=True, spec='''\
action my_action_default
''')
        def my_action_default(ctx, unused_req):
            return ctx.response_text(HTTPStatus.OK)

        app = Application()
        app.add_request(my_action_default)
        self.assertEqual(my_action_default.name, 'my_action_default')
        self.assertEqual(my_action_default.urls, (('POST', '/foo'),))
        self.assertTrue(isinstance(my_action_default.model, ActionModel))
        self.assertEqual(my_action_default.model.name, 'my_action_default')
        self.assertEqual(my_action_default.wsgi_response, True)
Ejemplo n.º 12
0
    def test_error_raised(self):

        @action(spec='''\
action my_action
  errors
    MyError
''')
        def my_action(unused_app, unused_req):
            raise ActionError('MyError')

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('POST', '/my_action', wsgi_input=b'{}')
        self.assertEqual(status, '400 Bad Request')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{"error":"MyError"}')
Ejemplo n.º 13
0
    def test_decorator_other(self):

        # Action decorator with urls, custom response callback, and validate response bool
        @action(urls=('/foo',), wsgi_response=True)
        def my_action_default(app, dummy_req):
            return app.response_text('200 OK', 'OK')

        app = Application()
        app.specs.parse_string('''\
action my_action_default
''')
        app.add_request(my_action_default)
        self.assertEqual(my_action_default.name, 'my_action_default')
        self.assertEqual(my_action_default.urls, (('GET', '/foo'), ('POST', '/foo')))
        self.assertTrue(isinstance(my_action_default.model, ActionModel))
        self.assertEqual(my_action_default.model.name, 'my_action_default')
        self.assertEqual(my_action_default.wsgi_response, True)
Ejemplo n.º 14
0
    def test_error_invalid_query_string(self):

        @action(method='GET', spec='''\
action my_action
  input
    int a
''')
        def my_action(unused_app, unused_req):
            return {}

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('GET', '/my_action', query_string='a')
        self.assertEqual(status, '400 Bad Request')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{"error":"InvalidInput","message":"Invalid key/value pair \'a\'"}')
Ejemplo n.º 15
0
    def test_headers(self):

        @action(spec='''\
action my_action
''')
        def my_action(ctx, unused_req):
            ctx.add_header('MyHeader', 'MyInitialValue')
            ctx.add_header('MyHeader', 'MyValue')
            return {}

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('POST', '/my_action')
        self.assertEqual(status, '200 OK')
        self.assertEqual(headers, [('Content-Type', 'application/json'), ('MyHeader', 'MyValue')])
        self.assertEqual(response.decode('utf-8'), '{}')
Ejemplo n.º 16
0
    def test_error_raise_builtin(self):

        @action(spec='''\
action my_action
''')
        def my_action(unused_app, unused_req):
            raise ActionError('UnexpectedError', status=HTTPStatus.INTERNAL_SERVER_ERROR)

        app = Application()
        app.add_request(my_action)

        environ = {'wsgi.errors': StringIO()}
        status, headers, response = app.request('POST', '/my_action', wsgi_input=b'{}', environ=environ)
        self.assertEqual(status, '500 Internal Server Error')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{"error":"UnexpectedError"}')
        self.assertEqual(environ['wsgi.errors'].getvalue(), '')
Ejemplo n.º 17
0
    def test_error_raised_status(self):

        @action(spec='''\
action my_action
  errors
    MyError
''')
        def my_action(unused_app, unused_req):
            raise ActionError('MyError', message='My message', status=HTTPStatus.NOT_FOUND)

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('POST', '/my_action', wsgi_input=b'{}')
        self.assertEqual(status, '404 Not Found')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{"error":"MyError","message":"My message"}')
Ejemplo n.º 18
0
    def test_error_array_output(self):

        @action(spec='''\
action my_action
''')
        def my_action(unused_app, unused_req):
            return []

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('POST', '/my_action', wsgi_input=b'{}')
        self.assertEqual(status, '500 Internal Server Error')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'),
                         '{"error":"InvalidOutput","message":"Invalid value [] (type \'list\'), '
                         'expected type \'my_action_output\'"}')
Ejemplo n.º 19
0
    def test_error_response(self):

        @action(spec='''\
action my_action
  errors
    MyError
''')
        def my_action(unused_app, unused_req):
            return {'error': 'MyError'}

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('POST', '/my_action', wsgi_input=b'{}')
        self.assertEqual(status, '500 Internal Server Error')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{"error":"InvalidOutput","message":"Unknown member \'error\'"}')
Ejemplo n.º 20
0
    def test_error_invalid_method(self):

        @action(spec='''\
action my_action
  input
    int a
''')
        def my_action(unused_app, unused_req):
            return {}

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('FOO', '/my_action', wsgi_input=b'{"a": 7}')
        self.assertEqual(status, '405 Method Not Allowed')
        self.assertEqual(sorted(headers), [('Content-Type', 'text/plain')])
        self.assertEqual(response.decode('utf-8'), 'Method Not Allowed')
Ejemplo n.º 21
0
    def test_request_args(self):

        def request1(environ, unused_start_response):
            ctx = environ[Environ.CTX]
            self.assertEqual(environ['QUERY_STRING'], 'a=1&b=2')
            self.assertEqual(environ['wsgi.input'].read(), b'hello')
            ctx.log.warning('in request1')
            return ctx.response_text(HTTPStatus.OK, 'request1')

        app = Application()
        app.add_request(Request(request1))

        environ = {'wsgi.errors': StringIO()}
        status, _, response = app.request('GET', '/request1', query_string='a=1&b=2', wsgi_input=b'hello', environ=environ)
        self.assertEqual(status, '200 OK')
        self.assertEqual(response, b'request1')
        self.assertIn('in request1', environ['wsgi.errors'].getvalue())
Ejemplo n.º 22
0
    def setUp(self):

        # Application object
        self.app = Application()
        self.app.pretty_output = True
        self.app.specs.parse_string(self._spec)
        self.app.add_request(Action(lambda app, req: {}, name='my_action1'))
        self.app.add_request(Action(lambda app, req: {}, name='my_action2'))
        self.app.add_request(DocAction())
Ejemplo n.º 23
0
    def test_error_raised_message(self):

        @action(spec='''\
action my_action
  errors
    MyError
''')
        def my_action(dummy_app, dummy_req):
            raise ActionError('MyError', 'My message')

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('POST', '/my_action', wsgi_input=b'{}')
        self.assertEqual(status, '500 Internal Server Error')
        self.assertEqual(sorted(headers), [('Content-Length', '42'),
                                           ('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{"error":"MyError","message":"My message"}')
Ejemplo n.º 24
0
    def test_request_nested(self):

        def request1(environ, unused_start_response):
            ctx = environ[Environ.CTX]
            return ctx.response_text(HTTPStatus.OK, '7')

        def request2(environ, unused_start_response):
            ctx = environ[Environ.CTX]
            unused_status, _, response = ctx.app.request('GET', '/request1')
            return ctx.response_text(HTTPStatus.OK, str(5 + int(response)))

        app = Application()
        app.add_request(Request(request1))
        app.add_request(Request(request2))

        status, _, response = app.request('GET', '/request2')
        self.assertEqual(status, '200 OK')
        self.assertEqual(response, b'12')
Ejemplo n.º 25
0
    def test_decorator_spec(self):

        @action(spec='''\
action my_action
''')
        def my_action(unused_app, unused_req):
            return {}

        self.assertTrue(isinstance(my_action, Action))
        self.assertTrue(isinstance(my_action, Request))

        app = Application()
        app.add_request(my_action)
        self.assertEqual(my_action.name, 'my_action')
        self.assertEqual(my_action.urls, (('POST', '/my_action'),))
        self.assertTrue(isinstance(my_action.model, ActionModel))
        self.assertEqual(my_action.model.name, 'my_action')
        self.assertEqual(my_action.wsgi_response, False)
Ejemplo n.º 26
0
    def test_nested_requests(self):

        def request1(environ, dummy_start_response):
            ctx = environ[ENVIRON_CTX]
            return ctx.response_text('200 OK', '7')

        def request2(environ, dummy_start_response):
            ctx = environ[ENVIRON_CTX]
            dummy_status, dummy_headers, response = ctx.app.request('GET', '/request1')
            return ctx.response_text('200 OK', str(5 + int(response)))

        app = Application()
        app.add_request(Request(request1))
        app.add_request(Request(request2))

        status, dummy_headers, response = app.request('GET', '/request2')
        self.assertEqual(status, '200 OK')
        self.assertEqual(response, b'12')
Ejemplo n.º 27
0
    def test_custom_response(self):

        @action(wsgi_response=True, spec='''\
action my_action
  input
    string a
  output
    string b
''')
        def my_action(ctx, req):
            return ctx.response_text(HTTPStatus.OK, 'Hello ' + str(req['a'].upper()))

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('POST', '/my_action', wsgi_input=b'{"a": "world"}')
        self.assertEqual(status, '200 OK')
        self.assertEqual(sorted(headers), [('Content-Type', 'text/plain')])
        self.assertEqual(response.decode('utf-8'), 'Hello WORLD')
Ejemplo n.º 28
0
    def test_request_head(self):

        def request(environ, unused_start_response):
            assert environ['REQUEST_METHOD'] == 'GET'
            ctx = environ[Environ.CTX]
            return ctx.response_text(HTTPStatus.OK, 'the response')

        app = Application()
        app.add_request(Request(request, method='GET'))

        status, headers, response = app.request('GET', '/request')
        self.assertEqual(status, '200 OK')
        self.assertEqual(response, b'the response')
        self.assertListEqual(headers, [('Content-Type', 'text/plain')])

        status, headers, response = app.request('HEAD', '/request')
        self.assertEqual(status, '200 OK')
        self.assertEqual(response, b'')
        self.assertListEqual(headers, [('Content-Type', 'text/plain')])
Ejemplo n.º 29
0
    def test_error_invalid_input(self):

        @action(spec='''\
action my_action
  input
    string a
''')
        def my_action(unused_app, unused_req):
            return {}

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('POST', '/my_action', wsgi_input=b'{"a": 7}')
        self.assertEqual(status, '400 Bad Request')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'),
                         '{"error":"InvalidInput","member":"a","message":"Invalid value 7 (type \'int\') '
                         'for member \'a\', expected type \'string\'"}')
Ejemplo n.º 30
0
    def test_error_invalid_output(self):

        @action(spec='''\
action my_action
  output
    int a
''')
        def my_action(unused_app, unused_req):
            return {'a': 'asdf'}

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('POST', '/my_action', wsgi_input=b'{}')
        self.assertEqual(status, '500 Internal Server Error')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'),
                         '{"error":"InvalidOutput","member":"a","message":"Invalid value \'asdf\' (type \'str\') '
                         'for member \'a\', expected type \'int\'"}')