Created
November 20, 2009 13:33
-
-
Save greut/239503 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def renderer(template, args, encoding=None): | |
return template + ": "+ args['body'] | |
class Resource(resource.Resource): | |
@resource.child("foo") | |
def foo(self, request, segments): | |
method_name = "foo_"+request.method.lower() | |
if request.method == "HEAD" and not hasattr(self, method_name): | |
method_name = "foo_get" | |
if hasattr(self, method_name): | |
return getattr(self, method_name)(request, segments) | |
else: | |
raise http.MethodNotAllowedError(["HEAD", "GET", "POST"]) | |
@templating.page("page") | |
def foo_get(self, request, segments): | |
return {"body": "get"} | |
@templating.page("page") | |
def foo_post(self, request, segments): | |
return {"body": "post"} | |
environ = {'restish.templating': templating.Templating(renderer)} | |
A = webtest.TestApp(app.RestishApp(Resource())) | |
R = A.get('/foo', extra_environ=environ, status=200) | |
assert R.body == 'page: get' | |
R = A.post('/foo', extra_environ=environ, status=200) | |
assert R.body == 'page: post' | |
R = A.put('/foo', extra_environ=environ, status=405) | |
R = A.head('/foo', extra_environ=environ, status=200) | |
assert R.body == '' # this fails! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment