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
a7a26a6a
Commit
a7a26a6a
authored
Mar 16, 2012
by
Jacek Furmankiewicz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support for automatic parsing of JSON, YAML, XML, Form arguments, needs docs before final release
parent
f7cdbf9a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
74 additions
and
13 deletions
+74
-13
routing.py
corepost/routing.py
+20
-11
arguments.feature
corepost/test/feature/arguments.feature
+50
-2
utils.py
corepost/utils.py
+4
-0
No files found.
corepost/routing.py
View file @
a7a26a6a
...
@@ -7,7 +7,7 @@ Common routing classes, regardless of whether used in HTTP or multiprocess conte
...
@@ -7,7 +7,7 @@ Common routing classes, regardless of whether used in HTTP or multiprocess conte
from
collections
import
defaultdict
from
collections
import
defaultdict
from
corepost
import
Response
,
RESTException
from
corepost
import
Response
,
RESTException
from
corepost.enums
import
Http
,
HttpHeader
from
corepost.enums
import
Http
,
HttpHeader
from
corepost.utils
import
getMandatoryArgumentNames
,
convertToJson
from
corepost.utils
import
getMandatoryArgumentNames
,
convertToJson
,
safeDictUpdate
from
corepost.convert
import
convertForSerialization
,
generateXml
from
corepost.convert
import
convertForSerialization
,
generateXml
from
corepost.filters
import
IRequestFilter
,
IResponseFilter
from
corepost.filters
import
IRequestFilter
,
IResponseFilter
...
@@ -385,17 +385,26 @@ class RequestRouter:
...
@@ -385,17 +385,26 @@ class RequestRouter:
requestargs
=
parse_qs
(
request
.
content
.
read
(),
1
)
requestargs
=
parse_qs
(
request
.
content
.
read
(),
1
)
#merge form args
#merge form args
for
arg
in
requestargs
.
keys
():
if
len
(
requestargs
.
keys
())
>
0
:
# maintain first instance of an argument always
for
arg
in
requestargs
.
keys
():
if
arg
not
in
allargs
:
# maintain first instance of an argument always
allargs
[
arg
]
=
requestargs
[
arg
][
0
]
safeDictUpdate
(
allargs
,
arg
,
requestargs
[
arg
][
0
])
elif
hasattr
(
request
,
'json'
):
# if JSON parse root elements instead of form elements
# if YAML parse root elements instead of form elements
if
hasattr
(
request
,
'json'
):
for
key
in
request
.
json
.
keys
():
for
key
in
request
.
json
.
keys
():
if
key
not
in
allargs
:
safeDictUpdate
(
allargs
,
key
,
request
.
json
[
key
])
allargs
[
key
]
=
request
.
json
[
key
]
elif
hasattr
(
request
,
'yaml'
):
# if YAML parse root elements instead of form elements
for
key
in
request
.
yaml
.
keys
():
safeDictUpdate
(
allargs
,
key
,
request
.
yaml
[
key
])
elif
hasattr
(
request
,
'xml'
):
# if XML, parse attributes first, then root nodes
for
key
in
request
.
xml
.
attrib
:
safeDictUpdate
(
allargs
,
key
,
request
.
xml
.
attrib
[
key
])
for
el
in
request
.
xml
.
findall
(
"*"
):
safeDictUpdate
(
allargs
,
el
.
tag
,
el
.
text
)
def
__filterRequests
(
self
,
request
):
def
__filterRequests
(
self
,
request
):
"""Filters incoming requests"""
"""Filters incoming requests"""
for
webFilter
in
self
.
__requestFilters
:
for
webFilter
in
self
.
__requestFilters
:
...
...
corepost/test/feature/arguments.feature
View file @
a7a26a6a
...
@@ -33,11 +33,13 @@ Feature: Arguments
...
@@ -33,11 +33,13 @@ Feature: Arguments
@arguments_by_type
@arguments_by_type
Scenario Outline
:
Parse form arguments OR from JSON documents for POST / PUT
Scenario Outline
:
Parse form arguments OR from JSON documents for POST / PUT
Given 'arguments' is running
Given 'arguments' is running
# pass in as form arguments
# pass in as form arguments
When as user 'None
:
None' I <method> 'http
:
//127.0.0.1
:
8082/formOrJson'
with
'first=John&last=Doe'
When as user 'None
:
None' I <method> 'http
:
//127.0.0.1
:
8082/formOrJson'
with
'first=John&last=Doe'
Then
I expect HTTP code
<code>
Then
I expect HTTP code
<code>
And
I expect content contains 'John Doe'
And
I expect content contains 'John Doe'
# pass in as JSON document
# pass in as *** JSON *** document
When as user 'None
:
None' I <method> 'http
:
//127.0.0.1
:
8082/formOrJson'
with
JSON
When as user 'None
:
None' I <method> 'http
:
//127.0.0.1
:
8082/formOrJson'
with
JSON
"""
"""
{"first":"Jane","last":"Doeovskaya"}
{"first":"Jane","last":"Doeovskaya"}
...
@@ -51,7 +53,53 @@ Feature: Arguments
...
@@ -51,7 +53,53 @@ Feature: Arguments
"""
"""
Then
I expect HTTP code
<code>
Then
I expect HTTP code
<code>
And
I expect content contains 'Jane Doeovskaya'
And
I expect content contains 'Jane Doeovskaya'
# pass in as *** YAML *** document
When as user 'None
:
None' I <method> 'http
:
//127.0.0.1
:
8082/formOrJson'
with
YAML
"""
first: Oksana
last: Dolovskaya
"""
Then
I expect HTTP code
<code>
And
I expect content contains 'Oksana Dolovskaya'
# additional arguments should be OK
When as user 'None
:
None' I <method> 'http
:
//127.0.0.1
:
8082/formOrJson'
with
YAML
"""
first: Svetlana
middle: Jane
last: Gingrychnoya
"""
Then
I expect HTTP code
<code>
And
I expect content contains 'Svetlana Gingrychnoya'
# pass in as *** XML *** document wit both attributes and child nodes
When as user 'None
:
None' I <method> 'http
:
//127.0.0.1
:
8082/formOrJson'
with
XML
"""
<root first="John" last="Doe" middle="Jim"/>
"""
Then
I expect HTTP code
<code>
And
I expect content contains 'John Doe'
When as user 'None
:
None' I <method> 'http
:
//127.0.0.1
:
8082/formOrJson'
with
XML
"""
<root first="Jan" middle="Jim">
<last>Dolowski</last>
</root>
"""
Then
I expect HTTP code
<code>
And
I expect content contains 'Jan Dolowski'
When as user 'None
:
None' I <method> 'http
:
//127.0.0.1
:
8082/formOrJson'
with
XML
"""
<root>
<first>Grzegorz</first>
<middle>Jim</middle>
<last>Brzeczyszczykiewicz</last>
</root>
"""
Then
I expect HTTP code
<code>
And
I expect content contains 'Grzegorz Brzeczyszczykiewicz'
Examples
:
Examples
:
|
method
|
code
|
|
method
|
code
|
|
POST
|
201
|
|
POST
|
201
|
...
...
corepost/utils.py
View file @
a7a26a6a
...
@@ -29,4 +29,8 @@ def checkExpectedInterfaces(objects,expectedInterface):
...
@@ -29,4 +29,8 @@ def checkExpectedInterfaces(objects,expectedInterface):
if
not
expectedInterface
.
providedBy
(
obj
):
if
not
expectedInterface
.
providedBy
(
obj
):
raise
RuntimeError
(
"Object
%
s does not implement
%
s interface"
%
(
obj
,
expectedInterface
))
raise
RuntimeError
(
"Object
%
s does not implement
%
s interface"
%
(
obj
,
expectedInterface
))
def
safeDictUpdate
(
dictObject
,
key
,
value
):
"""Only adds a key to a dictionary. If key exists, it leaves it untouched"""
if
key
not
in
dictObject
:
dictObject
[
key
]
=
value
\ No newline at end of file
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