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_default_error(): app = Shake(__file__) app.add_url('/', fail) c = app.test_client() with pytest.raises(AssertionError): c.get('/')
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_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_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_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_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_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_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_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_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
def test_send_file_xsendfile(): def index(request): filename = path_join(__file__, 'static/index.html') resp = send_file(request, filename, use_x_sendfile=True) assert resp.direct_passthrough assert 'x-sendfile' in resp.headers assert resp.headers['x-sendfile'] == filename assert resp.mimetype == 'text/html' app = Shake() app.add_url('/', index) c = app.test_client() resp = c.get('/')
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_add_url(): def number(request, num): return str(num) app = Shake() app.add_url('/', index) app.add_url('/<int:num>/', number) c = app.test_client() resp = c.get('/') assert resp.status_code == HTTP_OK assert resp.data == 'hello' resp = c.get('/3/') assert resp.status_code == HTTP_OK assert resp.data == '3'
def test_bad_responses(): bad_responses = [ 42, [], (), [1, 2, 3], ('a', 'b', 'c'), os.path, Ellipsis, ] for r in bad_responses: app = Shake(__file__) app.add_url('/', lambda request: r) c = app.test_client() print '\nr is:', r resp = c.get('/')
def test_send_file_regular(): def index(request): filename = path_join(__file__, 'static/index.html') resp = send_file(request, filename) assert resp.direct_passthrough assert resp.mimetype == 'text/html' with io.open(filename) as f: assert resp.data == f.read() filename = path_join(__file__, 'static/favicon.ico') resp = send_file(request, filename) assert resp.direct_passthrough assert resp.mimetype == 'image/x-icon' app = Shake() app.add_url('/', index) c = app.test_client() resp = c.get('/')
def test_render_template_view_args(): app = Shake(__file__) app.add_url('/', render_template, defaults={ 'render': render, 'template': 'tmpl.txt', 'context': { 'who': 'You', 'action': 'are', 'where': 'here', }, 'mimetype': 'foo/bar', } ) c = app.test_client() resp = c.get('/') assert resp.data == 'You are here' assert resp.mimetype == 'foo/bar'
def test_render_template_view_args(): app = Shake(__file__) app.add_url('/', render_template, defaults={ 'render': render, 'template': 'tmpl.txt', 'context': { 'who': 'You', 'action': 'are', 'where': 'here', }, 'mimetype': 'foo/bar', }) c = app.test_client() resp = c.get('/') assert resp.data == 'You are here' assert resp.mimetype == 'foo/bar'
def test_send_file_object_xsendfile(): def index(request): filename = path_join(__file__, 'static/index.html') with io.open(filename) as f: resp = send_file(request, f, mimetype='text/html', use_x_sendfile=True) assert 'x-sendfile' in resp.headers assert resp.headers['x-sendfile'] == filename assert resp.mimetype == 'text/html' f = StringIO('Test') resp = send_file(request, f, mimetype='text/plain', use_x_sendfile=True) assert 'x-sendfile' not in resp.headers app = Shake() app.add_url('/', index) c = app.test_client() resp = c.get('/')
def test_send_file_object(): def index(request): filename = path_join(__file__, 'static/index.html') with io.open(filename) as f: data = f.read() 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' assert resp.data == data with io.open(filename) as f: resp = send_file(request, f, attachment_filename='foo.html') assert resp.direct_passthrough assert resp.mimetype == 'text/html' assert resp.data == data f = StringIO('Test') resp = send_file(request, f, attachment_filename='test') assert resp.mimetype == 'application/octet-stream' assert resp.data == 'Test' f = StringIO('Test') resp = send_file(request, f, mimetype='text/plain') assert resp.mimetype == 'text/plain' assert resp.data == 'Test' app = Shake() app.add_url('/', index) c = app.test_client() resp = c.get('/')