本文整理汇总了Python中aspen.testing.client.Client类的典型用法代码示例。如果您正苦于以下问题:Python Client类的具体用法?Python Client怎么用?Python Client使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Client类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Harness
class Harness(object):
"""A harness to be used in the Aspen test suite itself. Probably not useful to you.
"""
def __init__(self):
self.fs = namedtuple('fs', 'www project')
self.fs.www = FilesystemTree()
self.fs.project = FilesystemTree()
self.client = Client(self.fs.www.root, self.fs.project.root)
def teardown(self):
self.fs.www.remove()
self.fs.project.remove()
# Simple API
# ==========
def simple(self, contents='Greetings, program!', filepath='index.html.spt', uripath=None,
argv=None, **kw):
"""A helper to create a file and hit it through our machinery.
"""
if filepath is not None:
self.fs.www.mk((filepath, contents))
if argv is not None:
self.client.hydrate_website(argv)
if uripath is None:
if filepath is None:
uripath = '/'
else:
uripath = '/' + filepath
if uripath.endswith('.spt'):
uripath = uripath[:-len('.spt')]
for indexname in self.client.website.indices:
if uripath.endswith(indexname):
uripath = uripath[:-len(indexname)]
break
return self.client.GET(uripath, **kw)
def make_request(self, *a, **kw):
kw['return_after'] = 'dispatch_request_to_filesystem'
kw['want'] = 'request'
return self.simple(*a, **kw)
def make_dispatch_result(self, *a, **kw):
kw['return_after'] = 'dispatch_request_to_filesystem'
kw['want'] = 'dispatch_result'
return self.simple(*a, **kw)
示例2: compile_assets
def compile_assets(website):
client = Client(website.www_root, website.project_root)
client._website = website
for spt in find_files(website.www_root+'/assets/', '*.spt'):
filepath = spt[:-4] # /path/to/www/assets/foo.css
urlpath = spt[spt.rfind('/assets/'):-4] # /assets/foo.css
try:
# Remove any existing compiled asset, so we can access the dynamic
# one instead (Aspen prefers foo.css over foo.css.spt).
os.unlink(filepath)
except:
pass
content = client.GET(urlpath).body
tmpfd, tmpfpath = mkstemp(dir='.')
os.write(tmpfd, content)
os.close(tmpfd)
os.rename(tmpfpath, filepath)
示例3: compile_assets
def compile_assets(website):
client = Client(website.www_root, website.project_root)
client._website = website
for spt in find_files(website.www_root+'/assets/', '*.spt'):
filepath = spt[:-4] # /path/to/www/assets/foo.css
urlpath = spt[spt.rfind('/assets/'):-4] # /assets/foo.css
try:
# Remove any existing compiled asset, so we can access the dynamic
# one instead (Aspen prefers foo.css over foo.css.spt).
os.unlink(filepath)
except:
pass
headers = {}
if website.base_url:
url = urlparse.urlparse(website.base_url)
headers[b'HTTP_X_FORWARDED_PROTO'] = str(url.scheme)
headers[b'HTTP_HOST'] = str(url.netloc)
content = client.GET(urlpath, **headers).body
tmpfd, tmpfpath = mkstemp(dir='.')
os.write(tmpfd, content.encode('utf8'))
os.close(tmpfd)
os.rename(tmpfpath, filepath)
atexit.register(lambda: clean_assets(website.www_root))
示例4: build_wsgi_environ
def build_wsgi_environ(self, *a, **kw):
"""Extend base class to support authenticating as a certain user.
"""
# csrf - for both anon and authenticated
self.cookie[b'csrf_token'] = b'sotokeny'
kw[b'HTTP_X-CSRF-TOKEN'] = b'sotokeny'
# user authentication
auth_as = kw.pop('auth_as', None)
if auth_as is None:
if SESSION in self.cookie:
del self.cookie[SESSION]
else:
user = User.from_username(auth_as)
user.sign_in(self.cookie)
return Client.build_wsgi_environ(self, *a, **kw)
示例5: build_wsgi_environ
def build_wsgi_environ(self, *a, **kw):
"""Extend base class to support authenticating as a certain user.
"""
self.cookie.clear()
# csrf - for both anon and authenticated
csrf_token = kw.get('csrf_token', b'ThisIsATokenThatIsThirtyTwoBytes')
if csrf_token:
self.cookie[b'csrf_token'] = csrf_token
kw[b'HTTP_X-CSRF-TOKEN'] = csrf_token
# user authentication
auth_as = kw.pop('auth_as', None)
if auth_as:
user.User.from_username(auth_as).sign_in(self.cookie)
for k, v in kw.pop('cookies', {}).items():
self.cookie[k] = v
return Client.build_wsgi_environ(self, *a, **kw)
示例6: build_wsgi_environ
def build_wsgi_environ(self, *a, **kw):
"""Extend base class to support authenticating as a certain user.
"""
self.cookie.clear()
# csrf - for both anon and authenticated
csrf_token = kw.get('csrf_token', b'ThisIsATokenThatIsThirtyTwoBytes')
if csrf_token:
self.cookie[b'csrf_token'] = csrf_token
kw[b'HTTP_X-CSRF-TOKEN'] = csrf_token
# user authentication
auth_as = kw.pop('auth_as', None)
if auth_as:
assert auth_as.session_token
self.cookie[SESSION] = '%s:%s' % (auth_as.id, auth_as.session_token)
# cookies
for k, v in kw.pop('cookies', {}).items():
self.cookie[k] = v
return Client.build_wsgi_environ(self, *a, **kw)
示例7: __init__
def __init__(self, *a, **kw):
Client.__init__(self, *a, **kw)
Client.website = website
示例8: __init__
def __init__(self, *a, **kw):
Client.__init__(self, *a, **kw)
Client.website = Client.hydrate_website(self)
示例9: __init__
def __init__(self):
self.fs = namedtuple("fs", "www project")
self.fs.www = FilesystemTree()
self.fs.project = FilesystemTree()
self.client = Client(self.fs.www.root, self.fs.project.root)
示例10: Harness
class Harness(object):
"""A harness to be used in the Aspen test suite itself. Probably not useful to you.
"""
def __init__(self):
self.fs = namedtuple("fs", "www project")
self.fs.www = FilesystemTree()
self.fs.project = FilesystemTree()
self.client = Client(self.fs.www.root, self.fs.project.root)
def teardown(self):
self.fs.www.remove()
self.fs.project.remove()
# Simple API
# ==========
def simple(self, contents="Greetings, program!", filepath="index.html.spt", uripath=None, argv=None, **kw):
"""A helper to create a file and hit it through our machinery.
"""
if filepath is not None:
self.fs.www.mk((filepath, contents))
if argv is not None:
self.client.hydrate_website(argv)
if uripath is None:
if filepath is None:
uripath = "/"
else:
uripath = "/" + filepath
if uripath.endswith(".spt"):
uripath = uripath[: -len(".spt")]
for indexname in self.client.website.indices:
if uripath.endswith(indexname):
uripath = uripath[: -len(indexname)]
break
return self.client.GET(uripath, **kw)
def make_request(self, *a, **kw):
kw["return_after"] = "dispatch_request_to_filesystem"
kw["want"] = "request"
return self.simple(*a, **kw)
# Sockets
# =======
def make_transport(self, content="", state=0):
self.fs.www.mk(("echo.sock.spt", content))
socket = self.make_socket()
transport = XHRPollingTransport(socket)
transport.timeout = 0.05 # for testing, could screw up the test
if state == 1:
transport.respond(Request(uri="/echo.sock"))
return transport
def make_socket_request(self, filename="echo.sock.spt"):
request = Request(uri="/echo.sock")
request.website = self.client.website
request.fs = self.fs.www.resolve(filename)
return request
def make_socket(self, filename="echo.sock.spt", channel=None):
request = self.make_socket_request(filename="echo.sock.spt")
if channel is None:
channel = Channel(request.line.uri.path.raw, ThreadedBuffer)
socket = Socket(request, channel)
return socket
def SocketInThread(harness):
class _SocketInThread(object):
def __enter__(self, filename="echo.sock.spt"):
self.socket = harness.make_socket(filename)
self.socket.loop.start()
return self.socket
def __exit__(self, *a):
self.socket.loop.stop()
return _SocketInThread()
示例11: client
def client():
client = Client(www_root='www', project_root='.')
client._website = website
yield client
示例12: __init__
def __init__(self, *a, **kw):
Client.__init__(self, *a, **kw)
Client.app = _get_app()
Client.website = Client.app.website