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
f7cdbf9a
Commit
f7cdbf9a
authored
Mar 16, 2012
by
Jacek Furmankiewicz
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
https://github.com/jacek99/corepost
parents
365be2fc
29a983f3
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
16 deletions
+58
-16
routing.py
corepost/routing.py
+24
-14
arguments.py
corepost/test/arguments.py
+5
-0
arguments.feature
corepost/test/feature/arguments.feature
+29
-2
No files found.
corepost/routing.py
View file @
f7cdbf9a
...
...
@@ -248,26 +248,15 @@ class RequestRouter:
#actual call
if
urlRouterInstance
!=
None
and
pathargs
!=
None
:
allargs
=
copy
.
deepcopy
(
pathargs
)
# 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
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
for
arg
in
requestargs
.
keys
():
# maintain first instance of an argument always
if
arg
not
in
allargs
:
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
, YAML, XML
self
.
__parseRequestData
(
request
)
# parse request arguments from form or JSON docss
self
.
__addRequestArguments
(
request
,
allargs
)
urlRouter
=
urlRouterInstance
.
urlRouter
val
=
urlRouter
.
call
(
urlRouterInstance
.
clazz
,
request
,
**
allargs
)
#handle Deferreds natively
if
isinstance
(
val
,
defer
.
Deferred
):
# add callback to finish the request
...
...
@@ -386,6 +375,27 @@ class RequestRouter:
except
Exception
as
ex
:
raise
TypeError
(
"Unable to parse YAML body:
%
s"
%
ex
)
def
__addRequestArguments
(
self
,
request
,
allargs
):
"""Parses the request form arguments OR JSON document root elements to build the list of arguments to a method"""
# 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
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
for
arg
in
requestargs
.
keys
():
# maintain first instance of an argument always
if
arg
not
in
allargs
:
allargs
[
arg
]
=
requestargs
[
arg
][
0
]
# if JSON parse root elements instead of form elements
if
hasattr
(
request
,
'json'
):
for
key
in
request
.
json
.
keys
():
if
key
not
in
allargs
:
allargs
[
key
]
=
request
.
json
[
key
]
def
__filterRequests
(
self
,
request
):
"""Filters incoming requests"""
for
webFilter
in
self
.
__requestFilters
:
...
...
corepost/test/arguments.py
View file @
f7cdbf9a
...
...
@@ -28,6 +28,11 @@ class ArgumentApp():
def
postValidateCustom
(
self
,
request
,
rootId
,
childId
,
**
kwargs
):
return
"
%
s -
%
s -
%
s"
%
(
rootId
,
childId
,
kwargs
)
@
route
(
"/formOrJson"
,(
Http
.
POST
,
Http
.
PUT
))
def
postArgumentsByContentType
(
self
,
request
,
first
,
last
,
**
kwargs
):
return
"
%
s
%
s"
%
(
str
(
first
),
str
(
last
))
def
run_app_arguments
():
app
=
RESTResource
((
ArgumentApp
(),))
app
.
run
(
8082
)
\ No newline at end of file
corepost/test/feature/arguments.feature
View file @
f7cdbf9a
...
...
@@ -3,7 +3,7 @@ Using step definitions from: '../steps'
@arguments
Feature
:
Arguments
CorePost should be able to correctly extract arguments
from paths, query arguments
and form arg
uments
from paths, query arguments
, form arguments and JSON doc
uments
@arguments_ok
Scenario Outline
:
Path argument extraction
...
...
@@ -29,4 +29,31 @@ Feature: Arguments
|
url
|
code
|
content
|
|
/int/WRONG/float/1.1/string/TEST
|
404
|
URL
'/int/WRONG/float/1.1/string/TEST'
not
found
|
|
/int/1/float/WRONG/string/TEST
|
404
|
URL
'/int/1/float/WRONG/string/TEST'
not
found
|
|
\ No newline at end of file
@arguments_by_type
Scenario Outline
:
Parse form arguments OR from JSON documents for POST / PUT
Given 'arguments' is running
# pass in as form arguments
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>
And
I expect content contains 'John Doe'
# pass in as JSON document
When as user 'None
:
None' I <method> 'http
:
//127.0.0.1
:
8082/formOrJson'
with
JSON
"""
{"first":"Jane","last":"Doeovskaya"}
"""
Then
I expect HTTP code
<code>
And
I expect content contains 'Jane Doeovskaya'
# additional arguments should be OK
When as user 'None
:
None' I <method> 'http
:
//127.0.0.1
:
8082/formOrJson'
with
JSON
"""
{"first":"Jane","last":"Doeovskaya","middle":"Oksana"}
"""
Then
I expect HTTP code
<code>
And
I expect content contains 'Jane Doeovskaya'
Examples
:
|
method
|
code
|
|
POST
|
201
|
|
PUT
|
200
|
\ 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