Working on unit tests.
[darkhttpd] / devel / test.py
1 #!/usr/bin/env python
2 import unittest
3 import socket
4 import signal
5
6 class Conn:
7 def __init__(self):
8 self.port = 12346
9 self.s = socket.socket()
10 self.s.connect(("0.0.0.0", self.port))
11 # connect throws socket.error on connection refused
12
13 def get(self, req):
14 self.s.send(req)
15 ret = ""
16 while True:
17 signal.alarm(1)
18 r = self.s.recv(65536)
19 signal.alarm(0)
20 if r == "":
21 break
22 else:
23 ret += r
24 return ret
25
26 class TestCases(unittest.TestCase):
27 def assertContains(self, body, *strings):
28 for s in strings:
29 self.assertTrue(s in body,
30 msg="expected %s in %s"%(repr(s), repr(body)))
31
32 def assertIsIndex(self, body, path):
33 self.assertContains(body,
34 "<title>%s</title>\n"%path,
35 "<h1>%s</h1>\n"%path,
36 '<a href="..">..</a>/',
37 'Generated by darkhttpd')
38
39 def assertIsInvalid(self, body, path):
40 self.assertContains(body,
41 "<title>400 Bad Request</title>",
42 "<h1>Bad Request</h1>\n",
43 "You requested an invalid URI: %s\n"%path,
44 'Generated by darkhttpd')
45
46 # FIXME: failing
47 #def testIndex_HTTP_0_9(self):
48 # body = Conn().get("GET /\n\n")
49 # self.assertIsIndex(body)
50
51 def testIndex_HTTP_1_0(self):
52 body = Conn().get("GET / HTTP/1.0\n\n")
53 self.assertIsIndex(body, "/")
54
55 def testUpDirValid(self):
56 body = Conn().get("GET /dir/../ HTTP/1.0\n\n")
57 self.assertIsIndex(body, "/dir/../")
58
59 def testUpDirInvalid(self):
60 body = Conn().get("GET /../ HTTP/1.0\n\n")
61 self.assertIsInvalid(body, "/../")
62
63 def testUpDirInvalidFancy(self):
64 body = Conn().get("GET /dir/../../ HTTP/1.0\n\n")
65 self.assertIsInvalid(body, "/dir/../../")
66
67 if __name__ == '__main__':
68 unittest.main()
69 #print Conn().get("GET /xyz/../ HTTP/1.0")
70
71 # vim:set ts=4 sw=4 et: