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
9fdea465
Commit
9fdea465
authored
Sep 13, 2011
by
Jacek Furmankiewicz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
automatic parsing of incoming JSON and XML
parent
cd0e0bc8
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
70 additions
and
5 deletions
+70
-5
content_types.feature
corepost/test/feature/content_types.feature
+43
-0
home_resource.py
corepost/test/home_resource.py
+9
-0
steps.py
corepost/test/steps.py
+14
-0
web.py
corepost/web.py
+4
-5
No files found.
corepost/test/feature/content_types.feature
0 → 100644
View file @
9fdea465
Using step definitions from
:
'../steps'
@content_types
Feature
:
Content types
CorePost should be able to
correctly parse/generate
JSON/XML/YAML based on content types
@json
Scenario Outline
:
Parse 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
"""
{"test":"test2"}
"""
Then
I expect HTTP code
<code>
And
I expect JSON content
"""
{"test":"test2"}
"""
Examples
:
|
method
|
code
|
|
POST
|
201
|
|
PUT
|
200
|
@xml
Scenario Outline
:
Parse incoming XML data
Given 'home_resource' is running
When as user 'None
:
None' I <method> 'http
:
//127.0.0.1
:
8080/post/xml'
with
XML
"""
<root><test>TEST</test><test2>Yo</test2></root>
"""
Then
I expect HTTP code
<code>
# ElementTree object
And
I expect content contains '<Element 'root' at'
Examples
:
|
method
|
code
|
|
POST
|
201
|
|
PUT
|
200
|
\ No newline at end of file
corepost/test/home_resource.py
View file @
9fdea465
...
...
@@ -6,6 +6,7 @@ Server tests
from
corepost.web
import
CorePost
,
route
from
corepost.enums
import
Http
from
twisted.internet
import
defer
import
json
class
HomeApp
(
CorePost
):
...
...
@@ -40,6 +41,14 @@ class HomeApp(CorePost):
def
test_delete
(
self
,
request
,
**
kwargs
):
return
"
%
s"
%
kwargs
@
route
(
"/post/json"
,(
Http
.
POST
,
Http
.
PUT
))
def
test_json
(
self
,
request
,
**
kwargs
):
return
"
%
s"
%
json
.
dumps
(
request
.
json
)
@
route
(
"/post/xml"
,(
Http
.
POST
,
Http
.
PUT
))
def
test_xml
(
self
,
request
,
**
kwargs
):
return
"
%
s"
%
request
.
xml
def
run_app_home
():
app
=
HomeApp
()
app
.
run
()
...
...
corepost/test/steps.py
View file @
9fdea465
...
...
@@ -13,6 +13,8 @@ from corepost.test.arguments import run_app_arguments
apps
=
{
'home_resource'
:
run_app_home
,
'multi_resource'
:
run_app_multi
,
'arguments'
:
run_app_arguments
}
NULL
=
'None'
def
as_dict
(
parameters
):
dict_val
=
{}
for
pair
in
parameters
.
split
(
'&'
)
:
...
...
@@ -74,6 +76,11 @@ def when_as_user_i_send_post_put_xml_json_to_url(payload,user,password,method,ur
scc
.
http_headers
[
'Content-type'
]
=
'application/json'
if
request_type
==
"JSON"
else
'text/xml'
scc
.
response
,
scc
.
content
=
h
.
request
(
url
,
method
,
payload
,
headers
=
scc
.
http_headers
)
@
When
(
"I prepare HTTP header '(.*)' = '(.*)'"
)
def
when_i_define_http_header_with_value
(
header
,
value
):
if
header
!=
NULL
:
scc
.
http_headers
[
header
]
=
value
##################################
# THEN
##################################
...
...
@@ -95,4 +102,11 @@ def then_check_http_header_matches(header,regex):
assert_true
(
re
.
search
(
regex
,
scc
.
response
[
header
.
lower
()],
re
.
X
|
re
.
I
)
!=
None
,
"the regex
%
s does not match the response
\n
%
s"
%
(
regex
,
scc
.
response
[
header
.
lower
()]))
@
Then
(
"^I expect JSON content
\
s*$"
)
def
then_i_expect_json
(
content
):
expected_json
=
json
.
loads
(
content
)
expected_json_sorted
=
json
.
dumps
(
expected_json
,
sort_keys
=
True
,
indent
=
4
)
received_json
=
json
.
loads
(
scc
.
content
)
received_json_sorted
=
json
.
dumps
(
received_json
,
sort_keys
=
True
,
indent
=
4
)
assert_equals
(
expected_json_sorted
,
received_json_sorted
,
"Expected JSON
\n
%
s
\n
*** actual ****
\n
%
s"
%
(
expected_json_sorted
,
received_json_sorted
))
corepost/web.py
View file @
9fdea465
...
...
@@ -197,7 +197,8 @@ class CorePost(Resource):
# handler for weird Twisted logic where PUT does not get form params
# see: http://twistedmatrix.com/pipermail/twisted-web/2007-March/003338.html
requestargs
=
request
.
args
if
request
.
method
==
Http
.
PUT
:
if
request
.
method
==
Http
.
PUT
and
HttpHeader
.
CONTENT_TYPE
in
request
.
received_headers
.
keys
()
\
and
request
.
received_headers
[
HttpHeader
.
CONTENT_TYPE
]
==
MediaType
.
APPLICATION_FORM_URLENCODED
:
requestargs
=
parse_qs
(
request
.
content
.
read
(),
1
)
#merge form args
...
...
@@ -242,14 +243,12 @@ class CorePost(Resource):
def
__parseRequestData
(
self
,
request
):
'''Automatically parses JSON,XML,YAML if present'''
if
HttpHeader
.
CONTENT_TYPE
in
request
.
headers
.
keys
():
type
=
request
.
headers
[
"content-type"
]
if
HttpHeader
.
CONTENT_TYPE
in
request
.
received_
headers
.
keys
():
type
=
request
.
received_
headers
[
"content-type"
]
if
type
==
MediaType
.
APPLICATION_JSON
:
request
.
json
=
json
.
loads
(
request
.
content
.
read
())
pass
elif
type
in
(
MediaType
.
APPLICATION_XML
,
MediaType
.
TEXT_XML
):
request
.
xml
=
ElementTree
.
XML
(
request
.
content
.
read
())
pass
elif
type
==
MediaType
.
TEXT_YAML
:
#TODO: parse YAML
pass
...
...
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