Working on unit tests.
authorEmil Mikulic <emikulic@gmail.com>
Mon, 17 Jan 2011 12:44:02 +0000 (23:44 +1100)
committerEmil Mikulic <emikulic@gmail.com>
Sun, 28 Apr 2013 10:37:08 +0000 (20:37 +1000)
devel/test.py [new file with mode: 0755]

diff --git a/devel/test.py b/devel/test.py
new file mode 100755 (executable)
index 0000000..e412f30
--- /dev/null
@@ -0,0 +1,71 @@
+#!/usr/bin/env python
+import unittest
+import socket
+import signal
+
+class Conn:
+    def __init__(self):
+        self.port = 12346
+        self.s = socket.socket()
+        self.s.connect(("0.0.0.0", self.port))
+        # connect throws socket.error on connection refused
+
+    def get(self, req):
+        self.s.send(req)
+        ret = ""
+        while True:
+            signal.alarm(1)
+            r = self.s.recv(65536)
+            signal.alarm(0)
+            if r == "":
+                break
+            else:
+                ret += r
+        return ret
+
+class TestCases(unittest.TestCase):
+    def assertContains(self, body, *strings):
+        for s in strings:
+            self.assertTrue(s in body,
+                            msg="expected %s in %s"%(repr(s), repr(body)))
+
+    def assertIsIndex(self, body, path):
+        self.assertContains(body,
+            "<title>%s</title>\n"%path,
+            "<h1>%s</h1>\n"%path,
+            '<a href="..">..</a>/',
+            'Generated by darkhttpd')
+
+    def assertIsInvalid(self, body, path):
+        self.assertContains(body,
+            "<title>400 Bad Request</title>",
+            "<h1>Bad Request</h1>\n",
+            "You requested an invalid URI: %s\n"%path,
+            'Generated by darkhttpd')
+
+    # FIXME: failing
+    #def testIndex_HTTP_0_9(self):
+    #    body = Conn().get("GET /\n\n")
+    #    self.assertIsIndex(body)
+
+    def testIndex_HTTP_1_0(self):
+        body = Conn().get("GET / HTTP/1.0\n\n")
+        self.assertIsIndex(body, "/")
+
+    def testUpDirValid(self):
+        body = Conn().get("GET /dir/../ HTTP/1.0\n\n")
+        self.assertIsIndex(body, "/dir/../")
+
+    def testUpDirInvalid(self):
+        body = Conn().get("GET /../ HTTP/1.0\n\n")
+        self.assertIsInvalid(body, "/../")
+
+    def testUpDirInvalidFancy(self):
+        body = Conn().get("GET /dir/../../ HTTP/1.0\n\n")
+        self.assertIsInvalid(body, "/dir/../../")
+
+if __name__ == '__main__':
+    unittest.main()
+    #print Conn().get("GET /xyz/../ HTTP/1.0")
+
+# vim:set ts=4 sw=4 et: