#! /usr/bin/env python3 import time from wsgiref.simple_server import make_server from yhttp import Application, text, json app = Application() app.staticfile('/demo', 'demo/demo.py') @app.route('/json') @json def get(req): # noqa: W0404 return {'foo': 'bar'} @app.route('/json') @json def post(req): # noqa: W0404 return req.form @app.route(r'/(.*)') @text def get(req, resource): # noqa: W0404 i = 0 while True: yield f'{req.path} {i:04}\r\n'.encode() time.sleep(1) i += 1
from yhttp import Application from easycli import SubCommand from bddcli import Application as CLIApplication, Given, stdout, \ status, stderr, when class Foo(SubCommand): __command__ = 'foo' def __call__(self, args): assert args.application is app print(app.settings.title) app = Application() app.settings.merge('title: foo') app.cliarguments.append(Foo) def test_applicationcli_default(): cliapp = CLIApplication('foo', 'tests.test_applicationcli:app.climain') with Given(cliapp, '--help'): assert status == 0 when('foo') assert status == 0 assert stdout == 'foo\n' with tempfile.NamedTemporaryFile() as f: f.write(b'title: bar')
def app(): return Application()
from bddcli import Application as CLIApplication, Given, stderr, stdout, \ status, when from yhttp import Application, __version__ app = Application(version=__version__) def test_versioncli(): cliapp = CLIApplication('foo', 'tests.test_builtincli_version:app.climain') with Given(cliapp, '--version'): assert status == 0 assert stdout.strip() == __version__ assert stderr == '' when('') assert status == 0 assert stderr == '' if __name__ == '__main__': app.climain(['--version'])
import time import requests from bddcli import Application as CLIApplication, Given, stderr, stdout from yhttp import Application, text app = Application() @app.route('/') @text def get(req): return 'foo' def test_servercli(freetcpport): cliapp = CLIApplication('foo', 'tests.test_builtincli:app.climain') with Given(cliapp, f'serve --bind {freetcpport}', nowait=True) as s: url = f'http://localhost:{freetcpport}' time.sleep(1) r = requests.get(url) assert r.text == 'foo' s.kill() if __name__ == '__main__': app.climain(['serve'])
from bddcli import Given, Application as CLIApplication, status, stderr, \ stdout, when from yhttp import Application from yhttp.extensions.auth import install app = Application() app.settings.merge(''' jwt: secret: foobarbaz ''') authorize = install(app) def test_jwtcli(): cliapp = CLIApplication('example', 'tests.test_cli:app.climain') with Given(cliapp, 'jwt --help'): assert status == 0 assert stderr == '' when('jwt create \'{"foo": "bar"}\'') print(stderr) assert status == 0 assert len(stdout.split('.')) == 3 if __name__ == '__main__': app.climain(['jwt', 'c', '{"foo": "bar"}'])