Beispiel #1
0
def test_custom_serializer():
    was_used = [False, False]
    class CustomSerializer(object):
        def loads(self, data_string):
            was_used[0] = True
            return json.loads(data_string.decode('utf-8'))

        def dumps(self, data):
            was_used[1] = True
            return json.dumps(data).encode('utf-8')

    serializer = CustomSerializer()
    options = {'session.validate_key':'hoobermas', 'session.type':'cookie', 'data_serializer': serializer}
    app = TestApp(SessionMiddleware(simple_app, **options))

    res = app.get('/')
    assert 'current value is: 1' in res

    res = app.get('/')
    cookie = SignedCookie('hoobermas')
    session_data = cookie.value_decode(app.cookies['beaker.session.id'])[0]
    session_data = b64decode(session_data)
    data = serializer.loads(session_data)
    assert data['value'] == 2

    res = app.get('/')
    assert 'current value is: 3' in res

    assert all(was_used)
def test_custom_serializer():
    was_used = [False, False]

    class CustomSerializer(object):
        def loads(self, data_string):
            was_used[0] = True
            return json.loads(data_string.decode("utf-8"))

        def dumps(self, data):
            was_used[1] = True
            return json.dumps(data).encode("utf-8")

    serializer = CustomSerializer()
    options = {
        "session.validate_key": "hoobermas",
        "session.type": "cookie",
        "data_serializer": serializer,
    }
    app = TestApp(SessionMiddleware(simple_app, **options))

    res = app.get("/")
    assert "current value is: 1" in res

    res = app.get("/")
    cookie = SignedCookie("hoobermas")
    session_data = cookie.value_decode(app.cookies["beaker.session.id"])[0]
    session_data = b64decode(session_data)
    data = serializer.loads(session_data)
    assert data["value"] == 2

    res = app.get("/")
    assert "current value is: 3" in res

    assert all(was_used)
Beispiel #3
0
def test_use_json_serializer_without_encryption_key():
    setup_cookie_request()
    so = get_session(use_cookies=False, type='file', data_dir='./cache', data_serializer='json')
    so['foo'] = 'bar'
    so.save()
    session = get_session(id=so.id, use_cookies=False, type='file', data_dir='./cache', data_serializer='json')
    assert 'foo' in session
    serialized_session = open(session.namespace.file, 'rb').read()
    memory_state = pickle.loads(serialized_session)
    session_data = b64decode(memory_state.get('session'))
    data = deserialize(session_data, 'json')
    assert 'foo' in data
Beispiel #4
0
def test_pickle_serializer():
    options = {'session.validate_key':'hoobermas', 'session.type':'cookie', 'data_serializer': 'pickle'}
    app = TestApp(SessionMiddleware(simple_app, **options))

    res = app.get('/')
    assert 'current value is: 1' in res

    res = app.get('/')
    cookie = SignedCookie('hoobermas')
    session_data = cookie.value_decode(app.cookies['beaker.session.id'])[0]
    session_data = b64decode(session_data)
    data = beaker.util.deserialize(session_data, 'pickle')
    assert data['value'] == 2

    res = app.get('/')
    assert 'current value is: 3' in res
def test_json_serializer():
    options = {
        "session.validate_key": "hoobermas",
        "session.type": "cookie",
        "data_serializer": "json",
    }
    app = TestApp(SessionMiddleware(simple_app, **options))

    res = app.get("/")
    assert "current value is: 1" in res

    res = app.get("/")
    cookie = SignedCookie("hoobermas")
    session_data = cookie.value_decode(app.cookies["beaker.session.id"])[0]
    session_data = b64decode(session_data)
    data = beaker.util.deserialize(session_data, "json")
    assert data["value"] == 2

    res = app.get("/")
    assert "current value is: 3" in res