Skip to content

Commit

Permalink
Merge pull request #73 from Tarsbot/master
Browse files Browse the repository at this point in the history
if output is empty, auto return the error 404.
  • Loading branch information
hfaran committed May 22, 2015
2 parents 973640b + 87c3956 commit 23ea981
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
53 changes: 53 additions & 0 deletions tests/func_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,43 @@ def post(self):
return "Fission mailed."


class NotFoundHandler(requesthandlers.APIHandler):

@schema.validate(**{
"output_schema": {
"type": "number",
},
"on_empty_404": True
})
def get(self):
"""This handler is used for testing empty output."""
return 0

@schema.validate(**{
"input_schema": {
"type": "number",
},
"output_schema": {
"type": "object",
"properties": {
"name": {"type": "string"}
},
"required": ["name", ]
},
"on_empty_404": False
})
def post(self):
"""This handler is used for testing empty json output."""
return {}


class APIFunctionalTest(AsyncHTTPTestCase):

def get_app(self):
rts = routes.get_routes(helloworld)
rts += [
("/api/explodinghandler", ExplodingHandler),
("/api/notfoundhandler", NotFoundHandler),
("/views/someview", DummyView),
("/api/dbtest", DBTestHandler)
]
Expand Down Expand Up @@ -149,6 +180,28 @@ def test_write_error(self):
"fail"
)

def test_empty_resource(self):
# Test empty output
r = self.fetch(
"/api/notfoundhandler"
)
self.assertEqual(r.code, 404)
self.assertEqual(
jl(r.body)["status"],
"fail"
)
# Test empty output on_empty_404 is False
r = self.fetch(
"/api/notfoundhandler",
method="POST",
body="1"
)
self.assertEqual(r.code, 500)
self.assertEqual(
jl(r.body)["status"],
"error"
)

def test_view_db_conn(self):
r = self.fetch(
"/views/someview",
Expand Down
14 changes: 13 additions & 1 deletion tornado_json/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

import jsonschema
import tornado.gen

from tornado_json.exceptions import APIError

try:
from tornado.concurrent import is_future
except ImportError:
Expand All @@ -15,10 +18,13 @@

def validate(input_schema=None, output_schema=None,
input_example=None, output_example=None,
format_checker=None):
format_checker=None, on_empty_404=False):
"""Parameterized decorator for schema validation
:type format_checker: jsonschema.FormatChecker or None
:type on_empty_404: bool
:param on_empty_404: If this is set, and the result from the
decorated method is a falsy value, a 404 will be raised.
"""
@container
def _validate(rh_method):
Expand All @@ -38,6 +44,8 @@ def _validate(rh_method):
or malformed
:raises TypeError: If the output is invalid as per the schema
or malformed
:raises APIError: If the output is a falsy value and
on_empty_404 is True, an HTTP 404 error is returned
"""
@wraps(rh_method)
@tornado.gen.coroutine
Expand Down Expand Up @@ -76,6 +84,10 @@ def _wrapper(self, *args, **kwargs):
if is_future(output):
output = yield output

# if output is empty, auto return the error 404.
if not output and on_empty_404:
raise APIError(404, "Resource not found.")

if output_schema is not None:
# We wrap output in an object before validating in case
# output is a string (and ergo not a validatable JSON object)
Expand Down

0 comments on commit 23ea981

Please sign in to comment.