Merged in asalkeld/wsme-2 (pull request #15)

pecan: Make it possible to use the Response to return non-default status codes.
This commit is contained in:
Christophe de Vienne 2013-05-01 11:59:26 +02:00
commit 8753691839
3 changed files with 21 additions and 1 deletions

View File

@ -70,6 +70,10 @@ class AuthorsController(RestController):
def get(self, id): def get(self, id):
if id == 999: if id == 999:
raise wsme.exc.ClientSideError('Wrong ID') raise wsme.exc.ClientSideError('Wrong ID')
if id == 911:
return wsme.api.Response(Author(),
status_code=401)
author = Author() author = Author()
author.id = id author.id = id
author.firstname = u"aname" author.firstname = u"aname"
@ -81,7 +85,7 @@ class AuthorsController(RestController):
] ]
return author return author
@wsmeext.pecan.wsexpose(Author, body=Author) @wsmeext.pecan.wsexpose(Author, body=Author, status_code=201)
def post(self, author): def post(self, author):
author.id = 10 author.id = 10
return author return author

View File

@ -63,6 +63,7 @@ class TestWS(FunctionalTest):
'/authors', '{"firstname": "test"}', '/authors', '{"firstname": "test"}',
headers={"Content-Type": "application/json"} headers={"Content-Type": "application/json"}
) )
assert res.status_int == 201
a = json.loads(res.body) a = json.loads(res.body)
print a print a
assert a['id'] == 10 assert a['id'] == 10
@ -87,6 +88,14 @@ class TestWS(FunctionalTest):
self.assertEqual(res.status, '400 Bad Request') self.assertEqual(res.status, '400 Bad Request')
assert '<faultcode>Client</faultcode>' in res.body assert '<faultcode>Client</faultcode>' in res.body
def test_non_default_response(self):
res = self.app.get(
'/authors/911.json',
expect_errors=True
)
self.assertEqual(res.status_int, 401)
self.assertEqual(res.status, '401 Unauthorized')
def test_serversideerror(self): def test_serversideerror(self):
res = self.app.get('/divide_by_zero.json', expect_errors=True) res = self.app.get('/divide_by_zero.json', expect_errors=True)
self.assertEqual(res.status, '500 Internal Server Error') self.assertEqual(res.status, '500 Internal Server Error')

View File

@ -68,6 +68,13 @@ def wsexpose(*args, **kwargs):
if funcdef.pass_request: if funcdef.pass_request:
kwargs[funcdef.pass_request] = pecan.request kwargs[funcdef.pass_request] = pecan.request
result = f(self, *args, **kwargs) result = f(self, *args, **kwargs)
# NOTE: Support setting of status_code with default 201
pecan.response.status = funcdef.status_code
if isinstance(result, wsme.api.Response):
pecan.response.status = result.status_code
result = result.obj
except: except:
data = wsme.api.format_exception( data = wsme.api.format_exception(
sys.exc_info(), sys.exc_info(),