snowflake/proxy/spec/broker.spec.js
Arlo Breault 31ad9566e6 Compile coffee files and remove them
With,

  ./node_modules/.bin/coffee -b -c Cakefile `find . -path ./node_modules -prune -o -name '*.coffee'`
2019-07-10 10:49:36 +02:00

119 lines
3.5 KiB
JavaScript

// Generated by CoffeeScript 2.4.1
/*
jasmine tests for Snowflake broker
*/
var XMLHttpRequest;
XMLHttpRequest = (function() {
// fake xhr
// class XMLHttpRequest
class XMLHttpRequest {
constructor() {
this.onreadystatechange = null;
}
open() {}
setRequestHeader() {}
send() {}
};
XMLHttpRequest.prototype.DONE = 1;
return XMLHttpRequest;
}).call(this);
describe('Broker', function() {
it('can be created', function() {
var b;
b = new Broker('fake');
expect(b.url).toEqual('https://fake/');
return expect(b.id).not.toBeNull();
});
describe('getClientOffer', function() {
it('polls and promises a client offer', function(done) {
var b, poll;
b = new Broker('fake');
// fake successful request and response from broker.
spyOn(b, '_postRequest').and.callFake(function() {
b._xhr.readyState = b._xhr.DONE;
b._xhr.status = Broker.STATUS.OK;
b._xhr.responseText = 'fake offer';
return b._xhr.onreadystatechange();
});
poll = b.getClientOffer();
expect(poll).not.toBeNull();
expect(b._postRequest).toHaveBeenCalled();
return poll.then(function(desc) {
expect(desc).toEqual('fake offer');
return done();
}).catch(function() {
fail('should not reject on Broker.STATUS.OK');
return done();
});
});
it('rejects if the broker timed-out', function(done) {
var b, poll;
b = new Broker('fake');
// fake timed-out request from broker
spyOn(b, '_postRequest').and.callFake(function() {
b._xhr.readyState = b._xhr.DONE;
b._xhr.status = Broker.STATUS.GATEWAY_TIMEOUT;
return b._xhr.onreadystatechange();
});
poll = b.getClientOffer();
expect(poll).not.toBeNull();
expect(b._postRequest).toHaveBeenCalled();
return poll.then(function(desc) {
fail('should not fulfill on Broker.STATUS.GATEWAY_TIMEOUT');
return done();
}, function(err) {
expect(err).toBe(Broker.MESSAGE.TIMEOUT);
return done();
});
});
return it('rejects on any other status', function(done) {
var b, poll;
b = new Broker('fake');
// fake timed-out request from broker
spyOn(b, '_postRequest').and.callFake(function() {
b._xhr.readyState = b._xhr.DONE;
b._xhr.status = 1337;
return b._xhr.onreadystatechange();
});
poll = b.getClientOffer();
expect(poll).not.toBeNull();
expect(b._postRequest).toHaveBeenCalled();
return poll.then(function(desc) {
fail('should not fulfill on non-OK status');
return done();
}, function(err) {
expect(err).toBe(Broker.MESSAGE.UNEXPECTED);
expect(b._xhr.status).toBe(1337);
return done();
});
});
});
it('responds to the broker with answer', function() {
var b;
b = new Broker('fake');
spyOn(b, '_postRequest');
b.sendAnswer('fake id', 123);
return expect(b._postRequest).toHaveBeenCalledWith('fake id', jasmine.any(Object), 'answer', '123');
});
return it('POST XMLHttpRequests to the broker', function() {
var b;
b = new Broker('fake');
b._xhr = new XMLHttpRequest();
spyOn(b._xhr, 'open');
spyOn(b._xhr, 'setRequestHeader');
spyOn(b._xhr, 'send');
b._postRequest(0, b._xhr, 'test', 'data');
expect(b._xhr.open).toHaveBeenCalled();
expect(b._xhr.setRequestHeader).toHaveBeenCalled();
return expect(b._xhr.send).toHaveBeenCalled();
});
});