Add some tests

This commit is contained in:
Neale Pickett 2012-02-15 21:58:43 -07:00
parent c8798ad669
commit bc73690a03
6 changed files with 73 additions and 2 deletions

View File

@ -1,3 +1,11 @@
2.0:
Major modifications
Replace libowfat with libc
Replace buffer_1 and buffer_2 with stdio
Fix directory listing of / SEGV
Replace compile-time options with command-line ones
Add regression test suite
1.11:
Fix if-modified-since date parsing
Make text content-types use charset=UTF-8

View File

@ -4,5 +4,8 @@ CFLAGS = -DFNORD='"fnord/$(VERSION)"' -Wall -Werror
all: fnord
test: fnord
cd tests && python3 ./test.py
clean:
rm -f *.[oa] fnord

View File

@ -1093,7 +1093,7 @@ handledirlist(const char *origurl)
size = 32 + nl;
fputs("HTTP/1.0 200 OK\r\nServer: " FNORD
"\r\nConnection: close\r\n", stdout);
fputs("Content-Type: text/html\r\n", stdout);
fputs("Content-Type: text/html; charset=utf-8\r\n", stdout);
fputs("\r\n<h3>Directory Listing: /", stdout);
hdl_encode_html(nurl, nl);
fputs("</h3>\n<pre>\n", stdout);
@ -1425,7 +1425,7 @@ main(int argc, char *argv[], const char *const *envp)
redirect = 1;
break;
default:
fprintf(stderr, "%d Usage: %s [-d] [-r]\n", opt, argv[0]);
fprintf(stderr, "Usage: %s [-c] [-d] [-r]\n", argv[0]);
return 69;
}
}

1
tests/default/index.html Normal file
View File

@ -0,0 +1 @@
james

0
tests/moo:80/.empty Normal file
View File

59
tests/test.py Executable file
View File

@ -0,0 +1,59 @@
#! /usr/bin/python3
import unittest
from subprocess import *
import os
def fnord(*args):
return Popen(('../fnord',) + args,
stdin=PIPE, stdout=PIPE, stderr=PIPE,
env={'PROTO': 'TCP',
'TCPREMOTEPORT': '5858',
'TCPREMOTEIP': '10.1.2.3'})
class ArgTests(unittest.TestCase):
def check_index(self, *args):
p = fnord(*args)
so, se = p.communicate(b'GET / HTTP/1.0\r\n\r\n')
self.assertEqual(so, b'HTTP/1.0 200 OK\r\nServer: fnord/2.0\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: 6\r\nLast-Modified: Thu, 16 Feb 2012 04:08:57 GMT\r\n\r\njames\n')
self.assertEqual(se, b'10.1.2.3 200 6 127.0.0.1:80 (null) (null) /index.html\n')
def testArgs(self):
"Make sure index.html is the same for all arguments"
self.check_index()
self.check_index('-d')
self.check_index('-r')
self.check_index('-c')
def testBadArgs(self):
"Make sure bad arguments actually fail"
self.assertRaises(AssertionError, self.check_index, '-Z')
class BasicTests(unittest.TestCase):
args = []
def setUp(self):
self.p = fnord(*self.args)
def tearDown(self):
del self.p
def get(self, path, host):
h = 'GET %s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host)
so, se = self.p.communicate(h.encode('utf-8'))
return (so, se)
class DirTests(BasicTests):
args = ['-d']
def testRootDir(self):
so, se = self.get('/', 'moo')
self.assertEqual(so, b'HTTP/1.0 200 OK\r\nServer: fnord/2.0\r\nConnection: close\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<h3>Directory Listing: /</h3>\n<pre>\n</pre>\n')
self.assertEqual(se, b'10.1.2.3 200 32 moo:80 (null) (null) /\n')
unittest.main()