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 if urlpath == '/assets/_well-known/acme-challenge/%token': # This *should* be dynamic. continue 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) os.close(tmpfd) os.rename(tmpfpath, filepath) atexit.register(lambda: clean_assets(website.www_root))
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)
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)
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)
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))
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)
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)
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)
def __init__(self, *a, **kw): Client.__init__(self, *a, **kw) Client.website = website
def __init__(self, *a, **kw): Client.__init__(self, *a, **kw) Client.website = Client.hydrate_website(self)
def client(): return Client('www', '.')
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)
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()
def client(): client = Client(www_root='www', project_root='.') client._website = website yield client
def __init__(self, *a, **kw): Client.__init__(self, *a, **kw) Client.app = _get_app() Client.website = Client.app.website