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
...@@ -40,6 +55,22 @@ Feature: Content types ...@@ -40,6 +55,22 @@ Feature: Content types
| 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
Given 'home_resource' is running Given 'home_resource' is running
...@@ -110,3 +141,19 @@ total: 4443.52 ...@@ -110,3 +141,19 @@ total: 4443.52
| method | code | | method | code |
| POST | 201 | | POST | 201 |
| PUT | 200 | | PUT | 200 |
@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
......
...@@ -209,13 +209,13 @@ class CorePost(Resource): ...@@ -209,13 +209,13 @@ class CorePost(Resource):
if arg not in allargs: if arg not in allargs:
allargs[arg] = requestargs[arg][0] allargs[arg] = requestargs[arg][0]
try:
# if POST/PUT, check if we need to automatically parse JSON # if POST/PUT, check if we need to automatically parse JSON
self.__parseRequestData(request) self.__parseRequestData(request)
#handle Deferreds natively
try:
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:
try:
request.json = json.loads(request.content.read()) 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):
try:
request.xml = ElementTree.XML(request.content.read()) 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:
try:
request.yaml = yaml.safe_load(request.content.read()) 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