Skip to content

Commit

Permalink
add a test layer that sets up a server on a known port
Browse files Browse the repository at this point in the history
  • Loading branch information
benji-york committed Jun 5, 2006
1 parent 8b653f2 commit 68b86c6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
38 changes: 38 additions & 0 deletions ftests/reallayer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from zope.app.server.main import setup, load_options, run
import ThreadedAsync.LoopCallback
import threading
from zope.testbrowser.realproxy import TARGET_PORT

class TestbrowserRealClass:

__bases__ = ()

def __init__(self):
self.__name__ = self.__class__.__name__[:-5]
self.__module__ = self.__class__.__module__

def setUp(self):
self.startZope()

def tearDown(self):
self.stopZope()

def startZope(self, fg=None):
"""start Zope in a daemon thread"""
def go():
# force the server to run on a known port
args = ['-X', 'server/address=%s' % TARGET_PORT,
'-X', 'site-definition=ftesting.zcml']
setup(load_options(args))
run()

self.zope_thread = threading.Thread(target=go)
self.zope_thread.setDaemon(True)
self.zope_thread.start()

def stopZope(self):
"""tell Zope to stop and wait for it to do so"""
ThreadedAsync.LoopCallback.exit_status = 0
self.zope_thread.join()

TestbrowserReal = TestbrowserRealClass()
2 changes: 2 additions & 0 deletions ftests/testdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
import unittest
import doctest
from zope.app.testing.functional import FunctionalDocFileSuite
from reallayer import TestbrowserReal

def test_suite():
flags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
readme = FunctionalDocFileSuite('../README.txt', optionflags=flags)
real = doctest.DocFileSuite('../real.txt', optionflags=flags)
real.level = 2
real.layer = TestbrowserReal
wire = FunctionalDocFileSuite('../over_the_wire.txt', optionflags=flags)
wire.level = 2
return unittest.TestSuite((readme, wire, real))
Expand Down
19 changes: 8 additions & 11 deletions real.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ the testbrowser API. First, instantiate a Browser object.
>>> from zope.testbrowser.real import Browser
>>> browser = Browser()

We can open pages.
We can open pages and look at their contents.

>>> browser.open('http://localhost/')
>>> browser.getLink('buddies')
<Link text='buddies' url=u'http://localhost:8000/buddies'>
>>> browser.contents
u'...</head>...'

>>> browser.getLink('[top]', index=0)
<Link text='[top]' url=u'http://localhost:23123/@@SelectedManagementView.html'>

If a link doesn't exist, we get an exception.

Expand All @@ -23,18 +26,12 @@ If a link doesn't exist, we get an exception.

Links can be clicked.

>>> browser.getLink('buddies').click()
>>> browser.getLink('[top]', index=0).click()

We can retrieve the current address.

>>> browser.url
u'http://localhost:8000/buddies'

We can also view the contents of the page.

>>> browser.getLink('brian').click()
>>> browser.contents
u'...</head>...'
u'http://localhost:23123/'

When we're done with the browser we have to close it.

Expand Down
12 changes: 8 additions & 4 deletions realproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

base_dir = os.path.dirname(__file__)
allowed_resources = ['MochiKit', 'shim.js', 'commands.js', 'start.html']
PROXY_PORT = 8000

PROXY_PORT = 23123
PROXY_HOST = '127.0.0.1'
TARGET_PORT = 23124

class Constant(object):
def __init__(self, name):
Expand All @@ -26,7 +29,7 @@ def __str__(self):
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
# server_version = "TinyHTTPProxy/" + __version__
rbufsize = 0
remote_host = 'localhost:8080' # TODO needs to be configurable
remote_host = 'localhost:%d' % TARGET_PORT

def __init__(self, request, client_address, server):
self.command_queue = server.command_queue
Expand Down Expand Up @@ -195,8 +198,9 @@ def process_request_thread(self, request, client_address):

class ServerManager(object):
def __init__(self):
self.host = PROXY_HOST
self.port = PROXY_PORT
self.server = HttpServer(('127.0.0.1', self.port), RequestHandler)
self.server = HttpServer((self.host, self.port), RequestHandler)

def start(self):
self.server_thread = threading.Thread(
Expand All @@ -206,7 +210,7 @@ def start(self):

def stop(self):
self.executeCommand('stop')
conn = httplib.HTTPConnection('localhost:%d' % self.port)
conn = httplib.HTTPConnection('%s:%d' % (self.host, self.port))
conn.request('NOOP', '/')
conn.getresponse()

Expand Down

0 comments on commit 68b86c6

Please sign in to comment.