-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathangular-websocket-mock.js
139 lines (115 loc) · 3.42 KB
/
angular-websocket-mock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(['module', 'angular'], factory);
} else if (typeof exports !== "undefined") {
factory(module, require('angular'));
} else {
var mod = {
exports: {}
};
factory(mod, global.angular);
global.angularWebsocketMock = mod.exports;
}
})(this, function (module, _angular) {
'use strict';
var _angular2 = _interopRequireDefault(_angular);
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function $WebSocketBackend() {
var connectQueue = [];
var pendingConnects = [];
var closeQueue = [];
var pendingCloses = [];
var sendQueue = [];
var pendingSends = [];
var mock = false;
function $MockWebSocket(url, protocols) {
this.protocols = protocols;
this.ssl = /(wss)/i.test(this.url);
}
$MockWebSocket.prototype.send = function (msg) {
pendingSends.push(msg);
};
this.mockSend = function () {
if (mock) {
return sendQueue.shift();
}
};
this.mock = function () {
mock = true;
};
this.isMocked = function () {
return mock;
};
this.isConnected = function (url) {
return connectQueue.indexOf(url) > -1;
};
$MockWebSocket.prototype.close = function () {
pendingCloses.push(true);
};
function createWebSocketBackend(url, protocols) {
pendingConnects.push(url);
// pendingConnects.push({
// url: url,
// protocols: protocols
// });
if (protocols) {
return new $MockWebSocket(url, protocols);
}
return new $MockWebSocket(url);
}
this.create = createWebSocketBackend;
this.createWebSocketBackend = createWebSocketBackend;
this.flush = function () {
var url, msg, config;
while (url = pendingConnects.shift()) {
var i = connectQueue.indexOf(url);
if (i > -1) {
connectQueue.splice(i, 1);
}
// if (config && config.url) {
// }
}
while (pendingCloses.shift()) {
closeQueue.shift();
}
while (msg = pendingSends.shift()) {
var j;
sendQueue.forEach(function (pending, i) {
if (pending.message === msg.message) {
j = i;
}
});
if (j > -1) {
sendQueue.splice(j, 1);
}
}
};
this.expectConnect = function (url, protocols) {
connectQueue.push(url);
// connectQueue.push({url: url, protocols: protocols});
};
this.expectClose = function () {
closeQueue.push(true);
};
this.expectSend = function (msg) {
sendQueue.push(msg);
};
this.verifyNoOutstandingExpectation = function () {
if (connectQueue.length || closeQueue.length || sendQueue.length) {
throw new Error('Requests waiting to be flushed');
}
};
this.verifyNoOutstandingRequest = function () {
if (pendingConnects.length || pendingCloses.length || pendingSends.length) {
throw new Error('Requests waiting to be processed');
}
};
} // end $WebSocketBackend
_angular2.default.module('ngWebSocketMock', []).service('WebSocketBackend', $WebSocketBackend).service('$websocketBackend', $WebSocketBackend);
_angular2.default.module('angular-websocket-mock', ['ngWebSocketMock']);
module.exports = _angular2.default.module('ngWebSocketMock');
});