root/mail-onna-stick/trunk/tests/twill-tests/test-http-server.py

Revision 266, 1.7 kB (checked in by t, 11 years ago)

fixed problem with Web server trying to bind an already bound port.

Line 
1 """
2 Test both http access and WSGI-intercept access to the basic Web site.
3 """
4
5 import os, time, sys
6 import twill.unit
7
8 import twilltestlib
9 twilltestlib.add_lib_dirs()
10
11 import mos, mosweb
12
13 ###
14
15 def setup_module():
16     """
17     setup: change into the test directory.
18     """
19     twilltestlib.cd_testdir()
20     mos.config.debug_on()
21
22 def teardown_module():
23     """
24     teardown: change back out of the test directory.
25     """
26     twilltestlib.pop_testdir()
27     mos.config.debug_off()
28
29 def test_ok():
30     "Can we start up & connect to the HTTP server?"
31
32     # pick a port to attach to; it should be one that's not already
33     # taken!
34     BASE_PORT=8090
35
36     i = 0
37     while i < 100:
38         if twilltestlib.try_bind_port(BASE_PORT + i):
39             break
40         i += 1
41
42     PORT = BASE_PORT + i
43
44     ### build the command to start the server
45    
46     def run_server_fn():
47         mosweb.run(port=PORT)
48
49     ### build the testinfo object
50     test_info = twill.unit.TestInfo('scripts/test-ok.twill',
51                                     run_server_fn, PORT,
52                                     sleep=0.5)
53
54     ### run test!
55     pid = os.fork()
56     if pid is 0:
57         try:
58             try:
59                 twill.unit.run_child_process(test_info)
60             except SystemExit:
61                 pass
62         finally:
63             mosweb.cleanup()
64         os._exit(0)
65     else:
66         time.sleep(0.5)                 # allow for cherrypy setup
67        
68         url = test_info.get_url()
69         print url
70         twill.commands.go(url)
71        
72         try:
73             twill.commands.go(url + 'quit')
74         except Exception, e:
75             pass
76
77         time.sleep(0.5)                 # allow for cherrypy teardown
Note: See TracBrowser for help on using the browser.