We can't sensibly unit test pages withhout firing up a server.

We've just added functionality to do this ourselves, so rip out the old stuff.
This commit is contained in:
Aldo Cortesi 2012-06-07 16:35:54 +12:00
parent 26c8437e88
commit 9ae53aecd0
3 changed files with 113 additions and 139 deletions

View File

@ -3,104 +3,6 @@ import tornado.web, tornado.template, tornado.ioloop, tornado.httpserver
import rparse, utils, version
class APILog(tornado.web.RequestHandler):
def get(self):
self.write(
dict(
d = self.application.get_log()
)
)
class APILogClear(tornado.web.RequestHandler):
def post(self):
self.application.clear_log()
self.write("OK")
class APIShutdown(tornado.web.RequestHandler):
def post(self):
tornado.ioloop.IOLoop.instance().stop()
self.write("OK")
class APIInfo(tornado.web.RequestHandler):
def get(self):
self.write(
dict(
version = version.IVERSION
)
)
class _Page(tornado.web.RequestHandler):
def render(self, name, **kwargs):
tornado.web.RequestHandler.render(self, name + ".html", **kwargs)
class Index(_Page):
name = "index"
section = "main"
def get(self):
self.render(self.name, section=self.section, spec="")
class Preview(_Page):
name = "preview"
section = "main"
SANITY = 1024*1024
def get(self):
spec = self.get_argument("spec", None)
args = dict(
spec = spec,
section = self.section,
syntaxerror = None,
error = None
)
try:
r = rparse.parse(self.application.settings, spec)
except rparse.ParseException, v:
args["syntaxerror"] = str(v)
args["marked"] = v.marked()
return self.render(self.name, **args)
if r.length() > self.SANITY:
error = "Refusing to preview a response of %s bytes. This is for your own good."%r.length()
args["error"] = error
else:
d = utils.DummyRequest()
r.serve(d)
args["output"] = d.getvalue()
self.render(self.name, **args)
class Help(_Page):
name = "help"
section = "help"
def get(self):
self.render(self.name, section=self.section)
class Log(_Page):
name = "log"
section = "log"
def get(self):
self.render(self.name, section=self.section, log=self.application.log)
class OneLog(_Page):
name = "onelog"
section = "log"
def get(self, lid):
l = pprint.pformat(self.application.log_by_id(int(lid)))
self.render(self.name, section=self.section, alog=l, lid=lid)
class ClearLog(_Page):
def post(self):
self.application.clear_logs()
self.redirect("/log")
class Pathod(object):
def __init__(self, spec, application, request, **settings):
self.application, self.request, self.settings = application, request, settings
@ -262,3 +164,102 @@ def run(server):
tornado.ioloop.IOLoop.instance().start()
server.stop()
class APILog(tornado.web.RequestHandler):
def get(self):
self.write(
dict(
d = self.application.get_log()
)
)
class APILogClear(tornado.web.RequestHandler):
def post(self):
self.application.clear_log()
self.write("OK")
class APIShutdown(tornado.web.RequestHandler):
def post(self):
tornado.ioloop.IOLoop.instance().stop()
self.write("OK")
class APIInfo(tornado.web.RequestHandler):
def get(self):
self.write(
dict(
version = version.IVERSION
)
)
class _Page(tornado.web.RequestHandler):
def render(self, name, **kwargs):
tornado.web.RequestHandler.render(self, name + ".html", **kwargs)
class Index(_Page):
name = "index"
section = "main"
def get(self):
self.render(self.name, section=self.section, spec="")
class Preview(_Page):
name = "preview"
section = "main"
SANITY = 1024*1024
def get(self):
spec = self.get_argument("spec", None)
args = dict(
spec = spec,
section = self.section,
syntaxerror = None,
error = None
)
try:
r = rparse.parse(self.application.settings, spec)
except rparse.ParseException, v:
args["syntaxerror"] = str(v)
args["marked"] = v.marked()
return self.render(self.name, **args)
if r.length() > self.SANITY:
error = "Refusing to preview a response of %s bytes. This is for your own good."%r.length()
args["error"] = error
else:
d = utils.DummyRequest()
r.serve(d)
args["output"] = d.getvalue()
self.render(self.name, **args)
class Help(_Page):
name = "help"
section = "help"
def get(self):
self.render(self.name, section=self.section)
class Log(_Page):
name = "log"
section = "log"
def get(self):
self.render(self.name, section=self.section, log=self.application.log)
class OneLog(_Page):
name = "onelog"
section = "log"
def get(self, lid):
l = pprint.pformat(self.application.log_by_id(int(lid)))
self.render(self.name, section=self.section, alog=l, lid=lid)
class ClearLog(_Page):
def post(self):
self.application.clear_logs()
self.redirect("/log")

View File

@ -4,19 +4,6 @@ import pathod
IFACE = "127.0.0.1"
class PaThread(threading.Thread):
def __init__(self, q, app):
threading.Thread.__init__(self)
self.q = q
self.app = app
self.port = None
def run(self):
self.server, self.port = pathod.make_server(self.app, 0, IFACE, None)
self.q.put(self.port)
pathod.run(self.server)
class Daemon:
def __init__(self, staticdir=None, anchors=()):
self.app = pathod.make_app(staticdir=staticdir, anchors=anchors)
@ -33,3 +20,17 @@ class Daemon:
def shutdown(self):
requests.post("%s/api/shutdown"%self.urlbase)
class PaThread(threading.Thread):
def __init__(self, q, app):
threading.Thread.__init__(self)
self.q = q
self.app = app
self.port = None
# begin nocover
def run(self):
self.server, self.port = pathod.make_server(self.app, 0, IFACE, None)
self.q.put(self.port)
pathod.run(self.server)

View File

@ -31,41 +31,13 @@ class uApplication(libpry.AutoTree):
assert not a.log_by_id(0)
class uPages(libpry.AutoTree):
def dummy_page(self, path):
# A hideous, hideous kludge, but Tornado seems to have no more sensible
# way to do this.
a = pathod.PathodApp(staticdir=None)
for h in a.handlers[0][1]:
if h.regex.match(path):
klass = h.handler_class
r = httpserver.HTTPRequest("GET", path)
del r.connection
k = klass(a, r)
k._transforms = []
return k
def test_index(self):
page = self.dummy_page("/")
page.get()
assert "".join(page._write_buffer)
def test_help(self):
page = self.dummy_page("/help")
page.get()
assert "".join(page._write_buffer)
class u_make_server(libpry.AutoTree):
def test_simple(self):
app = pathod.PathodApp()
assert pathod.make_server(app, 0, "127.0.0.1", None)
tests = [
uApplication(),
#uPages(),
u_make_server()
]