3 """Server environment for testing simplewebserver (command line web server)
4 This uses the basic environment from test_create and runs a web server in a separate thread
5 The top-level tests are then in test_client / test_ie / test_mozilla etc
6 These can then be layered on top of this server"""
8 from Pootle
import test_create
9 from jToolkit
.web
import simplewebserver
10 from jToolkit
import errors
14 class TestCmdlineServer(test_create
.NoReuse
):
15 # this is called from setup_class in test_create.TestCreate
16 def setup_webserver(self
):
17 """setup the webserver that will be used for the tests"""
18 print "setting up web server"
19 webserverclass
= simplewebserver
.jToolkitHTTPServer(simplewebserver
.ThreadedHTTPServer
)
20 options
= simplewebserver
.WebOptionParser().parse_args([])[0]
22 options
.servertype
= 'standard'
23 errorhandler
= errors
.ConsoleErrorHandler()
24 webserver
= webserverclass(options
, errorhandler
)
27 def setup_method(self
, method
):
28 """starts a new simplewebserver in a separate thread"""
30 test_create
.NoReuse
.setup_method(self
, method
)
32 print "exception in test_create setup_method:", e
33 test_create
.NoReuse
.teardown_method(self
, method
)
35 print "finished setup_method phase 1"
36 # self.webserver.options.port = self.port
37 self
.baseaddress
= "http://%s:%d/" % (self
.webserver
.hostname
, self
.webserver
.port
)
38 ThreadClass
= self
.webserver
.ThreadClass
39 self
.webserverthread
= ThreadClass(target
= simplewebserver
.run
, name
="webserver", args
=(self
.server
, self
.webserver
.options
))
40 self
.webserverthread
.start()
41 # wait until it actually is started
45 while not hasattr(self
.webserver
, "stop"):
49 raise RuntimeError("webserver failed to start in %0.1f seconds" % maxwait
)
51 def teardown_method(self
, method
):
52 """close the web server for this method"""
54 self
.server
.sessioncache
.clear()
55 self
.webserver
.setstop(True)
56 # ping the webserver to make it stop
60 while self
.webserverthread
.isAlive():
62 if not self
.webserverthread
.isAlive():
64 # if the webserver is sleeping a request should wake it up...
66 # x = urllib2.urlopen(self.baseaddress)
67 # except Exception, e:
71 if waited
>= max_wait
:
72 raise RuntimeError("webserver failed to stop in %0.1f seconds" % (waited
))
74 test_create
.NoReuse
.teardown_method(self
, method
)