Commit 3c3d5260 authored by Jacek Furmankiewicz's avatar Jacek Furmankiewicz

argument tests

parent 3424e38d
...@@ -12,6 +12,8 @@ Single REST module example ...@@ -12,6 +12,8 @@ Single REST module example
The simplest possible REST application: The simplest possible REST application:
::
from corepost.web import CorePost from corepost.web import CorePost
from corepost.enums import Http from corepost.enums import Http
...@@ -41,6 +43,8 @@ The key CorePost object is just an extension of the regular twisted.web Resource ...@@ -41,6 +43,8 @@ The key CorePost object is just an extension of the regular twisted.web Resource
Therefore, it can easily be used to assemble a multi-module REST applications with Therefore, it can easily be used to assemble a multi-module REST applications with
different CorePost resources serving from different context paths: different CorePost resources serving from different context paths:
::
from corepost.web import CorePost from corepost.web import CorePost
from corepost.enums import Http from corepost.enums import Http
from twisted.web.resource import Resource from twisted.web.resource import Resource
...@@ -97,6 +101,23 @@ The example above creates 3 modules ("/","module1","/module2") and exposes the f ...@@ -97,6 +101,23 @@ The example above creates 3 modules ("/","module1","/module2") and exposes the f
http://127.0.0.1:8080/module2/ http://127.0.0.1:8080/module2/
http://127.0.0.1:8080/module2/sub http://127.0.0.1:8080/module2/sub
Path argument extraction
------------------------
CorePort can easily extract path arguments from an URL and convert them to the desired type.
The supported types are:
* *int*, e.g. '<int:some_int_arg>'
* *float*, e.g. '<int:some_float_arg>'
* *string*, e.g. '<string_arg>'
Example:
::
@app.route("/int/<int:intarg>/float/<float:floatarg>/string/<stringarg>",Http.GET)
def test(request,intarg,floatarg,stringarg,**kwargs):
pass
@defer.inlineCallbacks support @defer.inlineCallbacks support
------------------------------ ------------------------------
......
'''
Argument extraction tests
@author: jacekf
'''
from corepost.web import CorePost
from corepost.enums import Http
app = CorePost()
@app.route("/int/<int:intarg>/float/<float:floatarg>/string/<stringarg>",Http.GET)
def test(request,intarg,floatarg,stringarg,**kwargs):
args = (intarg,floatarg,stringarg)
return "%s" % map(lambda x: (type(x),x),args)
def run_app_arguments():
app.run(8082)
\ No newline at end of file
Using step definitions from: '../steps'
@arguments
Feature: Arguments
CorePost should be able to correctly extract arguments
from paths, query arguments and form arguments
@path_arguments
Scenario Outline: Path argument extraction
Given 'arguments' is running
When as user 'None:None' I GET 'http://127.0.0.1:8082<url>'
Then I expect HTTP code <code>
And I expect content contains '<content>'
Examples:
| url | code | content |
| /int/1/float/1.1/string/TEST | 200 | [(<type 'int'>, 1), (<type 'float'>, 1.1), (<type 'str'>, 'TEST')] |
| /int/1/float/1/string/TEST | 200 | [(<type 'int'>, 1), (<type 'float'>, 1.0), (<type 'str'>, 'TEST')] |
| /int/1/float/1/string/23 | 200 | [(<type 'int'>, 1), (<type 'float'>, 1.0), (<type 'str'>, '23')] |
@path_arguments
Scenario Outline: Path argument extraction - error handling
Given 'arguments' is running
When as user 'None:None' I GET 'http://127.0.0.1:8082<url>'
Then I expect HTTP code <code>
And I expect content contains '<content>'
Examples:
| 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
...@@ -9,8 +9,9 @@ from freshen import Before, After, Given, When, Then, scc, glc, assert_equals, a ...@@ -9,8 +9,9 @@ from freshen import Before, After, Given, When, Then, scc, glc, assert_equals, a
from urllib import urlencode from urllib import urlencode
from corepost.test.home_resource import run_app_home from corepost.test.home_resource import run_app_home
from corepost.test.multi_resource import run_app_multi from corepost.test.multi_resource import run_app_multi
from corepost.test.arguments import run_app_arguments
apps = {'home_resource' : run_app_home,'multi_resource':run_app_multi} apps = {'home_resource' : run_app_home,'multi_resource':run_app_multi,'arguments':run_app_arguments}
def as_dict(parameters): def as_dict(parameters):
dict_val = {} dict_val = {}
......
''' """
Created on 2011-08-25 Twisted REST micro-framework
================================
@author: jacekf Based on *Flask* API, with integrated multiprocessing support for full usage of all CPUs.
''' Provides a more Flask/Sinatra-style API on top of the core *twisted.web* APIs.
The simplest possible twisted.web CorePost REST application:
::
from corepost.web import CorePost
from corepost.enums import Http
app = CorePost()
@app.route("/",Http.GET)
def root(request,**kwargs):
return request.path
@app.route("/test",Http.GET)
def test(request,**kwargs):
return request.path
@app.route("/test/<int:numericid>",Http.GET)
def test_get_resources(request,numericid,**kwargs):
return "%s" % numericid
if __name__ == '__main__':
app.run()
Links
`````
* `website <http://github.com/jacek99/corepost>`_
* `Twisted <http://twistedmatrix.com>`_
"""
import os import os
from setuptools import setup from setuptools import setup
import markdown
# Utility function to read the README file. # Utility function to read the README file.
# Used for the long_description. It's nice, because now 1) we have a top level # Used for the long_description. It's nice, because now 1) we have a top level
# README file and 2) it's easier to type in the README file than to put a raw # README file and 2) it's easier to type in the README file than to put a raw
# string in below ... # string in below ...
def read(fname): def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read() md = open(os.path.join(os.path.dirname(__file__), fname)).read()
return md
#return "<html>%s<html>" % markdown.markdown(md)
setup( setup(
name="CorePost", name="CorePost",
...@@ -24,7 +60,7 @@ setup( ...@@ -24,7 +60,7 @@ setup(
keywords="twisted rest flask sinatra get post put delete web", keywords="twisted rest flask sinatra get post put delete web",
url="https://github.com/jacek99/corepost", url="https://github.com/jacek99/corepost",
packages=['corepost', ], packages=['corepost', ],
long_description=read('README.md'), long_description=__doc__,
classifiers=[ classifiers=[
"Development Status :: 3 - Alpha", "Development Status :: 3 - Alpha",
"Environment :: Web Environment", "Environment :: Web Environment",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment