Anonymous avatar Anonymous committed 2aa42d3

new tornado server. with basic handler & testing framework

Comments (0)

Files changed (1)

tic-tac-toe/server/tttserv.py

+#!/usr/bin/env python
+#adapted from Facebook's helloworld.py
+#testing from:
+#http://groups.google.com/group/python-tornado/browse_thread/thread/867cfb2665ea10a9/fc9a53000f4c104d
+
+import sys
+import unittest
+
+import tornado.httpserver
+import tornado.httpclient
+import tornado.ioloop
+import tornado.options
+import tornado.web
+
+from tornado.options import define, options
+
+import simplejson as json
+
+class TestTicTacToe(unittest.TestCase):
+    response = None
+
+    def setUp(self):
+        self.client = tornado.httpclient.AsyncHTTPClient()
+        #self.client = tornado.httpclient.HTTPClient()
+
+    def handle_request(self, response):
+        """
+        For async tests
+        """
+        self.response = response
+        tornado.ioloop.IOLoop.instance().stop()
+
+    def test_no_board(self):
+        self.client.fetch(
+            'http://localhost:%d/' % (options.port),
+            self.handle_request,
+            method='POST',
+            body='',#POST data
+        )
+        tornado.ioloop.IOLoop.instance().start()
+
+    def test_get(self):
+        self.client.fetch('http://localhost:%d/' % (options.port),
+                          self.handle_request)
+        tornado.ioloop.IOLoop.instance().start()
+
+class MainHandler(tornado.web.RequestHandler):
+    def post(self):
+        pass
+        #board_json = self.get_argument('board', None)
+        #if not board_json:
+        #    raise tornado.web.HTTPError(500, "Board not found.")
+        #board = json.loads(board_json)
+        #for row in board:
+        #    for item in row:
+        #        pass
+                #self.write()
+    def get(self):
+        pass
+
+define("port", default=8888, help="run on the given port", type=int)
+define("test", type=bool, help="run unit tests for server")
+def main():
+    tornado.options.parse_command_line()
+
+    application = tornado.web.Application([
+        (r"/", MainHandler),
+    ])
+
+    http_server = tornado.httpserver.HTTPServer(application)
+    http_server.listen(options.port)
+
+    if options.test:
+        sys.argv.remove('--test')
+        unittest.main()
+    else:
+        tornado.ioloop.IOLoop.instance().start()
+if __name__ == "__main__":
+    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.