Skip to content

Instantly share code, notes, and snippets.

@greut
Created November 20, 2009 13:33
Show Gist options
  • Save greut/239503 to your computer and use it in GitHub Desktop.
Save greut/239503 to your computer and use it in GitHub Desktop.
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