本文整理汇总了Python中webtest.TestApp类的典型用法代码示例。如果您正苦于以下问题:Python TestApp类的具体用法?Python TestApp怎么用?Python TestApp使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TestApp类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_enforce_https_wrong
def test_enforce_https_wrong(self):
config = copy.deepcopy(SHARED_DEFAULTS)
config["enforce_https"] = True
application = RoutingApplication(config)
app = TestApp(application, extra_environ={"REMOTE_ADDR": "127.0.0.1"})
app.get("/", status=406)
app.post("/connect", status=406)
示例2: IntegrationTests
class IntegrationTests(unittest.TestCase):
def setUp(self):
from webtest import TestApp
from pyramid.paster import get_app
app = get_app('development.ini')
self.app = TestApp(app)
def tearDown(self):
testing.tearDown()
def test_login_view(self):
self.app.authorization = ('Basic', ('user', 'password'))
response = self.app.get('/login')
self.assertEqual(response.status_int, 200)
def test_logout_view(self):
# run login to start session
self.app.authorization = ('Basic', ('user', 'password'))
self.app.get('/login')
response = self.app.get('/logout')
self.assertEqual(response.status_int, 200)
def test_logout_not_loggedin(self):
# expect 403 when trying to log out again
from pyramid.httpexceptions import HTTPForbidden
response = self.app.get('/logout', expect_errors=True)
self.assertEqual(response.status_int, 401)
示例3: GetCompletions_ClangCompleter_ForceSemantic_OnlyFileteredCompletions_test
def GetCompletions_ClangCompleter_ForceSemantic_OnlyFileteredCompletions_test():
app = TestApp( handlers.app )
contents = """
int main()
{
int foobar;
int floozar;
int gooboo;
int bleble;
fooar
}
"""
# 0-based line and column!
completion_data = BuildRequest( filepath = '/foo.cpp',
filetype = 'cpp',
force_semantic = True,
contents = contents,
line_num = 8,
column_num = 7,
start_column = 7,
query = 'fooar',
compilation_flags = ['-x', 'c++'] )
results = app.post_json( '/completions', completion_data ).json
assert_that( results,
contains_inanyorder( CompletionEntryMatcher( 'foobar' ),
CompletionEntryMatcher( 'floozar' ) ) )
示例4: _RunCompleterCommand_Message_Clang
def _RunCompleterCommand_Message_Clang(filename, test, command):
contents = open( PathToTestFile( filename ) ).read()
app = TestApp( handlers.app )
common_args = {
'completer_target' : 'filetype_default',
'command_arguments' : command,
'compilation_flags' : ['-x',
'c++',
'-std=c++11'],
'line_num' : 10,
'column_num' : 3,
'contents' : contents,
'filetype' : 'cpp'
}
args = test[0]
expected = test[1];
request = common_args
request.update( args )
request_data = BuildRequest( **request )
eq_( {'message': expected},
app.post_json( '/run_completer_command', request_data ).json )
示例5: test_create_hosted_zone_xml
def test_create_hosted_zone_xml():
app = TestApp(api.api)
request = open('createhostedzone.xml', 'r').read()
response = app.post('/hostedzone', request)
#reply = open('createhostedzone_response.xml', 'r').read()
assert response.status == '201 Created'
print response.body
示例6: test_simple_generic
def test_simple_generic(self):
class RootController(object):
@expose(generic=True)
def index(self):
pass
@index.when(method="POST", template="json")
def do_post(self):
return dict(result="POST")
@index.when(method="GET")
def do_get(self):
return "GET"
app = TestApp(Pecan(RootController()))
r = app.get("/")
assert r.status_int == 200
assert r.body == "GET"
r = app.post("/")
assert r.status_int == 200
assert r.body == dumps(dict(result="POST"))
r = app.get("/do_get", status=404)
assert r.status_int == 404
示例7: test_pull_task
def test_pull_task():
test_app = TestApp(app)
session = create_session()
task1 = Task()
task1.uuid = 'de305d54-75b4-431b-adb2-eb6b9e546018'
task1.test_id = 'de305d54-75b4-431b-adb2-eb6b9e546018'
task1.claimed = datetime.utcnow()
task1.data = json.dumps({'wait_time': 123})
session.add(task1)
task2 = Task()
task2.uuid = 'de305d54-75b4-431b-adb2-eb6b9e546019'
task2.test_id = 'de305d54-75b4-431b-adb2-eb6b9e546019'
task2.claimed = datetime.utcnow()
task2.completed = datetime.utcnow()
task2.result_data = json.dumps({'result': 'epic success'})
session.add(task2)
task3 = Task()
task3.uuid = 'de305d54-75b4-431b-adb2-eb6b9e546020'
task3.test_id = 'de305d54-75b4-431b-adb2-eb6b9e546020'
task3.claimed = datetime.utcnow()
task3.failed = datetime.utcnow()
task3.error = 'unknown error'
session.add(task3)
session.commit()
test_app.get('/task/de305d54-75b4-431b-adb2-eb6b9e546018')
test_app.get('/task/de305d54-75b4-431b-adb2-eb6b9e546019')
test_app.get('/task/de305d54-75b4-431b-adb2-eb6b9e546020')
示例8: RunCompleterCommand_GoTo_Clang_ZeroBasedLineAndColumn_test
def RunCompleterCommand_GoTo_Clang_ZeroBasedLineAndColumn_test():
app = TestApp( handlers.app )
contents = """
struct Foo {
int x;
int y;
char c;
};
int main()
{
Foo foo;
return 0;
}
"""
goto_data = BuildRequest( completer_target = 'filetype_default',
command_arguments = ['GoToDefinition'],
compilation_flags = ['-x', 'c++'],
line_num = 10,
column_num = 3,
contents = contents,
filetype = 'cpp' )
eq_( {
'filepath': '/foo',
'line_num': 2,
'column_num': 8
},
app.post_json( '/run_completer_command', goto_data ).json )
示例9: ImperativeIncludeConfigurationTest
class ImperativeIncludeConfigurationTest(unittest.TestCase):
def setUp(self):
from pyramid.config import Configurator
config = Configurator()
from pyramid.tests.pkgs.includeapp1.root import configure
configure(config)
app = config.make_wsgi_app()
from webtest import TestApp
self.testapp = TestApp(app)
self.config = config
def tearDown(self):
self.config.end()
def test_root(self):
res = self.testapp.get('/', status=200)
self.assertTrue(b'root' in res.body)
def test_two(self):
res = self.testapp.get('/two', status=200)
self.assertTrue(b'two' in res.body)
def test_three(self):
res = self.testapp.get('/three', status=200)
self.assertTrue(b'three' in res.body)
示例10: test_disconnect_hooks_multiple_listener
def test_disconnect_hooks_multiple_listener(self):
hook1_has_been_called = []
def hook1_listener():
hook1_has_been_called.append(True)
hook2_has_been_called = []
def hook2_listener():
hook2_has_been_called.append(True)
class RootController(TGController):
@expose()
def test(self):
tg.hooks.notify('custom_hook', controller=RootController.test)
return 'HI!'
conf = AppConfig(minimal=True, root_controller=RootController())
tg.hooks.register('custom_hook', hook1_listener)
tg.hooks.register('custom_hook', hook2_listener)
conf.package = PackageWithModel()
app = conf.make_wsgi_app()
app = TestApp(app)
app.get('/test')
app.get('/test')
tg.hooks.disconnect('custom_hook', hook2_listener)
app.get('/test')
# Disconnecting an unregistered hook should do nothing.
tg.hooks.disconnect('unregistered', hook1_listener)
assert len(hook1_has_been_called) == 3, hook1_has_been_called
assert len(hook2_has_been_called) == 2, hook2_has_been_called
示例11: TestResource
class TestResource(unittest.TestCase):
def setUp(self):
from pyramid.renderers import JSONP
self.config = testing.setUp()
self.config.add_renderer("jsonp", JSONP(param_name="callback"))
self.config.include("cornice")
self.config.scan("cornice.tests.test_resource")
self.app = TestApp(CatchErrors(self.config.make_wsgi_app()))
def tearDown(self):
testing.tearDown()
def test_basic_resource(self):
self.assertEquals(self.app.get("/users").json, {"status": "ok", "result": {"users": [1, 2]}})
self.assertEquals(self.app.get("/users/1").json, {"status": "ok", "result": {"name": "gawel"}})
resp = self.app.get("/users/1?callback=test")
self.assertEquals(resp.body, 'test({"status": "ok", "result": {"name": "gawel"}})', resp.body)
def test_accept_headers(self):
# the accept headers should work even in case they're specified in a
# resource method
self.assertEquals(
self.app.post("/users", headers={"Accept": "text/json"}, params=json.dumps({"test": "yeah"})).json,
{"status": "ok", "result": {"test": "yeah"}},
)
示例12: test_config_hooks
def test_config_hooks(self):
class RootController(TGController):
@expose()
def test(self):
return 'HI!'
visited_hooks = []
def before_config_hook(app):
visited_hooks.append('before_config')
return app
def after_config_hook(app):
visited_hooks.append('after_config')
return app
def configure_new_app_hook(app):
assert isinstance(app, TGApp)
visited_hooks.append('configure_new_app')
conf = AppConfig(minimal=True, root_controller=RootController())
conf.register_hook('before_config', before_config_hook)
conf.register_hook('after_config', after_config_hook)
conf.register_hook('configure_new_app', configure_new_app_hook)
app = conf.make_wsgi_app()
app = TestApp(app)
assert 'HI!' in app.get('/test')
assert 'before_config' in visited_hooks
assert 'after_config' in visited_hooks
assert 'configure_new_app' in visited_hooks
示例13: test_variable_path_parameter_required_with_default
def test_variable_path_parameter_required_with_default():
config = setup()
class app(morepath.App):
testing_config = config
class Model(object):
def __init__(self, id):
self.id = id
@app.path(model=Model, path='', required=['id'])
def get_model(id='b'):
return Model(id)
@app.view(model=Model)
def default(self, request):
return "View: %s" % self.id
@app.view(model=Model, name='link')
def link(self, request):
return request.link(self)
config.commit()
c = Client(app())
response = c.get('/?id=a')
assert response.body == b"View: a"
response = c.get('/', status=400)
示例14: test_extra_predicates
def test_extra_predicates():
config = setup()
app = App(testing_config=config)
@app.path(path='{id}')
class Model(object):
def __init__(self, id):
self.id = id
@app.view(model=Model, name='foo', id='a')
def get_a(self, request):
return 'a'
@app.view(model=Model, name='foo', id='b')
def get_b(self, request):
return 'b'
@app.predicate(name='id', order=2, default='')
def get_id(self, request):
return self.id
config.commit()
c = Client(app)
response = c.get('/a/foo')
assert response.body == 'a'
response = c.get('/b/foo')
assert response.body == 'b'
示例15: test_view_after_doesnt_apply_to_exception_view
def test_view_after_doesnt_apply_to_exception_view():
config = setup()
class App(morepath.App):
testing_config = config
class Root(object):
pass
class MyException(Exception):
pass
@App.path(model=Root, path='')
def get_root():
return Root()
@App.view(model=Root)
def view(self, request):
@request.after
def set_header(response):
response.headers.add('Foo', 'FOO')
raise MyException()
@App.view(model=MyException)
def exc_view(self, request):
return "My exception"
config.commit()
c = Client(App())
response = c.get('/')
assert response.body == b'My exception'
assert response.headers.get('Foo') is None