示例#1
0
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('/')
示例#2
0
def test_default_error():
    app = Shake(__file__)
    app.add_url('/', fail)
    
    c = app.test_client()
    with pytest.raises(AssertionError):
        c.get('/')
示例#3
0
文件: test_app.py 项目: lucuma/Shake
def test_default_error():
    app = Shake(__file__)
    app.add_url('/', fail)

    c = app.test_client()
    with pytest.raises(AssertionError):
        c.get('/')
示例#4
0
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('/')
示例#5
0
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
示例#6
0
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'
示例#7
0
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'
示例#8
0
文件: test_app.py 项目: lucuma/Shake
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
示例#9
0
文件: test_app.py 项目: lucuma/Shake
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'
示例#10
0
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
示例#11
0
文件: test_app.py 项目: lucuma/Shake
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
示例#12
0
文件: test_app.py 项目: lucuma/Shake
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'
示例#13
0
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'
示例#14
0
文件: test_app.py 项目: lucuma/Shake
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'
示例#15
0
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'
示例#16
0
文件: test_app.py 项目: lucuma/Shake
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'
示例#17
0
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'
示例#18
0
文件: test_app.py 项目: lucuma/Shake
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'
示例#19
0
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'
示例#20
0
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
示例#21
0
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
示例#22
0
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
示例#23
0
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
示例#24
0
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('/')
示例#25
0
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'
示例#26
0
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('/')
示例#27
0
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'
示例#28
0
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('/')
示例#29
0
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'
示例#30
0
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('/')
示例#31
0
文件: test_app.py 项目: lucuma/Shake
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('/')
示例#32
0
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('/')
示例#33
0
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'
示例#34
0
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'
示例#35
0
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('/')
示例#36
0
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('/')
示例#37
0
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('/')
示例#38
0
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('/')