Commits

silf...@Pioneer  committed 10b1521

checking in initial tornado code with tests;

  • Participants

Comments (0)

Files changed (9)

File __init__.py

Empty file added.

File bin/__init__.py

Empty file added.

File bin/pywebclient.py

+#!/usr/bin/env python
+
+if __name__=='__main__':
+    from os.path import dirname, abspath
+    from sys import path
+    new_path = dirname(abspath(__file__))+'/../../'
+    print(new_path)
+    path.append(new_path)
+    
+    from pwc.server import run_server
+    run_server()
+#!/usr/bin/env python
+
+#Tornado spike
+
+import tornado.httpserver
+import tornado.ioloop
+import tornado.web
+import tornado.template
+
+#PATH HACK
+from os.path import dirname, abspath
+PATH=dirname(abspath(__file__))+'/'
+
+class MainHandler(tornado.web.RequestHandler):
+    def get(self):
+        loader = tornado.template.Loader(PATH+'templates')
+        self.write(loader.load('base.html').generate(title='Test Page'))
+
+    def post(self):
+        loader = tornado.template.Loader(PATH+'templates')
+        items=[]
+        [items.append(self.get_argument(x)) for x in ['request_type',
+                                                      'url',]]
+        self.write(loader.load('resp.html').generate(title='Response', 
+                                                     items=items))
+
+application = tornado.web.Application([
+    (r'/', MainHandler),
+])
+
+def run_server():
+    http_server = tornado.httpserver.HTTPServer(application)
+    http_server.listen(8888)
+    tornado.ioloop.IOLoop.instance().start()
+

File templates/__init__.py

Empty file added.

File templates/base.html

+<html>
+  <head>
+    <title>{% block title %} {{ title }} {% end %}</title>
+  </head>
+  <body>
+    <h1>{% block title %} {{ title }} {% end %}</h1>
+    <form action='/' method='post'>
+      <select name='request_type'>
+        <option value='GET' selected>GET</option>
+        <option value='POST'>POST</option>
+      </select>
+      <input type='text' name='url'><br>
+      <input type='submit' value='submit url'>
+    </form>
+  </body>
+</html>

File templates/resp.html

+<html>
+   <head>
+      <title>{{ title }}</title>
+   </head>
+   <body>
+     <h2>Data I got:</h2>
+     <ul>
+       {% for item in items %}
+         <li>{{ escape(item) }}</li>
+       {% end %}
+     </ul>
+   </body>
+ </html>

File test/__init__.py

Empty file added.

File test/unittests.py

+import unittest
+import tornado.httpserver
+import tornado.httpclient
+import tornado.ioloop
+import tornado.web
+
+#From tornado group 
+#http://groups.google.com/group/python-tornado/browse_thread/thread/867cfb2665ea10a9
+
+#PATH HACK
+from os.path import dirname, abspath
+PATH=dirname(abspath(__file__))+'/'
+
+from sys import path
+path.append(PATH+'/../')
+from server import MainHandler
+
+class TestPWC(unittest.TestCase):
+    response = None
+
+    def setUp(self):
+        class StoppableHTTPServer(tornado.httpserver.HTTPServer):
+            def stop(self):
+                self.io_loop.remove_handler(self._socket.fileno())
+                self._socket.close()
+
+        application = tornado.web.Application([
+                (r'/', MainHandler),
+                ])
+
+        self.http_server = StoppableHTTPServer(application)
+        self.http_server.listen(8888)
+
+    def tearDown(self):
+        self.http_server.stop()
+
+    def handle_request(self, response):
+        self.response = response
+
+        tornado.ioloop.IOLoop.instance().stop()
+
+    def testGetPage(self):
+        http_client = tornado.httpclient.AsyncHTTPClient()
+        http_client.fetch('http://localhost:8888',
+                          self.handle_request)
+
+        tornado.ioloop.IOLoop.instance().start()
+
+        self.failIf(self.response.error)
+        self.assertEqual(self.response.body, '''<html>
+<head>
+<title> Test Page </title>
+</head>
+<body>
+<h1> Test Page </h1>
+<form action='/' method='post'>
+<select name='request_type'>
+<option value='GET' selected>GET</option>
+<option value='POST'>POST</option>
+</select>
+<input type='text' name='url'><br>
+<input type='submit' value='submit url'>
+</form>
+</body>
+</html>
+''')
+
+if __name__=='__main__':
+    unittest.main()
+