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
0f3a3792
Commit
0f3a3792
authored
Sep 02, 2011
by
Jacek Furmankiewicz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
validators WIP
parent
7d4f05aa
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
21 deletions
+66
-21
arguments.py
corepost/test/arguments.py
+13
-4
validate.feature
corepost/test/feature/validate.feature
+14
-5
web.py
corepost/web.py
+38
-11
setup.py
setup.py
+1
-1
No files found.
corepost/test/arguments.py
View file @
0f3a3792
...
@@ -3,20 +3,29 @@ Argument extraction tests
...
@@ -3,20 +3,29 @@ Argument extraction tests
@author: jacekf
@author: jacekf
'''
'''
from
corepost.web
import
CorePost
from
corepost.web
import
CorePost
,
validate
from
corepost.enums
import
Http
from
corepost.enums
import
Http
from
formencode
import
Schema
,
validators
from
formencode
import
Schema
,
validators
app
=
CorePost
()
app
=
CorePost
()
class
TestSchema
(
Schema
):
allow_extra_fields
=
True
childId
=
validators
.
Regex
(
regex
=
"^jacekf|test$"
)
@
app
.
route
(
"/int/<int:intarg>/float/<float:floatarg>/string/<stringarg>"
,
Http
.
GET
)
@
app
.
route
(
"/int/<int:intarg>/float/<float:floatarg>/string/<stringarg>"
,
Http
.
GET
)
def
test
(
request
,
intarg
,
floatarg
,
stringarg
,
**
kwargs
):
def
test
(
request
,
intarg
,
floatarg
,
stringarg
,
**
kwargs
):
args
=
(
intarg
,
floatarg
,
stringarg
)
args
=
(
intarg
,
floatarg
,
stringarg
)
return
"
%
s"
%
map
(
lambda
x
:
(
type
(
x
),
x
),
args
)
return
"
%
s"
%
map
(
lambda
x
:
(
type
(
x
),
x
),
args
)
@
app
.
route
(
"/validate/<int:rootId>/children"
,
Http
.
POST
)
@
app
.
route
(
"/validate/<int:rootId>/schema"
,
Http
.
POST
)
@
app
.
validate
(
childid
=
validators
.
String
(
not_empty
=
True
))
@
validate
(
schema
=
TestSchema
)
def
post
(
request
,
rootId
,
childId
,
**
kwargs
):
def
postValidateSchema
(
request
,
rootId
,
childId
,
**
kwargs
):
return
"
%
s -
%
s -
%
s"
%
(
rootId
,
childId
,
kwargs
)
@
app
.
route
(
"/validate/<int:rootId>/custom"
,
Http
.
POST
)
@
validate
(
childId
=
validators
.
Regex
(
regex
=
"^jacekf|test$"
))
def
postValidateCustom
(
request
,
rootId
,
childId
,
**
kwargs
):
return
"
%
s -
%
s -
%
s"
%
(
rootId
,
childId
,
kwargs
)
return
"
%
s -
%
s -
%
s"
%
(
rootId
,
childId
,
kwargs
)
def
run_app_arguments
():
def
run_app_arguments
():
...
...
corepost/test/feature/validate.feature
View file @
0f3a3792
...
@@ -5,14 +5,23 @@ Feature: Argument Validators
...
@@ -5,14 +5,23 @@ Feature: Argument Validators
CorePost should be able to correctly validate path, query and form arguments
CorePost should be able to correctly validate path, query and form arguments
@validate
@validate
Scenario Outline
:
Path argument extrac
tion
Scenario Outline
:
Form argument valida
tion
Given 'arguments' is running
Given 'arguments' is running
When as user 'None
:
None' I POST 'http
:
//127.0.0.1
:
8082/validate/23/children'
with
'<args>'
# childId accepts only jacekf or test, via Regex validator
When as user 'None
:
None' I POST 'http
:
//127.0.0.1
:
8082/validate/23/<url>'
with
'<args>'
Then
I expect HTTP code
<code>
Then
I expect HTTP code
<code>
And
I expect content contains '<content>'
And
I expect content contains '<content>'
Examples
:
Examples
:
|
args
|
code
|
content
|
|
url
|
args
|
code
|
content
|
|
childId=jacekf
|
201
|
23
-
jacekf
-
{}
|
# validates using argument-specific validators
|
childId=jacekf&otherId=test
|
201
|
23
-
jacekf
-
{'otherId':
'test'}
|
|
custom
|
childId=jacekf
|
201
|
23
-
jacekf
-
{}
|
|
custom
|
childId=jacekf&otherId=test
|
201
|
23
-
jacekf
-
{'otherId':
'test'}
|
|
custom
|
childId=test
|
201
|
23
-
test
-
{}
|
|
custom
|
childId=wrong
|
400
|
'wrong'
is
not
a
valid
value
for
'childId':
The
input
is
not
valid
|
# validates using Schema
|
schema
|
childId=jacekf
|
201
|
23
-
jacekf
-
{}
|
|
schema
|
childId=jacekf&otherId=test
|
201
|
23
-
jacekf
-
{'otherId':
'test'}
|
|
schema
|
childId=test
|
201
|
23
-
test
-
{}
|
|
schema
|
childId=wrong
|
400
|
'wrong'
is
not
a
valid
value
for
'childId':
The
input
is
not
valid
|
\ No newline at end of file
corepost/web.py
View file @
0f3a3792
...
@@ -12,7 +12,7 @@ from collections import defaultdict
...
@@ -12,7 +12,7 @@ from collections import defaultdict
from
enums
import
MediaType
from
enums
import
MediaType
from
corepost.enums
import
Http
from
corepost.enums
import
Http
from
corepost.utils
import
getMandatoryArgumentNames
from
corepost.utils
import
getMandatoryArgumentNames
from
formencode
import
validators
,
Schema
,
Validator
from
formencode
import
FancyValidator
,
Invalid
class
RequestRouter
:
class
RequestRouter
:
''' Common class for containing info related to routing a request to a function '''
''' Common class for containing info related to routing a request to a function '''
...
@@ -148,15 +148,6 @@ class CorePost(Resource):
...
@@ -148,15 +148,6 @@ class CorePost(Resource):
return
f
return
f
return
wrap
return
wrap
def
validate
(
self
,
schema
=
None
,
**
kwargs
):
'''
Main decorator for registering additional validators for incoming URL arguments
'''
def
wrap
(
f
,
**
kwargs
):
print
kwargs
return
f
return
wrap
def
render_GET
(
self
,
request
):
def
render_GET
(
self
,
request
):
""" Handles all GET requests """
""" Handles all GET requests """
return
self
.
__renderUrl
(
request
)
return
self
.
__renderUrl
(
request
)
...
@@ -246,5 +237,41 @@ class CorePost(Resource):
...
@@ -246,5 +237,41 @@ class CorePost(Resource):
reactor
.
listenTCP
(
port
,
factory
)
#@UndefinedVariable
reactor
.
listenTCP
(
port
,
factory
)
#@UndefinedVariable
reactor
.
run
()
#@UndefinedVariable
reactor
.
run
()
#@UndefinedVariable
##################################################################################################
#
# DECORATORS
#
##################################################################################################
def
validate
(
schema
=
None
,
**
vKwargs
):
\ No newline at end of file
'''
Main decorator for registering additional validators for incoming URL arguments
'''
def
fn
(
realfn
):
def
wrap
(
*
args
,
**
kwargs
):
# first run schema validation, then the custom validators
errors
=
()
if
schema
!=
None
:
try
:
schema
.
to_python
(
kwargs
)
except
Invalid
as
ex
:
errors
=
()
for
error
in
ex
.
error_dict
.
keys
():
errors
.
append
()
raise
TypeError
(
"
%
s"
%
ex
)
for
arg
in
vKwargs
.
keys
():
validator
=
vKwargs
[
arg
]
if
arg
in
kwargs
:
val
=
kwargs
[
arg
]
try
:
validator
.
to_python
(
val
)
except
Invalid
as
ex
:
raise
TypeError
(
"'
%
s' is not a valid value for '
%
s':
%
s"
%
(
val
,
arg
,
ex
))
else
:
if
isinstance
(
validator
,
FancyValidator
)
and
validator
.
not_empty
:
raise
TypeError
(
"Missing mandatory argument '
%
s'"
%
arg
)
return
realfn
(
*
args
,
**
kwargs
)
return
wrap
return
fn
setup.py
View file @
0f3a3792
...
@@ -49,7 +49,7 @@ def read(fname):
...
@@ -49,7 +49,7 @@ def read(fname):
setup
(
setup
(
name
=
"CorePost"
,
name
=
"CorePost"
,
version
=
"0.0.
4
"
,
version
=
"0.0.
5
"
,
author
=
"Jacek Furmankiewicz"
,
author
=
"Jacek Furmankiewicz"
,
author_email
=
"jacek99@gmail.com"
,
author_email
=
"jacek99@gmail.com"
,
description
=
(
"A Twisted Web REST micro-framework"
),
description
=
(
"A Twisted Web REST micro-framework"
),
...
...
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