Esempio n. 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('/')
Esempio n. 2
0
def test_default_error():
    app = Shake(__file__)
    app.add_url('/', fail)
    
    c = app.test_client()
    with pytest.raises(AssertionError):
        c.get('/')
Esempio n. 3
0
def test_default_error():
    app = Shake(__file__)
    app.add_url('/', fail)

    c = app.test_client()
    with pytest.raises(AssertionError):
        c.get('/')
Esempio n. 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('/')
Esempio n. 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
Esempio n. 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'
Esempio n. 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'
Esempio n. 8
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
Esempio n. 9
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'
Esempio n. 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
Esempio n. 11
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
Esempio n. 12
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'
Esempio n. 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'
Esempio n. 14
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'
Esempio n. 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'
Esempio n. 16
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'
Esempio n. 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'
Esempio n. 18
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'
Esempio n. 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'
Esempio n. 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
Esempio n. 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
Esempio n. 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
Esempio n. 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
Esempio n. 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('/')
Esempio n. 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'
Esempio n. 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('/')
Esempio n. 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'
Esempio n. 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('/')
Esempio n. 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'
Esempio n. 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('/')
Esempio n. 31
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('/')
Esempio n. 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('/')
Esempio n. 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'
Esempio n. 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'
Esempio n. 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('/')
Esempio n. 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('/')
Esempio n. 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('/')
Esempio n. 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('/')