示例#1
0
def test_get_key():
    with pytest.raises(Exception) as exc_info:
        get_key(Request({}, {}))
    assert (exc_info.value.args[0] == "You must set config['SECRET_KEY'] to "
                                      "use sessions")

    assert get_key(Request({}, config={'SECRET_KEY': b'abc'})) == b'abc'
示例#2
0
def test_open_session():
    key = b'abc'

    good_sess = 'eyJhIjoxfQ.2XFQKMS-erhoKkSGsezDxFsim6YctUnzxaiiMP1wzFs'
    good_req = Request({'HTTP_COOKIE': 'session=' + good_sess},
                       {'SECRET_KEY': key})
    assert open_session(good_req) == {u'a': 1}

    for bad_sess in ('eyJhIjoxfQ.abc', 'eyJhIjoxfQ', ''):
        bad_req = Request({'HTTP_COOKIE': 'session=' + bad_sess},
                          {'SECRET_KEY': key})
        assert open_session(bad_req) == {}

    assert open_session(Request({}, {'SECRET_KEY': key})) == {}
示例#3
0
def test_headers():
    request = Request(
        {
            'CONTENT_TYPE': 'text/plain; charset=utf-8',
            'CONTENT_LENGTH': '42',
            'HTTP_HOST': 'example.com',
            'HTTP_X_AUTH_KEY': 'the auth key',
            'SERVER_NAME': 'localhost',
        }, {})
    assert request.headers['X-Auth-Key'] == 'the auth key'
    assert request.headers.get('X-Auth-Key') == 'the auth key'
    assert request.headers.get('X-Auth-Key', 'abc') == 'the auth key'
    assert request.host == 'example.com'

    with pytest.raises(KeyError) as exc_info:
        request.headers['X-Missing']
    assert exc_info.value.args[0] == 'X-Missing'

    assert request.headers.get('X-Missing') is None
    assert request.headers.get('X-Missing', 'abc') == 'abc'

    assert request.headers['Content-Type'] == 'text/plain; charset=utf-8'
    assert request.headers['Content-Length'] == '42'

    assert 'X-Missing' not in request.headers
    assert 'Content-Type' in request.headers
    assert 'CoNtEnT-LeNgTh' in request.headers
    assert 'x-AuTh-KeY' in request.headers

    assert set(request.headers) == set(
        ['Content-Type', 'Content-Length', 'Host', 'X-Auth-Key'])
示例#4
0
def test_wsgi_exceptions():
    wsgi = app.wsgi_app({})

    arr = [None]

    def start_request(status, headers):
        arr[0] = (status, list(headers))

    requests = [
        ('GET', '/', 200, '200 OK', b'Hello World!\n'),
        ('GET', '/before', 200, '200 OK', b'before_request returned\n'),
        ('GET', '/after', 200, '200 OK', b'after_request returned\n'),
        ('GET', '/forbidden', 403, '403 Forbidden', b'Not allowed, man\n'),
        ('GET', '/things/50', 200, '200 OK', b'Thing 50\n'),
        ('GET', '/asdf', 404, '404 Not Found', b'Not Found\n'),
        ('POST', '/', 405, '405 Method Not Allowed', b'Method Not Allowed\n'),
        ('GET', '/internal', 500, '500 Internal Server Error',
         b'Internal Server Error\n'),
    ]

    for method, url, status_code, status, text in requests:
        environ = {
            'REQUEST_METHOD': method,
            'PATH_INFO': url,
        }

        resp = app.dispatch(Request(environ, {}))
        assert list(resp) == [text]
        assert resp.status_code == status_code
        assert resp.status == status

        assert list(wsgi(environ, start_request)) == [text]
        assert arr[0] == (status, [('Content-Type',
                                    'text/plain; charset=utf-8')])
示例#5
0
def test_save_session():
    req = Request({}, {'SECRET_KEY': b'abc'})
    req.session = {u'a': 1}
    resp = Response()

    save_session(req, resp)
    expected = u'eyJhIjoxfQ.2XFQKMS-erhoKkSGsezDxFsim6YctUnzxaiiMP1wzFs'
    assert resp.headers['Set-Cookie'] == u'session=' + expected
示例#6
0
 def request_with_data(data, length=None, content_type=None):
     if length is None:
         length = str(len(data))
     environ = {
         'CONTENT_LENGTH': length,
         'wsgi.input': io.BytesIO(data),
     }
     if content_type is not None:
         environ['CONTENT_TYPE'] = content_type
     return Request(environ, {})
示例#7
0
def test_cookies():
    request = Request({
        'HTTP_COOKIE': 'a=b; %FF=%FF; empty_value=; key_only',
    }, {})
    assert request.cookies == {
        u'a': u'b',
        u'\ufffd': u'\ufffd',
        u'empty_value': u'',
        u'key_only': None,
    }
示例#8
0
 def req(env):
     base_environ = {
         'REQUEST_METHOD': 'GET',
         'PATH_INFO': '/',
         'SCRIPT_NAME': '/app',
         'SERVER_NAME': 'localhost',
         'SERVER_PORT': '5000',
         'wsgi.url_scheme': 'http',
     }
     base_environ.update(env)
     return Request(base_environ, {})
示例#9
0
def test_dispatch_error(monkeypatch):
    def raises_lookup_error(method, path):
        raise LookupError('Some unexpected message')

    monkeypatch.setattr(app.router, 'get_view', raises_lookup_error)

    req = Request({'REQUEST_METHOD': 'GET', 'PATH_INFO': '/'}, {})
    resp = app.dispatch(req)
    assert list(resp) == [b'Internal Server Error\n']
    assert resp.status_code == 500
    assert resp.status == '500 Internal Server Error'
示例#10
0
def test_environ():
    request = Request(
        {
            'PATH_INFO': '/',
            'REQUEST_METHOD': 'GET',
            'SCRIPT_NAME': '/app',
            'SERVER_NAME': 'localhost',
            'SERVER_PORT': '5000',
            'wsgi.url_scheme': 'http',
        }, {})
    assert request.method == 'GET'
    assert request.path == '/'
    assert request.script_name == '/app'
    assert request.host == 'localhost'
    assert request.port == '5000'
    assert request.scheme == 'http'