diff --git a/tests/test_flask.py b/tests/test_flask.py index abdcd10..789908c 100644 --- a/tests/test_flask.py +++ b/tests/test_flask.py @@ -1,5 +1,8 @@ +# encoding=utf8 import unittest from flask import Flask, json, abort +from flask.ext import restful + from wsmeext.flask import signature from wsme.api import Response from wsme.types import Base, text @@ -16,6 +19,7 @@ class Criterion(Base): value = text test_app = Flask(__name__) +api = restful.Api(test_app) @test_app.route('/multiply') @@ -78,6 +82,18 @@ def get_status_response(): return Response(1, status_code=201) +class RestFullApi(restful.Resource): + @signature(Model) + def get(self): + return Model(id=1, name=u"Gérard") + + @signature(int, body=Model) + def post(self, model): + return model.id + +api.add_resource(RestFullApi, '/restful') + + class FlaskrTestCase(unittest.TestCase): def setUp(self): @@ -170,5 +186,27 @@ class FlaskrTestCase(unittest.TestCase): "faultstring": "integer division or modulo by zero"} ) + def test_restful_get(self): + r = self.app.get('/restful', headers={'Accept': 'application/json'}) + self.assertEqual(r.status_code, 200) + + data = json.loads(r.data) + + self.assertEqual(data['id'], 1) + self.assertEqual(data['name'], u"Gérard") + + def test_restful_post(self): + r = self.app.post( + '/restful', + data=json.dumps({'id': 5, 'name': u'Huguette'}), + headers={ + 'Accept': 'application/json', + 'Content-Type': 'application/json'}) + self.assertEqual(r.status_code, 200) + + data = json.loads(r.data) + + self.assertEqual(data, 5) + if __name__ == '__main__': test_app.run() diff --git a/tox-tmpl.ini b/tox-tmpl.ini index 1bb97cc..2b1940f 100644 --- a/tox-tmpl.ini +++ b/tox-tmpl.ini @@ -14,6 +14,7 @@ basedeps= cloud_sptheme Sphinx < 1.2.99 Flask + flask-restful [axes] python=py26,py27,py32,py33,pypy diff --git a/tox.ini b/tox.ini index 9e50994..1b75333 100644 --- a/tox.ini +++ b/tox.ini @@ -13,6 +13,7 @@ basedeps = cloud_sptheme Sphinx < 1.2.99 Flask + flask-restful [testenv] setenv = diff --git a/wsmeext/flask.py b/wsmeext/flask.py index b497698..a8372bd 100644 --- a/wsmeext/flask.py +++ b/wsmeext/flask.py @@ -3,6 +3,7 @@ from __future__ import absolute_import import functools import logging import sys +import inspect import wsme import wsme.api @@ -43,12 +44,16 @@ def signature(*args, **kw): sig = wsme.signature(*args, **kw) def decorator(f): + args = inspect.getargspec(f)[0] + ismethod = args and args[0] == 'self' sig(f) funcdef = wsme.api.FunctionDefinition.get(f) funcdef.resolve_types(wsme.types.registry) @functools.wraps(f) def wrapper(*args, **kwargs): + if ismethod: + self, args = args[0], args[1:] args, kwargs = wsme.rest.args.get_args( funcdef, args, kwargs, flask.request.args, flask.request.form, @@ -62,6 +67,8 @@ def signature(*args, **kw): dataformat = get_dataformat() try: + if ismethod: + args = [self] + list(args) result = f(*args, **kwargs) # NOTE: Support setting of status_code with default 20