spec/javascripts/faye-extension_spec.js in faye-authentication-0.1.0 vs spec/javascripts/faye-extension_spec.js in faye-authentication-0.2.0
- old
+ new
@@ -22,18 +22,19 @@
});
});
describe('With extension', function() {
beforeEach(function() {
- this.extension = new FayeAuthentication();
+ this.extension = new FayeAuthentication(this.client);
this.client.addExtension(this.extension);
});
function stubSignature(context, callback) {
var self = context;
self.client.handshake(function() {
- var signature = CryptoJS.HmacSHA1("/foobar-" + self.client._clientId, "macaroni").toString();
+ var jwtsign = new jwt.WebToken('{"clientId": "' + self.client._clientId + '", "channel": "/foobar", "exp": 2803694528}', '{"alg": "HS256"}');
+ var signature = jwtsign.serialize("macaroni");
jasmine.Ajax.stubRequest('/faye/auth').andReturn({
'responseText': '{"signature": "' + signature + '"}'
});
callback();
@@ -56,19 +57,48 @@
done();
});
});
});
- it('clears the signatures when receiving an error from the server', function(done) {
- this.extension._signatures = {'123': []};
+ it('tries to get a new signature immediately when the used signature is bad or expired', function(done) {
jasmine.Ajax.stubRequest('/faye/auth').andReturn({
'responseText': '{"signature": "bad"}'
});
var self = this;
this.client.subscribe('/toto').then(undefined, function() {
- expect(Object.keys(self.extension._signatures).length).toBe(0);
+ expect(jasmine.Ajax.requests.count()).toBe(3); // Handshake + auth * 2
done();
});
+ });
+ it('calls the success callback for a successfully retried message', function(done) {
+
+ this.client.subscribe('/foo').then(function() {
+ expect(jasmine.Ajax.requests.count()).toBe(3); // Handshake + auth * 2
+ done();
+ }, function(e) { console.log(e)});
+
+ setTimeout(function() {
+ var request = jasmine.Ajax.requests.mostRecent();
+ var params = queryString.parse(request.params);
+
+ var jwtsign_bad = new jwt.WebToken('{"clientId": "' + params['message[clientId]'] + '", "channel": "/foo", "exp": 1}', '{"alg": "HS256"}');
+ var signature_bad = jwtsign_bad.serialize("macaroni");
+
+ var jwtsign_good = new jwt.WebToken('{"clientId": "' + params['message[clientId]'] + '", "channel": "/foo", "exp": 2803694528}', '{"alg": "HS256"}');
+ var signature_good = jwtsign_good.serialize("macaroni");
+
+ request.response({
+ 'status' : 200,
+ 'responseText': '{"signature": "' + signature_bad + '"}'
+ });
+
+ jasmine.Ajax.stubRequest('/faye/auth').andReturn({
+ 'responseText': '{"signature": "' + signature_good + '"}'
+ });
+
+ }, 1000);
+
});
+
});
});