Commit f98bdb48 authored by Jacek Furmankiewicz's avatar Jacek Furmankiewicz

error handling for invalid JSON,XML,YAML input

parent 3fcc8133
...@@ -24,6 +24,21 @@ Feature: Content types ...@@ -24,6 +24,21 @@ Feature: Content types
| POST | 201 | | POST | 201 |
| PUT | 200 | | PUT | 200 |
@json
Scenario Outline: Handle invalid incoming JSON data
Given 'home_resource' is running
When as user 'None:None' I <method> 'http://127.0.0.1:8080/post/json' with JSON
"""
wrong_json
"""
Then I expect HTTP code 400
And I expect content contains 'Unable to parse JSON body: No JSON object could be decoded'
Examples:
| method |
| POST |
| PUT |
@xml @xml
Scenario Outline: Parse incoming XML data Scenario Outline: Parse incoming XML data
Given 'home_resource' is running Given 'home_resource' is running
...@@ -39,6 +54,22 @@ Feature: Content types ...@@ -39,6 +54,22 @@ Feature: Content types
| method | code | | method | code |
| POST | 201 | | POST | 201 |
| PUT | 200 | | PUT | 200 |
@xml
Scenario Outline: Handle invalid XML data
Given 'home_resource' is running
When as user 'None:None' I <method> 'http://127.0.0.1:8080/post/xml' with XML
"""
wrong xml
"""
Then I expect HTTP code 400
And I expect content contains 'Unable to parse XML body: syntax error: line 1, column 0'
Examples:
| method |
| POST |
| PUT |
@yaml @yaml
Scenario Outline: Parse incoming YAML data Scenario Outline: Parse incoming YAML data
...@@ -109,4 +140,20 @@ total: 4443.52 ...@@ -109,4 +140,20 @@ total: 4443.52
Examples: Examples:
| method | code | | method | code |
| POST | 201 | | POST | 201 |
| PUT | 200 | | PUT | 200 |
\ No newline at end of file
@yaml
Scenario Outline: Handle invalid YAML data
Given 'home_resource' is running
When as user 'None:None' I <method> 'http://127.0.0.1:8080/post/yaml' with YAML
"""
- test
{test}
"""
Then I expect HTTP code 400
And I expect content contains 'Unable to parse YAML body: while scanning a simple key'
Examples:
| method |
| POST |
| PUT |
\ No newline at end of file
...@@ -5,7 +5,7 @@ Common Freshen BDD steps ...@@ -5,7 +5,7 @@ Common Freshen BDD steps
''' '''
from multiprocessing import Process from multiprocessing import Process
import httplib2, json, re, time import httplib2, json, re, time
from freshen import Before, After, Given, When, Then, scc, glc, assert_equals, assert_true #@UnresolvedImport from freshen import Before, Given, When, Then, scc, glc, assert_equals, assert_true #@UnresolvedImport
from urllib import urlencode from urllib import urlencode
from corepost.test.home_resource import run_app_home from corepost.test.home_resource import run_app_home
from corepost.test.multi_resource import run_app_multi from corepost.test.multi_resource import run_app_multi
......
...@@ -208,14 +208,14 @@ class CorePost(Resource): ...@@ -208,14 +208,14 @@ class CorePost(Resource):
# maintain first instance of an argument always # maintain first instance of an argument always
if arg not in allargs: if arg not in allargs:
allargs[arg] = requestargs[arg][0] allargs[arg] = requestargs[arg][0]
# if POST/PUT, check if we need to automatically parse JSON
self.__parseRequestData(request)
#handle Deferreds natively
try: try:
# if POST/PUT, check if we need to automatically parse JSON
self.__parseRequestData(request)
val = urlrouter.call(self,request,**allargs) val = urlrouter.call(self,request,**allargs)
#handle Deferreds natively
if isinstance(val,defer.Deferred): if isinstance(val,defer.Deferred):
# we assume the method will call request.finish() # we assume the method will call request.finish()
return NOT_DONE_YET return NOT_DONE_YET
...@@ -245,14 +245,23 @@ class CorePost(Resource): ...@@ -245,14 +245,23 @@ class CorePost(Resource):
def __parseRequestData(self,request): def __parseRequestData(self,request):
'''Automatically parses JSON,XML,YAML if present''' '''Automatically parses JSON,XML,YAML if present'''
if HttpHeader.CONTENT_TYPE in request.received_headers.keys(): if request.method in (Http.POST,Http.PUT) and HttpHeader.CONTENT_TYPE in request.received_headers.keys():
type = request.received_headers["content-type"] type = request.received_headers["content-type"]
if type == MediaType.APPLICATION_JSON: if type == MediaType.APPLICATION_JSON:
request.json = json.loads(request.content.read()) try:
request.json = json.loads(request.content.read())
except Exception as ex:
raise TypeError("Unable to parse JSON body: %s" % ex)
elif type in (MediaType.APPLICATION_XML,MediaType.TEXT_XML): elif type in (MediaType.APPLICATION_XML,MediaType.TEXT_XML):
request.xml = ElementTree.XML(request.content.read()) try:
request.xml = ElementTree.XML(request.content.read())
except Exception as ex:
raise TypeError("Unable to parse XML body: %s" % ex)
elif type == MediaType.TEXT_YAML: elif type == MediaType.TEXT_YAML:
request.yaml = yaml.safe_load(request.content.read()) try:
request.yaml = yaml.safe_load(request.content.read())
except Exception as ex:
raise TypeError("Unable to parse YAML body: %s" % ex)
def run(self,port=8080): def run(self,port=8080):
"""Shortcut for running app within Twisted reactor""" """Shortcut for running app within Twisted reactor"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment