Lightly massage some of the generated JavaScript

This commit is contained in:
Arlo Breault 2019-07-06 15:20:07 +02:00
parent 31ad9566e6
commit 1867a3f121
19 changed files with 986 additions and 989 deletions

View file

@ -1,39 +1,32 @@
// 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;
}
// fake xhr
// class XMLHttpRequest
class XMLHttpRequest {
constructor() {
this.onreadystatechange = null;
}
open() {}
setRequestHeader() {}
send() {}
};
open() {}
XMLHttpRequest.prototype.DONE = 1;
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();
expect(b.id).not.toBeNull();
});
describe('getClientOffer', function() {
it('polls and promises a client offer', function(done) {
var b, poll;
b = new Broker('fake');
@ -55,6 +48,7 @@ describe('Broker', function() {
return done();
});
});
it('rejects if the broker timed-out', function(done) {
var b, poll;
b = new Broker('fake');
@ -75,7 +69,8 @@ describe('Broker', function() {
return done();
});
});
return it('rejects on any other status', function(done) {
it('rejects on any other status', function(done) {
var b, poll;
b = new Broker('fake');
// fake timed-out request from broker
@ -95,18 +90,20 @@ describe('Broker', function() {
expect(b._xhr.status).toBe(1337);
return done();
});
});
});
it('responds to the broker with answer', function() {
var b;
b = new Broker('fake');
var b = new Broker('fake');
spyOn(b, '_postRequest');
b.sendAnswer('fake id', 123);
return expect(b._postRequest).toHaveBeenCalledWith('fake id', jasmine.any(Object), 'answer', '123');
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');
it('POST XMLHttpRequests to the broker', function() {
var b = new Broker('fake');
b._xhr = new XMLHttpRequest();
spyOn(b._xhr, 'open');
spyOn(b._xhr, 'setRequestHeader');
@ -114,6 +111,7 @@ describe('Broker', function() {
b._postRequest(0, b._xhr, 'test', 'data');
expect(b._xhr.open).toHaveBeenCalled();
expect(b._xhr.setRequestHeader).toHaveBeenCalled();
return expect(b._xhr.send).toHaveBeenCalled();
expect(b._xhr.send).toHaveBeenCalled();
});
});

View file

@ -1,8 +1,6 @@
// Generated by CoffeeScript 2.4.1
// Fake snowflake to interact with
var snowflake;
snowflake = {
var snowflake = {
ui: new UI,
broker: {
sendAnswer: function() {}
@ -11,24 +9,25 @@ snowflake = {
};
describe('Init', function() {
it('gives a dialog when closing, only while active', function() {
var msg, silenceNotifications;
silenceNotifications = false;
snowflake.state = Snowflake.MODE.WEBRTC_READY;
msg = window.onbeforeunload();
var msg = window.onbeforeunload();
expect(snowflake.state).toBe(Snowflake.MODE.WEBRTC_READY);
expect(msg).toBe(Snowflake.MESSAGE.CONFIRMATION);
snowflake.state = Snowflake.MODE.INIT;
msg = window.onbeforeunload();
expect(snowflake.state).toBe(Snowflake.MODE.INIT);
return expect(msg).toBe(null);
expect(msg).toBe(null);
});
return it('does not give a dialog when silent flag is on', function() {
var msg, silenceNotifications;
it('does not give a dialog when silent flag is on', function() {
silenceNotifications = true;
snowflake.state = Snowflake.MODE.WEBRTC_READY;
msg = window.onbeforeunload();
var msg = window.onbeforeunload();
expect(snowflake.state).toBe(Snowflake.MODE.WEBRTC_READY);
return expect(msg).toBe(null);
expect(msg).toBe(null);
});
});

View file

@ -1,17 +1,15 @@
// Generated by CoffeeScript 2.4.1
/*
jasmine tests for Snowflake proxypair
*/
var MessageEvent, arrayMatching;
// Replacement for MessageEvent constructor.
// https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/MessageEvent
MessageEvent = function(type, init) {
var MessageEvent = function(type, init) {
return init;
};
// Asymmetic matcher that checks that two arrays have the same contents.
arrayMatching = function(sample) {
var arrayMatching = function(sample) {
return {
asymmetricMatch: function(other) {
var _, a, b, i, j, len;
@ -35,46 +33,57 @@ arrayMatching = function(sample) {
};
describe('ProxyPair', function() {
var config, destination, fakeRelay, pp, rateLimit;
fakeRelay = Parse.address('0.0.0.0:12345');
rateLimit = new DummyRateLimit;
config = new Config;
destination = [];
// Using the mock PeerConnection definition from spec/snowflake.spec.coffee.
pp = new ProxyPair(fakeRelay, rateLimit, config.pcConfig);
var pp = new ProxyPair(fakeRelay, rateLimit, config.pcConfig);
beforeEach(function() {
return pp.begin();
});
it('begins webrtc connection', function() {
return expect(pp.pc).not.toBeNull();
});
describe('accepts WebRTC offer from some client', function() {
beforeEach(function() {
return pp.begin();
});
it('rejects invalid offers', function() {
expect(typeof pp.pc.setRemoteDescription).toBe("function");
expect(pp.pc).not.toBeNull();
expect(pp.receiveWebRTCOffer({})).toBe(false);
return expect(pp.receiveWebRTCOffer({
expect(pp.receiveWebRTCOffer({
type: 'answer'
})).toBe(false);
});
return it('accepts valid offers', function() {
it('accepts valid offers', function() {
expect(pp.pc).not.toBeNull();
return expect(pp.receiveWebRTCOffer({
expect(pp.receiveWebRTCOffer({
type: 'offer',
sdp: 'foo'
})).toBe(true);
});
});
it('responds with a WebRTC answer correctly', function() {
spyOn(snowflake.broker, 'sendAnswer');
pp.pc.onicecandidate({
candidate: null
});
return expect(snowflake.broker.sendAnswer).toHaveBeenCalled();
expect(snowflake.broker.sendAnswer).toHaveBeenCalled();
});
it('handles a new data channel correctly', function() {
expect(pp.client).toBeNull();
pp.pc.ondatachannel({
@ -84,21 +93,25 @@ describe('ProxyPair', function() {
expect(pp.client.onopen).not.toBeNull();
expect(pp.client.onclose).not.toBeNull();
expect(pp.client.onerror).not.toBeNull();
return expect(pp.client.onmessage).not.toBeNull();
expect(pp.client.onmessage).not.toBeNull();
});
it('connects to the relay once datachannel opens', function() {
spyOn(pp, 'connectRelay');
pp.client.onopen();
return expect(pp.connectRelay).toHaveBeenCalled();
expect(pp.connectRelay).toHaveBeenCalled();
});
it('connects to a relay', function() {
pp.connectRelay();
expect(pp.relay.onopen).not.toBeNull();
expect(pp.relay.onclose).not.toBeNull();
expect(pp.relay.onerror).not.toBeNull();
return expect(pp.relay.onmessage).not.toBeNull();
expect(pp.relay.onmessage).not.toBeNull();
});
return describe('flushes data between client and relay', function() {
describe('flushes data between client and relay', function() {
it('proxies data from client to relay', function() {
var msg;
pp.pc.ondatachannel({
@ -116,8 +129,9 @@ describe('ProxyPair', function() {
pp.onClientToRelayMessage(msg);
pp.flush();
expect(pp.client.send).not.toHaveBeenCalled();
return expect(pp.relay.send).toHaveBeenCalledWith(arrayMatching([1, 2, 3]));
expect(pp.relay.send).toHaveBeenCalledWith(arrayMatching([1, 2, 3]));
});
it('proxies data from relay to client', function() {
var msg;
spyOn(pp.client, 'send');
@ -128,16 +142,19 @@ describe('ProxyPair', function() {
pp.onRelayToClientMessage(msg);
pp.flush();
expect(pp.client.send).toHaveBeenCalledWith(arrayMatching([4, 5, 6]));
return expect(pp.relay.send).not.toHaveBeenCalled();
expect(pp.relay.send).not.toHaveBeenCalled();
});
return it('sends nothing with nothing to flush', function() {
it('sends nothing with nothing to flush', function() {
spyOn(pp.client, 'send');
spyOn(pp.relay, 'send');
pp.flush();
expect(pp.client.send).not.toHaveBeenCalled();
return expect(pp.relay.send).not.toHaveBeenCalled();
expect(pp.relay.send).not.toHaveBeenCalled();
});
});
});
// TODO: rate limit tests

View file

@ -1,62 +1,43 @@
// Generated by CoffeeScript 2.4.1
/*
jasmine tests for Snowflake
*/
var FakeBroker, PeerConnection, SessionDescription, WebSocket, config, log, ui;
// Fake browser functionality:
PeerConnection = class PeerConnection {
class PeerConnection {
setRemoteDescription() {
return true;
}
send(data) {}
};
SessionDescription = (function() {
class SessionDescription {};
class SessionDescription {};
SessionDescription.prototype.type = 'offer';
SessionDescription.prototype.type = 'offer';
class WebSocket {
constructor() {
this.bufferedAmount = 0;
}
send(data) {}
};
WebSocket.prototype.OPEN = 1;
WebSocket.prototype.CLOSED = 0;
return SessionDescription;
var log = function() {};
}).call(this);
var config = new Config;
WebSocket = (function() {
class WebSocket {
constructor() {
this.bufferedAmount = 0;
}
var ui = new UI;
send(data) {}
};
WebSocket.prototype.OPEN = 1;
WebSocket.prototype.CLOSED = 0;
return WebSocket;
}).call(this);
log = function() {};
config = new Config;
ui = new UI;
FakeBroker = class FakeBroker {
class FakeBroker {
getClientOffer() {
return new Promise(function(F, R) {
return {};
});
}
};
describe('Snowflake', function() {
it('constructs correctly', function() {
var s;
s = new Snowflake(config, ui, {
@ -67,22 +48,25 @@ describe('Snowflake', function() {
fake: 'broker'
});
expect(s.ui).not.toBeNull();
return expect(s.retries).toBe(0);
expect(s.retries).toBe(0);
});
it('sets relay address correctly', function() {
var s;
s = new Snowflake(config, ui, null);
s.setRelayAddr('foo');
return expect(s.relayAddr).toEqual('foo');
expect(s.relayAddr).toEqual('foo');
});
it('initalizes WebRTC connection', function() {
var s;
s = new Snowflake(config, ui, new FakeBroker());
spyOn(s.broker, 'getClientOffer').and.callThrough();
s.beginWebRTC();
expect(s.retries).toBe(1);
return expect(s.broker.getClientOffer).toHaveBeenCalled();
expect(s.broker.getClientOffer).toHaveBeenCalled();
});
it('receives SDP offer and sends answer', function() {
var pair, s;
s = new Snowflake(config, ui, new FakeBroker());
@ -92,8 +76,9 @@ describe('Snowflake', function() {
spyOn(pair, 'receiveWebRTCOffer').and.returnValue(true);
spyOn(s, 'sendAnswer');
s.receiveOffer(pair, '{"type":"offer","sdp":"foo"}');
return expect(s.sendAnswer).toHaveBeenCalled();
expect(s.sendAnswer).toHaveBeenCalled();
});
it('does not send answer when receiving invalid offer', function() {
var pair, s;
s = new Snowflake(config, ui, new FakeBroker());
@ -103,12 +88,14 @@ describe('Snowflake', function() {
spyOn(pair, 'receiveWebRTCOffer').and.returnValue(false);
spyOn(s, 'sendAnswer');
s.receiveOffer(pair, '{"type":"not a good offer","sdp":"foo"}');
return expect(s.sendAnswer).not.toHaveBeenCalled();
expect(s.sendAnswer).not.toHaveBeenCalled();
});
return it('can make a proxypair', function() {
it('can make a proxypair', function() {
var s;
s = new Snowflake(config, ui, new FakeBroker());
s.makeProxyPair();
return expect(s.proxyPairs.length).toBe(1);
expect(s.proxyPairs.length).toBe(1);
});
});

View file

@ -1,10 +1,7 @@
// Generated by CoffeeScript 2.4.1
/*
jasmine tests for Snowflake UI
*/
var document;
document = {
var document = {
getElementById: function(id) {
return {};
},
@ -14,6 +11,7 @@ document = {
};
describe('UI', function() {
it('activates debug mode when badge does not exist', function() {
var u;
spyOn(document, 'getElementById').and.callFake(function(id) {
@ -25,8 +23,9 @@ describe('UI', function() {
u = new DebugUI();
expect(document.getElementById.calls.count()).toEqual(2);
expect(u.$status).not.toBeNull();
return expect(u.$msglog).not.toBeNull();
expect(u.$msglog).not.toBeNull();
});
it('is not debug mode when badge exists', function() {
var u;
spyOn(document, 'getElementById').and.callFake(function(id) {
@ -38,8 +37,9 @@ describe('UI', function() {
u = new BadgeUI();
expect(document.getElementById).toHaveBeenCalled();
expect(document.getElementById.calls.count()).toEqual(1);
return expect(u.$badge).not.toBeNull();
expect(u.$badge).not.toBeNull();
});
it('sets status message when in debug mode', function() {
var u;
u = new DebugUI();
@ -50,16 +50,18 @@ describe('UI', function() {
}
};
u.setStatus('test');
return expect(u.$status.innerHTML).toEqual('Status: test');
expect(u.$status.innerHTML).toEqual('Status: test');
});
it('sets message log css correctly for debug mode', function() {
var u;
u = new DebugUI();
u.setActive(true);
expect(u.$msglog.className).toEqual('active');
u.setActive(false);
return expect(u.$msglog.className).toEqual('');
expect(u.$msglog.className).toEqual('');
});
it('sets badge css correctly for non-debug mode', function() {
var u;
u = new BadgeUI();
@ -67,9 +69,10 @@ describe('UI', function() {
u.setActive(true);
expect(u.$badge.className).toEqual('active');
u.setActive(false);
return expect(u.$badge.className).toEqual('');
expect(u.$badge.className).toEqual('');
});
return it('logs to the textarea correctly when debug mode', function() {
it('logs to the textarea correctly when debug mode', function() {
var u;
u = new DebugUI();
u.$msglog = {
@ -79,6 +82,7 @@ describe('UI', function() {
};
u.log('test');
expect(u.$msglog.value).toEqual('test\n');
return expect(u.$msglog.scrollTop).toEqual(1337);
expect(u.$msglog.scrollTop).toEqual(1337);
});
});

View file

@ -1,10 +1,12 @@
// Generated by CoffeeScript 2.4.1
/*
jasmine tests for Snowflake utils
*/
describe('Parse', function() {
describe('cookie', function() {
return it('parses correctly', function() {
it('parses correctly', function() {
expect(Parse.cookie('')).toEqual({});
expect(Parse.cookie('a=b')).toEqual({
a: 'b'
@ -30,12 +32,15 @@ describe('Parse', function() {
expect(Parse.cookie('key=%26%20')).toEqual({
key: '& '
});
return expect(Parse.cookie('a=\'\'')).toEqual({
expect(Parse.cookie('a=\'\'')).toEqual({
a: '\'\''
});
});
});
describe('address', function() {
it('parses IPv4', function() {
expect(Parse.address('')).toBeNull();
expect(Parse.address('3.3.3.3:4444')).toEqual({
@ -45,9 +50,10 @@ describe('Parse', function() {
expect(Parse.address('3.3.3.3')).toBeNull();
expect(Parse.address('3.3.3.3:0x1111')).toBeNull();
expect(Parse.address('3.3.3.3:-4444')).toBeNull();
return expect(Parse.address('3.3.3.3:65536')).toBeNull();
expect(Parse.address('3.3.3.3:65536')).toBeNull();
});
return it('parses IPv6', function() {
it('parses IPv6', function() {
expect(Parse.address('[1:2::a:f]:4444')).toEqual({
host: '1:2::a:f',
port: 4444
@ -56,15 +62,17 @@ describe('Parse', function() {
expect(Parse.address('[1:2::a:f]:0x1111')).toBeNull();
expect(Parse.address('[1:2::a:f]:-4444')).toBeNull();
expect(Parse.address('[1:2::a:f]:65536')).toBeNull();
return expect(Parse.address('[1:2::ffff:1.2.3.4]:4444')).toEqual({
expect(Parse.address('[1:2::ffff:1.2.3.4]:4444')).toEqual({
host: '1:2::ffff:1.2.3.4',
port: 4444
});
});
});
return describe('ipFromSDP', function() {
var testCases;
testCases = [
describe('ipFromSDP', function() {
var testCases = [
{
// https://tools.ietf.org/html/rfc4566#section-5
sdp: "v=0\no=jdoe 2890844526 2890842807 IN IP4 10.47.16.5\ns=SDP Seminar\ni=A Seminar on the session description protocol\nu=http://www.example.com/seminars/sdp.pdf\ne=j.doe@example.com (Jane Doe)\nc=IN IP4 224.2.17.12/127\nt=2873397496 2873404696\na=recvonly\nm=audio 49170 RTP/AVP 0\nm=video 51372 RTP/AVP 99\na=rtpmap:99 h263-1998/90000",
@ -128,7 +136,8 @@ describe('Parse', function() {
expected: void 0
}
];
return it('parses SDP', function() {
it('parses SDP', function() {
var i, len, ref, ref1, results, test;
results = [];
for (i = 0, len = testCases.length; i < len; i++) {
@ -143,10 +152,13 @@ describe('Parse', function() {
}
return results;
});
});
});
describe('query string', function() {
it('should parse correctly', function() {
expect(Query.parse('')).toEqual({});
expect(Query.parse('a=b')).toEqual({
@ -178,11 +190,12 @@ describe('query string', function() {
expect(Query.parse('a+b=c')).toEqual({
'a b': 'c'
});
return expect(Query.parse('a=b+c+d')).toEqual({
expect(Query.parse('a=b+c+d')).toEqual({
a: 'b c d'
});
});
return it('uses the first appearance of duplicate key', function() {
it('uses the first appearance of duplicate key', function() {
expect(Query.parse('a=b&c=d&a=e')).toEqual({
a: 'b',
c: 'd'
@ -201,21 +214,24 @@ describe('query string', function() {
a: 'b',
'': ''
});
return expect(Query.parse('a=b&&c=d')).toEqual({
expect(Query.parse('a=b&&c=d')).toEqual({
a: 'b',
'': '',
c: 'd'
});
});
});
describe('Params', function() {
describe('bool', function() {
var getBool;
getBool = function(query) {
var getBool = function(query) {
return Params.getBool(Query.parse(query), 'param', false);
};
return it('parses correctly', function() {
it('parses correctly', function() {
expect(getBool('param=true')).toBe(true);
expect(getBool('param')).toBe(true);
expect(getBool('param=')).toBe(true);
@ -223,19 +239,23 @@ describe('Params', function() {
expect(getBool('param=0')).toBe(false);
expect(getBool('param=false')).toBe(false);
expect(getBool('param=unexpected')).toBeNull();
return expect(getBool('pram=true')).toBe(false);
expect(getBool('pram=true')).toBe(false);
});
});
return describe('address', function() {
var DEFAULT, getAddress;
DEFAULT = {
describe('address', function() {
var DEFAULT = {
host: '1.1.1.1',
port: 2222
};
getAddress = function(query) {
var getAddress = function(query) {
return Params.getAddress(query, 'addr', DEFAULT);
};
return it('parses correctly', function() {
it('parses correctly', function() {
expect(getAddress({})).toEqual(DEFAULT);
expect(getAddress({
addr: '3.3.3.3:4444'
@ -246,9 +266,11 @@ describe('Params', function() {
expect(getAddress({
x: '3.3.3.3:4444'
})).toEqual(DEFAULT);
return expect(getAddress({
expect(getAddress({
addr: '---'
})).toBeNull();
});
});
});

View file

@ -1,32 +1,39 @@
// Generated by CoffeeScript 2.4.1
/*
jasmine tests for Snowflake websocket
*/
describe('BuildUrl', function() {
it('should parse just protocol and host', function() {
return expect(WS.buildUrl('http', 'example.com')).toBe('http://example.com');
expect(WS.buildUrl('http', 'example.com')).toBe('http://example.com');
});
it('should handle different ports', function() {
expect(WS.buildUrl('http', 'example.com', 80)).toBe('http://example.com');
expect(WS.buildUrl('http', 'example.com', 81)).toBe('http://example.com:81');
expect(WS.buildUrl('http', 'example.com', 443)).toBe('http://example.com:443');
return expect(WS.buildUrl('http', 'example.com', 444)).toBe('http://example.com:444');
expect(WS.buildUrl('http', 'example.com', 444)).toBe('http://example.com:444');
});
it('should handle paths', function() {
expect(WS.buildUrl('http', 'example.com', 80, '/')).toBe('http://example.com/');
expect(WS.buildUrl('http', 'example.com', 80, '/test?k=%#v')).toBe('http://example.com/test%3Fk%3D%25%23v');
return expect(WS.buildUrl('http', 'example.com', 80, '/test')).toBe('http://example.com/test');
expect(WS.buildUrl('http', 'example.com', 80, '/test')).toBe('http://example.com/test');
});
it('should handle params', function() {
expect(WS.buildUrl('http', 'example.com', 80, '/test', [['k', '%#v']])).toBe('http://example.com/test?k=%25%23v');
return expect(WS.buildUrl('http', 'example.com', 80, '/test', [['a', 'b'], ['c', 'd']])).toBe('http://example.com/test?a=b&c=d');
expect(WS.buildUrl('http', 'example.com', 80, '/test', [['a', 'b'], ['c', 'd']])).toBe('http://example.com/test?a=b&c=d');
});
it('should handle ips', function() {
expect(WS.buildUrl('http', '1.2.3.4')).toBe('http://1.2.3.4');
return expect(WS.buildUrl('http', '1:2::3:4')).toBe('http://[1:2::3:4]');
expect(WS.buildUrl('http', '1:2::3:4')).toBe('http://[1:2::3:4]');
});
return it('should handle bogus', function() {
it('should handle bogus', function() {
expect(WS.buildUrl('http', 'bog][us')).toBe('http://bog%5D%5Bus');
return expect(WS.buildUrl('http', 'bog:u]s')).toBe('http://bog%3Au%5Ds');
expect(WS.buildUrl('http', 'bog:u]s')).toBe('http://bog%3Au%5Ds');
});
});