2012-02-15 21:58:43 -07:00
#! /usr/bin/python3
import unittest
from subprocess import *
import os
2012-02-16 20:18:17 -07:00
def eris ( * args ) :
return Popen ( ( ' ../eris ' , ) + args ,
2012-02-15 21:58:43 -07:00
stdin = PIPE , stdout = PIPE , stderr = PIPE ,
env = { ' PROTO ' : ' TCP ' ,
' TCPREMOTEPORT ' : ' 5858 ' ,
' TCPREMOTEIP ' : ' 10.1.2.3 ' } )
2012-02-17 08:24:04 -07:00
class LinesTests ( unittest . TestCase ) :
def assertLinesEqual ( self , a , b ) :
self . assertSequenceEqual ( a . split ( b ' \n ' ) , b . split ( b ' \n ' ) )
2012-02-17 17:50:05 -07:00
class NewlineTests ( LinesTests ) :
def testBareNL ( self ) :
p = eris ( )
so , se = p . communicate ( b ' GET / HTTP/1.0 \n \n ' )
self . assertRegexpMatches ( so , b ' HTTP/1.0 200 OK \r \n Server: eris/2 \r \n Content-Type: text/html; charset=UTF-8 \r \n Content-Length: 6 \r \n Last-Modified: (Mon|Tue|Wed|Thu|Fri|Sat|Sun), .. (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) 2... ..:..:.. GMT \r \n \r \n james \n ' )
self . assertLinesEqual ( se , b ' 10.1.2.3 200 6 127.0.0.1 (null) (null) /index.html \n ' )
2012-02-17 08:24:04 -07:00
class ArgTests ( LinesTests ) :
2012-02-15 21:58:43 -07:00
def check_index ( self , * args ) :
2012-02-16 20:18:17 -07:00
p = eris ( * args )
2012-02-15 21:58:43 -07:00
so , se = p . communicate ( b ' GET / HTTP/1.0 \r \n \r \n ' )
2012-02-16 20:22:38 -07:00
self . assertRegexpMatches ( so , b ' HTTP/1.0 200 OK \r \n Server: eris/2 \r \n Content-Type: text/html; charset=UTF-8 \r \n Content-Length: 6 \r \n Last-Modified: (Mon|Tue|Wed|Thu|Fri|Sat|Sun), .. (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) 2... ..:..:.. GMT \r \n \r \n james \n ' )
2012-02-17 08:24:04 -07:00
self . assertLinesEqual ( se , b ' 10.1.2.3 200 6 127.0.0.1 (null) (null) /index.html \n ' )
2012-02-15 21:58:43 -07:00
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 ' )
2012-02-16 20:50:42 -07:00
def testPortAppend ( self ) :
p = eris ( ' -p ' )
so , se = p . communicate ( b ' GET / HTTP/1.0 \r \n \r \n ' )
self . assertRegexpMatches ( so , b ' HTTP/1.0 200 OK \r \n Server: eris/2 \r \n Content-Type: text/html; charset=UTF-8 \r \n Content-Length: 6 \r \n Last-Modified: (Mon|Tue|Wed|Thu|Fri|Sat|Sun), .. (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) 2... ..:..:.. GMT \r \n \r \n james \n ' )
2012-02-17 08:24:04 -07:00
self . assertLinesEqual ( se , b ' 10.1.2.3 200 6 127.0.0.1:80 (null) (null) /index.html \n ' )
2012-02-16 20:50:42 -07:00
2012-02-15 21:58:43 -07:00
def testBadArgs ( self ) :
" Make sure bad arguments actually fail "
self . assertRaises ( AssertionError , self . check_index , ' -Z ' )
2012-02-17 08:24:04 -07:00
class BasicTests ( LinesTests ) :
2012-02-15 21:58:43 -07:00
args = [ ]
def setUp ( self ) :
2012-02-16 20:18:17 -07:00
self . p = eris ( * self . args )
2012-02-15 21:58:43 -07:00
def tearDown ( self ) :
del self . p
def get ( self , path , host ) :
h = ' GET %s HTTP/1.0 \r \n Host: %s \r \n \r \n ' % ( path , host )
so , se = self . p . communicate ( h . encode ( ' utf-8 ' ) )
return ( so , se )
2012-02-16 12:05:02 -07:00
def post ( self , path , host , formdata ) :
h = ' POST %s HTTP/1.0 \r \n Host: %s \r \n Content-Type: application/x-www-form-urlencoded \r \n Content-Length: %d \r \n \r \n %s ' % ( path , host , len ( formdata ) , formdata )
so , se = self . p . communicate ( h . encode ( ' utf-8 ' ) )
return ( so , se )
2012-02-15 21:58:43 -07:00
class DirTests ( BasicTests ) :
args = [ ' -d ' ]
def testRootDir ( self ) :
2012-02-16 12:05:02 -07:00
so , se = self . get ( ' / ' , ' empty ' )
2012-02-17 08:24:04 -07:00
self . assertLinesEqual ( so , b ' HTTP/1.0 200 OK \r \n Server: eris/2 \r \n Connection: close \r \n Content-Type: text/html; charset=utf-8 \r \n \r \n <h3>Directory Listing: /</h3> \n <pre> \n </pre> \n ' )
self . assertLinesEqual ( se , b ' 10.1.2.3 200 32 empty (null) (null) / \n ' )
2012-02-16 12:05:02 -07:00
def testNoTrailingSlash ( self ) :
so , se = self . get ( ' /files ' , ' default ' )
2012-02-17 08:24:04 -07:00
self . assertLinesEqual ( so , b ' HTTP/1.0 404 Not Found \r \n Connection: close \r \n Content-Length: 50 \r \n Content-Type: text/html \r \n \r \n <title>Not Found</title>No such file or directory. ' )
self . assertLinesEqual ( se , b ' 10.1.2.3 404 0 default (null) (null) /files \n ' )
2012-02-16 12:05:02 -07:00
def testFiles ( self ) :
so , se = self . get ( ' /files/ ' , ' default ' )
2012-02-17 08:24:04 -07:00
self . assertLinesEqual ( so , b ' HTTP/1.0 200 OK \r \n Server: eris/2 \r \n Connection: close \r \n Content-Type: text/html; charset=utf-8 \r \n \r \n <h3>Directory Listing: /files/</h3> \n <pre> \n <a href= " / " >Parent directory</a> \n [TXT] <a href= " 1.txt " >1.txt</a> \n </pre> \n ' )
self . assertLinesEqual ( se , b ' 10.1.2.3 200 110 default (null) (null) /files/ \n ' )
2012-02-15 21:58:43 -07:00
2012-02-16 12:05:02 -07:00
class CGITests ( BasicTests ) :
args = [ ' -c ' ]
2012-02-17 08:24:04 -07:00
maxDiff = None
2012-02-16 12:05:02 -07:00
def testSet ( self ) :
so , se = self . get ( ' /cgi/set.cgi ' , ' default ' )
2012-02-17 08:24:04 -07:00
self . assertLinesEqual ( so , b ' HTTP/1.0 200 OK \r \n Server: eris/2 \r \n Pragma: no-cache \r \n Connection: close \r \n Content-Type: text/plain \r \n \r \n GATEWAY_INTERFACE:CGI/1.1 \n SERVER_PROTOCOL:HTTP/1.0 \n SERVER_SOFTWARE:eris/2 \n SERVER_NAME:default \n SERVER_PORT:80 \n REQUEST_METHOD:GET \n REQUEST_URI:/cgi/set.cgi \n SCRIPT_NAME:/cgi/set.cgi \n REMOTE_ADDR:10.1.2.3 \n REMOTE_PORT:5858 \n ' )
self . assertLinesEqual ( se , b ' 10.1.2.3 200 242 default (null) (null) /cgi/set.cgi \n ' )
2012-02-16 12:05:02 -07:00
def testSetArgs ( self ) :
so , se = self . get ( ' /cgi/set.cgi?a=1&b=2&c=3 ' , ' default ' )
2012-02-17 08:24:04 -07:00
self . assertLinesEqual ( so , b ' HTTP/1.0 200 OK \r \n Server: eris/2 \r \n Pragma: no-cache \r \n Connection: close \r \n Content-Type: text/plain \r \n \r \n GATEWAY_INTERFACE:CGI/1.1 \n SERVER_PROTOCOL:HTTP/1.0 \n SERVER_SOFTWARE:eris/2 \n SERVER_NAME:default \n SERVER_PORT:80 \n REQUEST_METHOD:GET \n REQUEST_URI:/cgi/set.cgi \n SCRIPT_NAME:/cgi/set.cgi \n REMOTE_ADDR:10.1.2.3 \n REMOTE_PORT:5858 \n QUERY_STRING:a=1&b=2&c=3 \n ' )
self . assertLinesEqual ( se , b ' 10.1.2.3 200 267 default (null) (null) /cgi/set.cgi \n ' )
2012-02-16 12:05:02 -07:00
def testPost ( self ) :
so , se = self . post ( ' /cgi/set.cgi ' , ' default ' , ' a=1&b=2&c=3 ' )
2012-02-17 08:24:04 -07:00
self . assertLinesEqual ( se , b ' 10.1.2.3 200 330 default (null) (null) /cgi/set.cgi \n ' )
self . assertLinesEqual ( so , b ' HTTP/1.0 200 OK \r \n Server: eris/2 \r \n Pragma: no-cache \r \n Connection: close \r \n Content-Type: text/plain \r \n \r \n GATEWAY_INTERFACE:CGI/1.1 \n SERVER_PROTOCOL:HTTP/1.0 \n SERVER_SOFTWARE:eris/2 \n SERVER_NAME:default \n SERVER_PORT:80 \n REQUEST_METHOD:POST \n REQUEST_URI:/cgi/set.cgi \n SCRIPT_NAME:/cgi/set.cgi \n REMOTE_ADDR:10.1.2.3 \n REMOTE_PORT:5858 \n CONTENT_TYPE:application/x-www-form-urlencoded \n CONTENT_LENGTH:11 \n Form data: a=1&b=2&c=3 ' )
# XXX: Test posting to static html with keepalive
# (it probably won't discard content-length octets)
2012-02-16 12:05:02 -07:00
2012-02-15 21:58:43 -07:00
unittest . main ( )
2012-02-16 12:05:02 -07:00