def test_fallback_error_code(): errors = { 400: BadRequest, 401: Unauthorized, 403: Forbidden, 404: NotFound, 405: MethodNotAllowed, 406: NotAcceptable, 408: RequestTimeout, 410: Gone, 411: LengthRequired, 412: PreconditionFailed, 413: RequestEntityTooLarge, 414: RequestURITooLarge, 415: UnsupportedMediaType, 500: InternalServerError, 501: NotImplemented, 502: BadGateway, 503: ServiceUnavailable, } settings = {'PAGE_ERROR': error, 'DEBUG': False} app = Shake(__file__, settings) @app.route('/<int:code>/') def index(request, code): print code raise errors[code] c = app.test_client() for code in errors: if code in app.error_handlers: continue resp = c.get('/%i/' % code) assert resp.status_code == code assert resp.data == 'error'
def test_i18n(): render = Render(i18n=views_dir) def ok(request): return render.from_string('{{ i18n.HELLO }}') def fail(request): return render.from_string('{{ i18n.FOOBAR }}') urls = [ Rule('/ok/', ok), Rule('/fail/', fail), ] app = Shake(urls) c = app.test_client() resp = c.get('/ok/?lang=en-US') assert resp.status_code == HTTP_OK assert resp.data == 'Hello World' resp = c.get('/ok/?lang=en_US') assert resp.status_code == HTTP_OK assert resp.data == 'Hello World' resp = c.get('/ok/?lang=es-AR') assert resp.data == 'Hola mundo' resp = c.get('/fail/?lang=en-US') assert resp.data == ''
def test_url_for(): app = Shake(__file__) c = app.test_client() def index(request): expected = '/hello/world/' url = url_for(endpoint, name='world') assert url == expected expected = 'http://0.0.0.0:5000/hello/world/' url = url_for(endpoint, name='world', external=True) assert url == expected expected = '/hello/world/#awesome' url = url_for(endpoint, name='world', anchor='awesome') assert url == expected expected = 'http://0.0.0.0:5000/hello/world/#awesome' url = url_for(endpoint, name='world', anchor='awesome', external=True) assert url == expected app.add_urls([ Rule('/', index), Rule('/hello/<name>/', endpoint), ]) c.get('/')
def test_send_file_object(): app = Shake(__file__) c = app.test_client() @app.route('/') def index(request): filename = path_join(__file__, 'static/index.html') with io.open(filename) as f: with pytest.raises(AssertionError): resp = send_file(request, f) with io.open(filename) as f: resp = send_file(request, f, mimetype='text/html') assert resp.direct_passthrough assert resp.mimetype == 'text/html' with io.open(filename) as f: resp = send_file(request, f, attachment_filename='foo.html') assert resp.direct_passthrough assert resp.mimetype == 'text/html' f = StringIO('Test') resp = send_file(request, f, attachment_filename='test') assert resp.mimetype == 'application/octet-stream' f = StringIO('Test') resp = send_file(request, f, mimetype='text/plain') assert resp.mimetype == 'text/plain' c.get('/')
def test_processors_order(): app = Shake(__file__) r = [] @app.route('/') def index(request): r.append('view') @app.before_request def br1(request): r.append('br1') @app.before_request def br2(request): r.append('br2') @app.after_request def ar1(response): r.append('ar1') return response @app.after_request def ar2(response): r.append('ar2') return response @app.on_exception def error(e): r.append('error') c = app.test_client() c.get('/') assert r == 'br1 br2 view ar1 ar2'.split()
def test_default_error(): app = Shake(__file__) app.add_url('/', fail) c = app.test_client() with pytest.raises(AssertionError): c.get('/')
def test_send_file_attachment(): app = Shake(__file__) c = app.test_client() @app.route('/') def index(request): filename = path_join(__file__, 'static/index.html') with io.open(filename) as f: resp = send_file(request, f, mimetype='text/html', as_attachment=True) cd_header = resp.headers['Content-Disposition'] value, options = parse_options_header(cd_header) assert value == 'attachment' resp = send_file(request, filename, as_attachment=True) cd_header = resp.headers['Content-Disposition'] value, options = parse_options_header(cd_header) assert value == 'attachment' assert options['filename'] == 'index.html' f = StringIO('Test') resp = send_file(request, f, attachment_filename='readme.txt', as_attachment=True) assert resp.mimetype == 'text/plain' cd_header = resp.headers['Content-Disposition'] value, options = parse_options_header(cd_header) assert value == 'attachment' assert options['filename'] == 'readme.txt' c.get('/')
def test_flash_messagesech(): def t1(request): msgs = get_messages() assert msgs == [] def t2(request): flash(request, 'foo') flash(request, 'bar', 'error', extra='blub') msgs = get_messages() assert len(msgs) == 2 assert (msgs[0]['msg'], msgs[0]['cat'], msgs[0]['extra']) == \ ('foo', 'info', None) assert (msgs[1]['msg'], msgs[1]['cat'], msgs[1]['extra']) == \ ('bar', 'error', 'blub') msgs2 = get_messages() assert msgs2 == msgs urls = [ Rule('/t1', t1), Rule('/t2', t2), ] settings = {'SECRET_KEY': 'abc'*20} app = Shake(urls, settings) c = app.test_client() c.get('/t1') c.get('/t2')
class StateMachine: def __init__(self): rospy.init_node('state_machine') rospy.loginfo('state machine') self.rate = rospy.Rate(60) # set rate to 60 hz # publishers #rospy.init_node('automaton') self.sub = rospy.Subscriber('/tricks', Temperature, self.trickCallback) self.nod_trick = Nod() self.shake_trick = Shake() self.dab_trick = Shake() def trickCallback(self, msg): rospy.loginfo('trickCallback') message = msg.average_temperature if message > 0.68 and message < 0.70: rospy.loginfo('callback if statement') self.nod_trick.nod() if message > 0.58 and message < 0.60: rospy.loginfo('callback if statement') self.shake_trick.shake() if message > 0.48 and message < 0.50: rospy.loginfo('callback if statement') self.dab_trick.dab()
def test_url_for(): def index(request): expected = '/hello/world/' url = url_for(endpoint, name='world') assert url == expected expected = 'http://localhost/hello/world/' url = url_for(endpoint, name='world', external=True) assert url == expected expected = '/hello/world/#awesome' url = url_for(endpoint, name='world', anchor='awesome') assert url == expected expected = 'http://localhost/hello/world/#awesome' url = url_for(endpoint, name='world', anchor='awesome', external=True) assert url == expected urls = [ Rule('/', index), Rule('/hello/<name>/', endpoint), ] app = Shake(urls) c = app.test_client() resp = c.get('/')
def test_send_file_attachment(): def index(request): filename = path_join(__file__, 'static/index.html') with io.open(filename) as f: resp = send_file(request, f, mimetype='text/html', as_attachment=True) cd_header = resp.headers['Content-Disposition'] value, options = parse_options_header(cd_header) assert value == 'attachment' resp = send_file(request, filename, as_attachment=True) cd_header = resp.headers['Content-Disposition'] value, options = parse_options_header(cd_header) assert value == 'attachment' assert options['filename'] == 'index.html' f = StringIO('Test') resp = send_file(request, f, attachment_filename='readme.txt', as_attachment=True) assert resp.mimetype == 'text/plain' cd_header = resp.headers['Content-Disposition'] value, options = parse_options_header(cd_header) assert value == 'attachment' assert options['filename'] == 'readme.txt' app = Shake() app.add_url('/', index) c = app.test_client() resp = c.get('/')
def test_flash_messagesech(): def t1(request): msgs = get_messages() assert msgs == [] def t2(request): flash(request, 'foo') flash(request, 'bar', 'error', extra='blub') msgs = get_messages() assert len(msgs) == 2 assert (msgs[0]['msg'], msgs[0]['cat'], msgs[0]['extra']) == \ ('foo', 'info', None) assert (msgs[1]['msg'], msgs[1]['cat'], msgs[1]['extra']) == \ ('bar', 'error', 'blub') msgs2 = get_messages() assert msgs2 == msgs urls = [ Rule('/t1', t1), Rule('/t2', t2), ] settings = {'SECRET_KEY': 'abc' * 20} app = Shake(urls, settings) c = app.test_client() c.get('/t1') c.get('/t2')
def test_string_endpoint(): app = Shake(__file__) app.add_url('/', 'tests.test_app.index') c = app.test_client() resp = c.get('/') assert resp.status_code == HTTP_OK assert resp.data == 'hello'
def test_callable_endpoint(): app = Shake(__file__) app.add_url('/', index) c = app.test_client() resp = c.get('/') assert resp.status_code == HTTP_OK assert resp.data == 'hello'
def test_default_not_found(): app = Shake(__file__) app.add_url('/', index) c = app.test_client() resp = c.get('/bla') assert resp.status_code == HTTP_NOT_FOUND assert '<title>Page not found</title>' in resp.data
def test_default_not_allowed(): app = Shake(__file__) app.add_url('/', no_pass) c = app.test_client() resp = c.get('/') assert resp.status_code == HTTP_FORBIDDEN assert '<title>Access Denied</title>' in resp.data
def test_custom_error(): settings = {'PAGE_ERROR': error, 'DEBUG': False} app = Shake(__file__, settings) app.add_url('/', fail) c = app.test_client() resp = c.get('/') assert resp.status_code == HTTP_ERROR assert resp.data == 'error'
def test_custom_not_found(): settings = {'PAGE_NOT_FOUND': not_found, 'DEBUG': True} app = Shake(__file__, settings) app.add_url('/', index) c = app.test_client() resp = c.get('/bla') assert resp.status_code == HTTP_NOT_FOUND assert resp.data == 'not found'
def test_custom_not_allowed(): settings = {'PAGE_NOT_ALLOWED': not_allowed} app = Shake(__file__, settings) app.add_url('/', no_pass) c = app.test_client() resp = c.get('/') assert resp.status_code == HTTP_FORBIDDEN assert resp.data == 'access denied'
def test_render_template_view(): app = Shake(__file__) c = app.test_client() app.add_url('/', render_template, defaults={'template': 'tmpl.html', 'render': render}) resp = c.get('/') assert resp.data == '<h1>Hello World</h1>' assert resp.mimetype == 'text/html'
def test_default_error(): urls = [ Rule('/', fail), ] app = Shake(urls) c = app.test_client() with pytest.raises(AssertionError): c.get('/')
def test_session_nosecret(): app = Shake(__file__) @app.route('/') def p1(request): request.session['foo'] = 'bar' c = app.test_client() with pytest.raises(RuntimeError): print c.get('/')
def test_redirect(): app = Shake(__file__) @app.route('/') def redir(request): return redirect('/bla') c = app.test_client() resp = c.get('/') assert resp.status_code == HTTP_FOUND
def test_callable_endpoint(): urls = [ Rule('/', index), ] app = Shake(urls) c = app.test_client() resp = c.get('/') assert resp.status_code == HTTP_OK assert resp.data == 'hello'
def test_default_not_allowed(): urls = [ Rule('/', no_pass), ] app = Shake(urls) c = app.test_client() resp = c.get('/') assert resp.status_code == HTTP_FORBIDDEN assert '<title>Access Denied</title>' in resp.data
def test_string_endpoint(): urls = [ Rule('/', 'tests.test_app.index'), ] app = Shake(urls) c = app.test_client() resp = c.get('/') assert resp.status_code == HTTP_OK assert resp.data == 'hello'
def test_default_not_found(): urls = [ Rule('/', index), ] app = Shake(urls) c = app.test_client() resp = c.get('/bla') assert resp.status_code == HTTP_NOT_FOUND assert '<title>Page not found</title>' in resp.data
def test_custom_error(): urls = [ Rule('/', fail), ] settings = {'PAGE_ERROR': error, 'DEBUG': False} app = Shake(urls, settings) c = app.test_client() resp = c.get('/') assert resp.status_code == HTTP_ERROR assert resp.data == 'error'
def test_custom_not_found(): urls = [ Rule('/', index), ] settings = {'PAGE_NOT_FOUND': not_found} app = Shake(urls, settings) c = app.test_client() resp = c.get('/bla') assert resp.status_code == HTTP_NOT_FOUND assert resp.data == 'not found'
def test_default_response(): app = Shake(__file__) @app.route('/') def index(request): pass c = app.test_client() resp = c.get('/') assert resp.status_code == HTTP_OK assert resp.data == ''
def test_render_view_controller(): urls = [ Rule('/', render_view, defaults={'render': render, 'view': 'view.html'}), ] app = Shake(urls) c = app.test_client() resp = c.get('/') assert resp.data == '<h1>Hello World</h1>' assert resp.mimetype == 'text/html'
def test_default_not_allowed(): settings = { 'PAGE_NOT_ALLOWED': not_allowed_page, } app = Shake(__file__, settings) app.add_url('/', no_pass) c = app.test_client() resp = c.get('/') assert resp.status_code == HTTP_FORBIDDEN assert '<title>Access Denied</title>' in resp.data
def test_csrf_token(): settings = {'SECRET_KEY': 'abc'*20} app = Shake(__file__, settings) environ = get_test_env() request = app.make_request(environ) csrf1 = get_csrf(request).value csrf2 = new_csrf(request).value csrf2_ = get_csrf(request).value assert csrf2 != csrf1 assert csrf2_ == csrf2
def test_csrf_token_query(): settings = {'SECRET_KEY': 'abc'*20} app = Shake(__file__, settings) environ = get_test_env() app.make_request(environ) csrf = get_csrf() tmpl = '{{ csrf.query }}' resp = app.render.from_string(tmpl, to_string=True) expected = '%s=%s' % (csrf.name, csrf.value) assert resp == expected
def test_csrf_token_input(): settings = {'SECRET_KEY': 'abc'*20} app = Shake(__file__, settings) environ = get_test_env() request = app.make_request(environ) csrf = get_csrf(request) tmpl = '{{ csrf.input }}' resp = app.render.from_string(tmpl, to_string=True) expected = '<input type="hidden" name="%s" value="%s">' \ % (csrf.name, csrf.value) assert resp == expected
def test_default_error(): settings = { 'DEBUG': False, 'PAGE_ERROR': error_page, } app = Shake(__file__, settings) app.add_url('/', fail) c = app.test_client() resp = c.get('/') assert resp.status_code == HTTP_ERROR assert '<title>Error</title>' in resp.data