exports['serialize'] = function(test){
    test.expect(7);
    // copy some functions
    var encrypt = sessions.encrypt;
    var hmac_signature = sessions.hmac_signature;
    var stringify= JSON.stringify;
    var Date_copy = global.Date;

    global.Date = function(){this.getTime = function(){return 1234;}};
    JSON.stringify = function(obj){
        test.same(
            obj, {test:'test'}, 'JSON.stringify called with cookie data obj'
        );
        return 'data';
    };
    sessions.encrypt = function(secret, str){
        test.equals(secret, 'secret', 'encrypt called with secret');
        test.equals(str, 'data', 'encrypt called with stringified data');
        return 'encrypted_data';
    };
    sessions.hmac_signature = function(secret, timestamp, data_str){
        test.equals(secret, 'secret', 'hmac_signature called with secret');
        test.equals(timestamp, 1234, 'hmac_signature called with timestamp');
        test.equals(
            data_str, 'encrypted_data',
            'hmac_signature called with encrypted data string'
        );
        return 'hmac';
    };
    var r = sessions.serialize('secret', {test:'test'});
    test.equals(
        r, 'hmac1234encrypted_data', 'serialize returns correct string'
    );

    // restore copied functions:
    sessions.encrypt = encrypt;
    sessions.hmac_signature = hmac_signature;
    JSON.stringify = stringify;
    global.Date = Date_copy;
    test.done();
};
exports['serialize data over 4096 chars'] = function(test){
    test.expect(1);
    // copy some functions
    var encrypt = sessions.encrypt;
    var hmac_signature = sessions.hmac_signature;
    var stringify= JSON.stringify;
    var Date_copy = global.Date;

    global.Date = function(){this.getTime = function(){return 1234;}};
    JSON.stringify = function(obj){
        return 'data';
    };
    sessions.encrypt = function(secret, str){
        // lets make this too long!
        var r = '';
        for(var i=0; i<4089; i++){
            r = r + 'x';
        };
        return r;
    };
    sessions.hmac_signature = function(secret, timestamp, data_str){
        return 'hmac';
    };
    try {
        var r = sessions.serialize('secret', {test:'test'});
    }
    catch(e){
        test.ok(
            true, 'serializing a cookie over 4096 chars throws an exception'
        );
    }

    // restore copied functions:
    sessions.encrypt = encrypt;
    sessions.hmac_signature = hmac_signature;
    JSON.stringify = stringify;
    global.Date = Date_copy;
    test.done();
};