-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathajax-helper.ts
255 lines (211 loc) · 5.81 KB
/
ajax-helper.ts
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
declare const __root__: any;
export class MockWebSocket {
static sockets: Array<MockWebSocket> = [];
static get lastSocket(): MockWebSocket {
const socket = MockWebSocket.sockets;
const length = socket.length;
return length > 0 ? socket[length - 1] : undefined;
}
static clearSockets(): void {
MockWebSocket.sockets.length = 0;
}
sent: Array<any> = [];
handlers: any = {};
readyState: number = 0;
closeCode: any;
closeReason: any;
constructor(public url: string, public protocol: string) {
MockWebSocket.sockets.push(this);
}
send(data: any): void {
this.sent.push(data);
}
get lastMessageSent(): any {
const sent = this.sent;
const length = sent.length;
return length > 0 ? sent[length - 1] : undefined;
}
triggerClose(e: any): void {
this.readyState = 3;
this.trigger('close', e);
}
triggerError(err: any): void {
this.readyState = 3;
this.trigger('error', err);
}
triggerMessage(data: any): void {
const messageEvent = {
data: data,
origin: 'mockorigin',
ports: undefined,
source: __root__,
};
this.trigger('message', messageEvent);
}
open(): void {
this.readyState = 1;
this.trigger('open', {});
}
close(code: any, reason: any): void {
if (this.readyState < 2) {
this.readyState = 2;
this.closeCode = code;
this.closeReason = reason;
this.triggerClose({ wasClean: true });
}
}
addEventListener(name: string, handler: any): void {
const lookup = this.handlers[name] = this.handlers[name] || [];
lookup.push(handler);
}
removeEventListener(name: string, handler: any): void {
const lookup = this.handlers[name];
if (lookup) {
for (let i = lookup.length - 1; i--; ) {
if (lookup[i] === handler) {
lookup.splice(i, 1);
}
}
}
}
trigger(name: string, e: any) {
if (this['on' + name]) {
this['on' + name](e);
}
const lookup = this.handlers[name];
if (lookup) {
for (let i = 0; i < lookup.length; i++) {
lookup[i](e);
}
}
}
}
export class MockXMLHttpRequest {
private static requests: Array<MockXMLHttpRequest> = [];
private static recentRequest: MockXMLHttpRequest;
static get mostRecent(): MockXMLHttpRequest {
return MockXMLHttpRequest.recentRequest;
}
static get allRequests(): Array<MockXMLHttpRequest> {
return MockXMLHttpRequest.requests;
}
static clearRequest(): void {
MockXMLHttpRequest.requests.length = 0;
MockXMLHttpRequest.recentRequest = null;
}
private previousRequest: MockXMLHttpRequest;
protected responseType: string = '';
private eventHandlers: Array<any> = [];
private readyState: number = 0;
private user: any;
private password: any;
private responseHeaders: any;
protected status: any;
protected responseText: string;
protected response: any;
url: any;
method: any;
data: any;
requestHeaders: any = {};
withCredentials: boolean = false;
constructor() {
this.previousRequest = MockXMLHttpRequest.recentRequest;
MockXMLHttpRequest.recentRequest = this;
MockXMLHttpRequest.requests.push(this);
}
send(data: any): void {
this.data = data;
}
open(method: any, url: any, async: any, user: any, password: any): void {
this.method = method;
this.url = url;
this.user = user;
this.password = password;
this.readyState = 1;
this.triggerEvent('readyStateChange');
}
setRequestHeader(key: any, value: any): void {
this.requestHeaders[key] = value;
}
addEventListener(name: string, handler: any): void {
this.eventHandlers.push({ name: name, handler: handler });
}
removeEventListener(name: string, handler: any): void {
for (let i = this.eventHandlers.length - 1; i--; ) {
let eh = this.eventHandlers[i];
if (eh.name === name && eh.handler === handler) {
this.eventHandlers.splice(i, 1);
}
}
}
throwError(err: any): void {
// TODO: something better with errors
this.triggerEvent('error');
}
protected jsonResponseValue(response: any) {
try {
this.response = JSON.parse(response.responseText);
} catch (err) {
throw new Error('unable to JSON.parse: \n' + response.responseText);
}
}
protected defaultResponseValue() {
throw new Error('unhandled type "' + this.responseType + '"');
}
respondWith(response: any): void {
this.readyState = 4;
this.responseHeaders = {
'Content-Type': response.contentType || 'text/plain'
};
this.status = response.status || 200;
this.responseText = response.responseText;
if (!('response' in response)) {
switch (this.responseType) {
case 'json':
this.jsonResponseValue(response);
break;
case 'text':
this.response = response.responseText;
break;
default:
this.defaultResponseValue();
}
}
// TODO: pass better event to onload.
this.triggerEvent('load');
this.triggerEvent('readystatechange');
}
triggerEvent(name: any, eventObj?: any): void {
// TODO: create a better default event
const e: any = eventObj || {};
if (this['on' + name]) {
this['on' + name](e);
}
this.eventHandlers.forEach(function (eh) {
if (eh.name === name) {
eh.handler.call(this, e);
}
});
}
}
export class MockXMLHttpRequestInternetExplorer extends MockXMLHttpRequest {
private mockHttp204() {
this.responseType = '';
this.responseText = '';
this.response = '';
}
protected jsonResponseValue(response: any) {
if (this.status == 204) {
this.mockHttp204();
return;
}
return super.jsonResponseValue(response);
}
protected defaultResponseValue() {
if (this.status == 204) {
this.mockHttp204();
return;
}
return super.defaultResponseValue();
}
}