本文整理匯總了Python中webobtoolkit.client.Client類的典型用法代碼示例。如果您正苦於以下問題:Python Client類的具體用法?Python Client怎麽用?Python Client使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Client類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_variable_path_explicit_trumps_implicit
def test_variable_path_explicit_trumps_implicit():
config = setup()
app = morepath.App(testing_config=config)
class Model(object):
def __init__(self, id):
self.id = id
@app.path(model=Model, path='{id}',
converters=dict(id=Converter(int)))
def get_model(id='foo'):
return Model(id)
@app.view(model=Model)
def default(self, request):
return "View: %s (%s)" % (self.id, type(self.id))
@app.view(model=Model, name='link')
def link(self, request):
return request.link(self)
config.commit()
c = Client(app)
response = c.get('1')
assert response.body == "View: 1 (<type 'int'>)"
response = c.get('/1/link')
assert response.body == '/1'
response = c.get('broken')
assert response.status == '404 Not Found'
示例2: test_basic_auth_remember
def test_basic_auth_remember():
config = setup()
app = morepath.App(testing_config=config)
@app.path(path='{id}',
variables=lambda model: {'id': model.id})
class Model(object):
def __init__(self, id):
self.id = id
@app.view(model=Model)
def default(self, request):
# will not actually do anything as it's a no-op for basic
# auth, but at least won't crash
response = Response()
generic.remember(response, request, Identity('foo'),
lookup=request.lookup)
return response
@app.identity_policy()
def policy():
return BasicAuthIdentityPolicy()
config.commit()
c = Client(app)
response = c.get('/foo')
assert response.status == '200 OK'
assert response.body == ''
示例3: test_permission_directive_no_identity
def test_permission_directive_no_identity():
config = setup()
app = morepath.App(testing_config=config)
class Model(object):
def __init__(self, id):
self.id = id
class Permission(object):
pass
@app.path(model=Model, path='{id}',
variables=lambda model: {'id': model.id})
def get_model(id):
return Model(id)
@app.permission(model=Model, permission=Permission, identity=None)
def get_permission(identity, model, permission):
if model.id == 'foo':
return True
else:
return False
@app.view(model=Model, permission=Permission)
def default(self, request):
return "Model: %s" % self.id
config.commit()
c = Client(app)
response = c.get('/foo')
assert response.body == 'Model: foo'
response = c.get('/bar')
assert response.status == '401 Unauthorized'
示例4: test_mount
def test_mount():
config = setup()
app = morepath.App('app', testing_config=config)
mounted = morepath.App('mounted', testing_config=config)
@mounted.path(path='')
class MountedRoot(object):
pass
@mounted.view(model=MountedRoot)
def root_default(self, request):
return "The root"
@mounted.view(model=MountedRoot, name='link')
def root_link(self, request):
return request.link(self)
@app.mount(path='{id}', app=mounted)
def get_context():
return {}
config.commit()
c = Client(app)
response = c.get('/foo')
assert response.body == 'The root'
response = c.get('/foo/link')
assert response.body == '/foo'
示例5: 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"
示例6: test_link_to_unknown_model
def test_link_to_unknown_model():
config = setup()
app = morepath.App(testing_config=config)
@app.path(path='')
class Root(object):
def __init__(self):
self.value = 'ROOT'
class Model(object):
def __init__(self, id):
self.id = id
@app.view(model=Root)
def root_link(self, request):
try:
return request.link(Model('foo'))
except LinkError:
return "Link error"
@app.view(model=Root, name='default')
def root_link_with_default(self, request):
try:
return request.link(Model('foo'), default='hey')
except LinkError:
return "Link Error"
config.commit()
c = Client(app)
response = c.get('/')
assert response.body == 'Link error'
response = c.get('/default')
assert response.body == 'Link Error'
示例7: test_root_link_with_parameters
def test_root_link_with_parameters():
config = setup()
app = morepath.App(testing_config=config)
@app.path(path='')
class Root(object):
def __init__(self, param=0):
assert isinstance(param, int)
self.param = param
@app.view(model=Root)
def default(self, request):
return "The view for root: %s" % self.param
@app.view(model=Root, name='link')
def link(self, request):
return request.link(self)
config.commit()
c = Client(app)
response = c.get('/')
assert response.body == 'The view for root: 0'
response = c.get('/link')
assert response.body == '/?param=0'
response = c.get('/?param=1')
assert response.body == 'The view for root: 1'
response = c.get('/link?param=1')
assert response.body == '/?param=1'
示例8: test_path_and_url_parameter_converter
def test_path_and_url_parameter_converter():
config = setup()
app = morepath.App(testing_config=config)
class Model(object):
def __init__(self, id, param):
self.id = id
self.param = param
from datetime import date
@app.path(model=Model, path='/{id}', converters=dict(param=date))
def get_model(id=0, param=None):
return Model(id, param)
@app.view(model=Model)
def default(self, request):
return "View: %s %s" % (self.id, self.param)
@app.view(model=Model, name='link')
def link(self, request):
return request.link(self)
config.commit()
c = Client(app)
response = c.get('/1/link')
assert response.body == '/1'
示例9: test_variable_path_one_step
def test_variable_path_one_step():
config = setup()
app = morepath.App(testing_config=config)
class Model(object):
def __init__(self, name):
self.name = name
@app.path(model=Model, path='{name}')
def get_model(name):
return Model(name)
@app.view(model=Model)
def default(self, request):
return "View: %s" % self.name
@app.view(model=Model, name='link')
def link(self, request):
return request.link(self)
config.commit()
c = Client(app)
response = c.get('/foo')
assert response.body == 'View: foo'
response = c.get('/foo/link')
assert response.body == '/foo'
示例10: test_type_hints_and_converters
def test_type_hints_and_converters():
config = setup()
app = morepath.App(testing_config=config)
class Model(object):
def __init__(self, d):
self.d = d
from datetime import date
@app.path(model=Model, path='', converters=dict(d=date))
def get_model(d):
return Model(d)
@app.view(model=Model)
def default(self, request):
return "View: %s" % self.d
@app.view(model=Model, name='link')
def link(self, request):
return request.link(self)
config.commit()
c = Client(app)
response = c.get('/?d=20140120')
assert response.body == "View: 2014-01-20"
response = c.get('/link?d=20140120')
assert response.body == '/?d=20140120'
示例11: test_link_for_none_means_no_parameter
def test_link_for_none_means_no_parameter():
config = setup()
app = morepath.App(testing_config=config)
class Model(object):
def __init__(self, id):
self.id = id
@app.path(model=Model, path='')
def get_model(id):
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('/')
assert response.body == "View: None"
response = c.get('/link')
assert response.body == '/'
示例12: test_variable_path_parameter_required_with_default
def test_variable_path_parameter_required_with_default():
config = setup()
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 == "View: a"
response = c.get('/')
assert response.status == '400 Bad Request'
示例13: test_simple_path_two_steps
def test_simple_path_two_steps():
config = setup()
app = morepath.App(testing_config=config)
class Model(object):
def __init__(self):
pass
@app.path(model=Model, path='one/two')
def get_model():
return Model()
@app.view(model=Model)
def default(self, request):
return "View"
@app.view(model=Model, name='link')
def link(self, request):
return request.link(self)
config.commit()
c = Client(app)
response = c.get('/one/two')
assert response.body == 'View'
response = c.get('/one/two/link')
assert response.body == '/one/two'
示例14: test_url_parameter_implicit_converter
def test_url_parameter_implicit_converter():
config = setup()
app = morepath.App(testing_config=config)
class Model(object):
def __init__(self, id):
self.id = id
@app.path(model=Model, path='/')
def get_model(id=0):
return Model(id)
@app.view(model=Model)
def default(self, request):
return "View: %s (%s)" % (self.id, type(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=1')
assert response.body == "View: 1 (<type 'int'>)"
response = c.get('/link?id=1')
assert response.body == '/?id=1'
response = c.get('/?id=broken')
assert response.status == '400 Bad Request'
response = c.get('/')
assert response.body == "View: 0 (<type 'int'>)"
示例15: test_mount_repr
def test_mount_repr():
config = setup()
app = morepath.App('app', testing_config=config)
mounted = morepath.App('mounted', variables=['mount_id'],
testing_config=config)
@mounted.path(path='models/{id}')
class Model(object):
def __init__(self, id):
self.id = id
@app.path(path='')
class Root(object):
pass
@app.view(model=Root)
def app_root_default(self, request):
return repr(request.mounted().child(mounted, id='foo'))
@app.mount(path='{id}', app=mounted)
def get_context(id):
return {
'mount_id': id
}
config.commit()
c = Client(app)
response = c.get('/')
assert response.body == (
"<morepath.Mount of <morepath.App 'mounted'> with "
"variables: id='foo', "
"parent=<morepath.Mount of <morepath.App 'app'>>>")