Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
corepost
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
nexlab
corepost
Commits
f98bdb48
Commit
f98bdb48
authored
Sep 15, 2011
by
Jacek Furmankiewicz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
error handling for invalid JSON,XML,YAML input
parent
3fcc8133
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
67 additions
and
11 deletions
+67
-11
content_types.feature
corepost/test/feature/content_types.feature
+48
-1
steps.py
corepost/test/steps.py
+1
-1
web.py
corepost/web.py
+18
-9
No files found.
corepost/test/feature/content_types.feature
View file @
f98bdb48
...
...
@@ -24,6 +24,21 @@ Feature: Content types
|
POST
|
201
|
|
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
Scenario Outline
:
Parse incoming XML data
Given 'home_resource' is running
...
...
@@ -40,6 +55,22 @@ Feature: Content types
|
POST
|
201
|
|
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
Scenario Outline
:
Parse incoming YAML data
Given 'home_resource' is running
...
...
@@ -110,3 +141,19 @@ total: 4443.52
|
method
|
code
|
|
POST
|
201
|
|
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
corepost/test/steps.py
View file @
f98bdb48
...
...
@@ -5,7 +5,7 @@ Common Freshen BDD steps
'''
from
multiprocessing
import
Process
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
corepost.test.home_resource
import
run_app_home
from
corepost.test.multi_resource
import
run_app_multi
...
...
corepost/web.py
View file @
f98bdb48
...
...
@@ -209,13 +209,13 @@ class CorePost(Resource):
if
arg
not
in
allargs
:
allargs
[
arg
]
=
requestargs
[
arg
][
0
]
try
:
# if POST/PUT, check if we need to automatically parse JSON
self
.
__parseRequestData
(
request
)
#handle Deferreds natively
try
:
val
=
urlrouter
.
call
(
self
,
request
,
**
allargs
)
#handle Deferreds natively
if
isinstance
(
val
,
defer
.
Deferred
):
# we assume the method will call request.finish()
return
NOT_DONE_YET
...
...
@@ -245,14 +245,23 @@ class CorePost(Resource):
def
__parseRequestData
(
self
,
request
):
'''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"
]
if
type
==
MediaType
.
APPLICATION_JSON
:
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
):
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
:
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
):
"""Shortcut for running app within Twisted reactor"""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment