如果不给TestApp提供app_errors参数,则TestApp将可能会抛出难以阅读的异常信息 当app.py发生错误时,TestApp可能会对app.py发起多次请求 TestApp程序会自动处理cookie, 当要求"assert sh.session.is_login"时可以用AppTest 提供的各种函数请求app程序,也可以使用proxyDo模拟用户 ''' import unittest import StringIO import web from paste.fixture import TestApp from app import app as _app import site_helper as sh from model import ModelData app_errors = StringIO.StringIO() app = TestApp(_app.wsgifunc(), extra_environ={'wsgi.errors': app_errors}) # 用于默认注册\登录的用户信息 default_user = dict(email='*****@*****.**', password='******', name='note') # 因为app在post时不能准确地向webpy程序传递参数,这可能是一个bug, 为paste.fixture hack def hackForInputs(f): def newInputs(): if web.ctx.env.get('wsgi.input.note.post.hack', None): return web.ctx.env.get('wsgi.input.note.post.hack') else: return f() return newInputs sh.inputs = hackForInputs(sh.inputs) class AppTest(unittest.TestCase):
如果不给TestApp提供app_errors参数,则TestApp将可能会抛出难以阅读的异常信息 当app.py发生错误时,TestApp可能会对app.py发起多次请求 TestApp程序会自动处理cookie, 当要求"assert sh.session.is_login"时可以用AppTest 提供的各种函数请求app程序,也可以使用proxyDo模拟用户 ''' import unittest import StringIO import web from paste.fixture import TestApp from app import app as _app import site_helper as sh from model import ModelData app_errors = StringIO.StringIO() app = TestApp(_app.wsgifunc(), extra_environ={'wsgi.errors': app_errors}) # 用于默认注册\登录的用户信息 default_user = dict(email='*****@*****.**', password='******', name='zarkpy') # 因为app在post时不能准确地向webpy程序传递参数,这可能是一个bug, 为paste.fixture hack def hackForInputs(f): def newInputs(): if not web.ctx.has_key('env'): return {} if web.ctx.env.get('wsgi.input.zarkpy.post.hack', None): return web.ctx.env.get('wsgi.input.zarkpy.post.hack') else: return f() return newInputs sh.inputs = hackForInputs(sh.inputs)
import os import leancloud from gevent.pywsgi import WSGIServer from geventwebsocket.handler import WebSocketHandler from web.httpserver import StaticMiddleware from app import app APP_ID = os.environ['LC_APP_ID'] MASTER_KEY = os.environ['LC_APP_MASTER_KEY'] PORT = int(os.environ['LC_APP_PORT']) leancloud.init(APP_ID, master_key=MASTER_KEY) engine = app.wsgifunc() if os.environ.get('LC_HTTPS_ON', 0): engine = leancloud.HttpsRedirectMiddleware(engine) engine = leancloud.Engine(engine) engine = StaticMiddleware(engine) application = engine if __name__ == '__main__': # 只在本地开发环境执行的代码 app.debug = True server = WSGIServer(('localhost', PORT), application, handler_class=WebSocketHandler) #server = WSGIServer(('localhost', PORT), application) server.serve_forever()
import os, sys sys.path.append(os.path.dirname(__file__)) from app import app application = app.wsgifunc()