Permalink
Browse files

Explicitly test the resource function and add some documentation.

  • Loading branch information...
1 parent 812155a commit ed95f2a74a35ce0841ac406b936b9aa1de682f22 @dreid dreid committed Apr 14, 2012
Showing with 47 additions and 0 deletions.
  1. +29 −0 README.rst
  2. +2 −0 klein/resource.py
  3. +13 −0 klein/test_app.py
  4. +3 −0 klein/test_resource.py
View
@@ -108,3 +108,32 @@ Here is a simple Google proxy.
run("localhost", 8080)
+
+twistd
+~~~~~~
+
+Another very important integration point with Twisted is the ``twistd``
+application runner. It provides rich logging support, daemonization, reactor
+selection, profiler integration, and many more incredibly useful features.
+
+To provide access to these features (and others like HTTPS) klein provides the
+``resource`` function which returns a valid ``twisted.web.resource.IResource``
+for your application.
+
+Here is our "Hello, World!" application again in a form that can be launched
+by ``twistd``.
+
+::
+
+ from klein import resource, route
+
+ @route('/')
+ def hello(request):
+ return "Hello, world!"
+
+
+To run the above application we can save it as ``helloworld.py`` and use the
+``twistd web`` plugin.
+
+::
+ twistd -n web --class=helloworld.resource
View
@@ -8,6 +8,7 @@
__all__ = ["KleinResource"]
+
class KleinResource(Resource):
"""
A ``Resource`` that can do URL routing.
@@ -59,6 +60,7 @@ def render(self, request):
# something renderable or printable. Return NOT_DONE_YET and set up
# the incremental renderer.
d = defer.maybeDeferred(handler, request, **kwargs)
+
def process(r):
if IResource.providedBy(r):
while (request.postpath and
View
@@ -6,6 +6,7 @@
from klein import Klein
+
class KleinTestCase(unittest.TestCase):
def test_route(self):
"""
@@ -118,3 +119,15 @@ def test_runWithLogFile(self, reactor, mock_log, mock_site, mock_kr):
mock_site.assert_called_with(mock_kr.return_value)
mock_kr.assert_called_with(app)
mock_log.startLogging.assert_called_with(logFile)
+
+
+ @patch('klein.app.KleinResource')
+ def test_resource(self, mock_kr):
+ """
+ L{Klien.resource} returns a L{KleinResource}.
+ """
+ app = Klein()
+ resource = app.resource()
+
+ mock_kr.assert_called_with(app)
+ self.assertEqual(mock_kr.return_value, resource)
View
@@ -10,6 +10,7 @@
from mock import Mock
+
def requestMock(path, method="GET", host="localhost", port=8080, isSecure=False):
postpath = path.split('/')
@@ -41,6 +42,7 @@ def processingFailed(failure):
return request
+
def _render(resource, request):
result = resource.render(request)
if isinstance(result, str):
@@ -78,6 +80,7 @@ def render(self, request):
class ChildResource(Resource):
isLeaf = True
+
def __init__(self, name):
self._name = name

0 comments on commit ed95f2a

Please sign in to comment.