ember-data-factory-guy 0.3.9 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/bower.json +1 -1
- data/dist/ember-data-factory-guy.js +15 -10
- data/dist/ember-data-factory-guy.min.js +1 -1
- data/ember-data-factory-guy.gemspec +1 -1
- data/package.json +8 -7
- data/src/factory_guy_test_mixin.js +4 -5
- data/src/store.js +11 -5
- data/tests/active_model_adapter_factory_test.js +27 -0
- data/tests/factory_guy_test.js +2 -2
- data/tests/rest_adapter_factory_test.js +14 -0
- data/tests/support/factories/material_factory.js +8 -0
- data/tests/support/factories/profile_factory.js +3 -1
- data/tests/support/libs/mockjax.js +600 -0
- data/tests/support/models/hat.js +4 -1
- data/tests/support/models/material.js +4 -0
- data/tests/support/models/profile.js +4 -2
- data/tests/support/test_helper.js +4 -0
- data/tests/test_setup.js +624 -4
- data/vendor/assets/javascripts/ember_data_factory_guy.js +15 -10
- metadata +6 -3
data/tests/support/models/hat.js
CHANGED
@@ -6,12 +6,16 @@ TestHelper = Ember.Object.createWithMixins(FactoryGuyTestMixin,{
|
|
6
6
|
* @param adapter DS.FixtureAdapter or DS.ActiveModelSerializer or whatever
|
7
7
|
*/
|
8
8
|
setup: function(adapter) {
|
9
|
+
$.mockjaxSettings.logging = false;
|
10
|
+
$.mockjaxSettings.responseTime = 0;
|
11
|
+
|
9
12
|
var container = new Ember.Container();
|
10
13
|
this.set('container', container);
|
11
14
|
|
12
15
|
container.register("model:hat", Hat);
|
13
16
|
container.register("model:small_hat", SmallHat);
|
14
17
|
container.register("model:big_hat", BigHat);
|
18
|
+
container.register("model:soft_material", SoftMaterial);
|
15
19
|
container.register("model:user", User);
|
16
20
|
container.register("model:profile", Profile);
|
17
21
|
container.register("model:company", Company);
|
data/tests/test_setup.js
CHANGED
@@ -12,9 +12,19 @@ FactoryGuy.define('hat', {
|
|
12
12
|
type: 'BigHat'
|
13
13
|
}
|
14
14
|
})
|
15
|
+
FactoryGuy.define('soft_material', {
|
16
|
+
default: {
|
17
|
+
name: 'Soft material'
|
18
|
+
},
|
19
|
+
silk: {
|
20
|
+
name: 'silk'
|
21
|
+
}
|
22
|
+
})
|
15
23
|
FactoryGuy.define('profile', {
|
16
24
|
default: {
|
17
|
-
description: 'Text goes here'
|
25
|
+
description: 'Text goes here',
|
26
|
+
camelCaseDescription: 'boyThisIsSilly',
|
27
|
+
snake_case_description: 'how_do_birds_find_worms'
|
18
28
|
}
|
19
29
|
})
|
20
30
|
FactoryGuy.define('project', {
|
@@ -60,13 +70,23 @@ Hat = DS.Model.extend({
|
|
60
70
|
hats: DS.hasMany('hat', {inverse: 'hat', polymorphic: true})
|
61
71
|
});
|
62
72
|
|
63
|
-
BigHat = Hat.extend(
|
73
|
+
BigHat = Hat.extend({
|
74
|
+
materials: DS.hasMany('soft_material')
|
75
|
+
});
|
64
76
|
SmallHat = Hat.extend();
|
65
77
|
|
66
78
|
|
79
|
+
|
80
|
+
SoftMaterial = DS.Model.extend({
|
81
|
+
name: DS.attr('string'),
|
82
|
+
hat: DS.belongsTo('big_hat')
|
83
|
+
})
|
84
|
+
|
67
85
|
Profile = DS.Model.extend({
|
68
|
-
description:
|
69
|
-
|
86
|
+
description: DS.attr('string'),
|
87
|
+
camelCaseDescription: DS.attr('string'),
|
88
|
+
snake_case_description: DS.attr('string'),
|
89
|
+
company: DS.belongsTo('company')
|
70
90
|
});
|
71
91
|
|
72
92
|
Project = DS.Model.extend({
|
@@ -82,6 +102,606 @@ User = DS.Model.extend({
|
|
82
102
|
projects: DS.hasMany('project'),
|
83
103
|
hats: DS.hasMany('hat', {polymorphic: true})
|
84
104
|
});
|
105
|
+
/*!
|
106
|
+
* MockJax - jQuery Plugin to Mock Ajax requests
|
107
|
+
*
|
108
|
+
* Version: 1.5.3
|
109
|
+
* Released:
|
110
|
+
* Home: http://github.com/appendto/jquery-mockjax
|
111
|
+
* Author: Jonathan Sharp (http://jdsharp.com)
|
112
|
+
* License: MIT,GPL
|
113
|
+
*
|
114
|
+
* Copyright (c) 2011 appendTo LLC.
|
115
|
+
* Dual licensed under the MIT or GPL licenses.
|
116
|
+
* http://appendto.com/open-source-licenses
|
117
|
+
*/
|
118
|
+
(function($) {
|
119
|
+
var _ajax = $.ajax,
|
120
|
+
mockHandlers = [],
|
121
|
+
mockedAjaxCalls = [],
|
122
|
+
CALLBACK_REGEX = /=\?(&|$)/,
|
123
|
+
jsc = (new Date()).getTime();
|
124
|
+
|
125
|
+
|
126
|
+
// Parse the given XML string.
|
127
|
+
function parseXML(xml) {
|
128
|
+
if ( window.DOMParser == undefined && window.ActiveXObject ) {
|
129
|
+
DOMParser = function() { };
|
130
|
+
|
131
|
+
DOMParser.prototype.parseFromString = function( xmlString ) {
|
132
|
+
var doc = new ActiveXObject('Microsoft.XMLDOM');
|
133
|
+
doc.async = 'false';
|
134
|
+
doc.loadXML( xmlString );
|
135
|
+
return doc;
|
136
|
+
};
|
137
|
+
}
|
138
|
+
|
139
|
+
try {
|
140
|
+
var xmlDoc = ( new DOMParser() ).parseFromString( xml, 'text/xml' );
|
141
|
+
if ( $.isXMLDoc( xmlDoc ) ) {
|
142
|
+
var err = $('parsererror', xmlDoc);
|
143
|
+
if ( err.length == 1 ) {
|
144
|
+
throw('Error: ' + $(xmlDoc).text() );
|
145
|
+
}
|
146
|
+
} else {
|
147
|
+
throw('Unable to parse XML');
|
148
|
+
}
|
149
|
+
return xmlDoc;
|
150
|
+
} catch( e ) {
|
151
|
+
var msg = ( e.name == undefined ? e : e.name + ': ' + e.message );
|
152
|
+
$(document).trigger('xmlParseError', [ msg ]);
|
153
|
+
return undefined;
|
154
|
+
}
|
155
|
+
}
|
156
|
+
|
157
|
+
// Trigger a jQuery event
|
158
|
+
function trigger(s, type, args) {
|
159
|
+
(s.context ? $(s.context) : $.event).trigger(type, args);
|
160
|
+
}
|
161
|
+
|
162
|
+
// Check if the data field on the mock handler and the request match. This
|
163
|
+
// can be used to restrict a mock handler to being used only when a certain
|
164
|
+
// set of data is passed to it.
|
165
|
+
function isMockDataEqual( mock, live ) {
|
166
|
+
var identical = true;
|
167
|
+
// Test for situations where the data is a querystring (not an object)
|
168
|
+
if (typeof live === 'string') {
|
169
|
+
// Querystring may be a regex
|
170
|
+
return $.isFunction( mock.test ) ? mock.test(live) : mock == live;
|
171
|
+
}
|
172
|
+
$.each(mock, function(k) {
|
173
|
+
if ( live[k] === undefined ) {
|
174
|
+
identical = false;
|
175
|
+
return identical;
|
176
|
+
} else {
|
177
|
+
// This will allow to compare Arrays
|
178
|
+
if ( typeof live[k] === 'object' && live[k] !== null ) {
|
179
|
+
identical = identical && isMockDataEqual(mock[k], live[k]);
|
180
|
+
} else {
|
181
|
+
if ( mock[k] && $.isFunction( mock[k].test ) ) {
|
182
|
+
identical = identical && mock[k].test(live[k]);
|
183
|
+
} else {
|
184
|
+
identical = identical && ( mock[k] == live[k] );
|
185
|
+
}
|
186
|
+
}
|
187
|
+
}
|
188
|
+
});
|
189
|
+
|
190
|
+
return identical;
|
191
|
+
}
|
192
|
+
|
193
|
+
// See if a mock handler property matches the default settings
|
194
|
+
function isDefaultSetting(handler, property) {
|
195
|
+
return handler[property] === $.mockjaxSettings[property];
|
196
|
+
}
|
197
|
+
|
198
|
+
// Check the given handler should mock the given request
|
199
|
+
function getMockForRequest( handler, requestSettings ) {
|
200
|
+
|
201
|
+
// If the mock was registered with a function, let the function decide if we
|
202
|
+
// want to mock this request
|
203
|
+
if ( $.isFunction(handler) ) {
|
204
|
+
return handler( requestSettings );
|
205
|
+
}
|
206
|
+
|
207
|
+
// Inspect the URL of the request and check if the mock handler's url
|
208
|
+
// matches the url for this ajax request
|
209
|
+
if ( $.isFunction(handler.url.test) ) {
|
210
|
+
// The user provided a regex for the url, test it
|
211
|
+
if ( !handler.url.test( requestSettings.url ) ) {
|
212
|
+
return null;
|
213
|
+
}
|
214
|
+
} else {
|
215
|
+
// Look for a simple wildcard '*' or a direct URL match
|
216
|
+
var star = handler.url.indexOf('*');
|
217
|
+
if (handler.url !== requestSettings.url && star === -1 ||
|
218
|
+
!new RegExp(handler.url.replace(/[-[\]{}()+?.,\\^$|#\s]/g, "\\$&").replace(/\*/g, '.+')).test(requestSettings.url)) {
|
219
|
+
return null;
|
220
|
+
}
|
221
|
+
}
|
222
|
+
// console.log(handler.url, handler.data, requestSettings.data)
|
223
|
+
// Inspect the data submitted in the request (either POST body or GET query string)
|
224
|
+
if ( handler.data && requestSettings.data ) {
|
225
|
+
if ( !isMockDataEqual(handler.data, requestSettings.data) ) {
|
226
|
+
// They're not identical, do not mock this request
|
227
|
+
return null;
|
228
|
+
}
|
229
|
+
}
|
230
|
+
// Inspect the request type
|
231
|
+
if ( handler && handler.type &&
|
232
|
+
handler.type.toLowerCase() != requestSettings.type.toLowerCase() ) {
|
233
|
+
// The request type doesn't match (GET vs. POST)
|
234
|
+
return null;
|
235
|
+
}
|
236
|
+
|
237
|
+
return handler;
|
238
|
+
}
|
239
|
+
|
240
|
+
// Process the xhr objects send operation
|
241
|
+
function _xhrSend(mockHandler, requestSettings, origSettings) {
|
242
|
+
|
243
|
+
// This is a substitute for < 1.4 which lacks $.proxy
|
244
|
+
var process = (function(that) {
|
245
|
+
return function() {
|
246
|
+
return (function() {
|
247
|
+
var onReady;
|
248
|
+
|
249
|
+
// The request has returned
|
250
|
+
this.status = mockHandler.status;
|
251
|
+
this.statusText = mockHandler.statusText;
|
252
|
+
this.readyState = 4;
|
253
|
+
|
254
|
+
// We have an executable function, call it to give
|
255
|
+
// the mock handler a chance to update it's data
|
256
|
+
if ( $.isFunction(mockHandler.response) ) {
|
257
|
+
mockHandler.response(origSettings);
|
258
|
+
}
|
259
|
+
// Copy over our mock to our xhr object before passing control back to
|
260
|
+
// jQuery's onreadystatechange callback
|
261
|
+
if ( requestSettings.dataType == 'json' && ( typeof mockHandler.responseText == 'object' ) ) {
|
262
|
+
this.responseText = JSON.stringify(mockHandler.responseText);
|
263
|
+
} else if ( requestSettings.dataType == 'xml' ) {
|
264
|
+
if ( typeof mockHandler.responseXML == 'string' ) {
|
265
|
+
this.responseXML = parseXML(mockHandler.responseXML);
|
266
|
+
//in jQuery 1.9.1+, responseXML is processed differently and relies on responseText
|
267
|
+
this.responseText = mockHandler.responseXML;
|
268
|
+
} else {
|
269
|
+
this.responseXML = mockHandler.responseXML;
|
270
|
+
}
|
271
|
+
} else {
|
272
|
+
this.responseText = mockHandler.responseText;
|
273
|
+
}
|
274
|
+
if( typeof mockHandler.status == 'number' || typeof mockHandler.status == 'string' ) {
|
275
|
+
this.status = mockHandler.status;
|
276
|
+
}
|
277
|
+
if( typeof mockHandler.statusText === "string") {
|
278
|
+
this.statusText = mockHandler.statusText;
|
279
|
+
}
|
280
|
+
// jQuery 2.0 renamed onreadystatechange to onload
|
281
|
+
onReady = this.onreadystatechange || this.onload;
|
282
|
+
|
283
|
+
// jQuery < 1.4 doesn't have onreadystate change for xhr
|
284
|
+
if ( $.isFunction( onReady ) ) {
|
285
|
+
if( mockHandler.isTimeout) {
|
286
|
+
this.status = -1;
|
287
|
+
}
|
288
|
+
onReady.call( this, mockHandler.isTimeout ? 'timeout' : undefined );
|
289
|
+
} else if ( mockHandler.isTimeout ) {
|
290
|
+
// Fix for 1.3.2 timeout to keep success from firing.
|
291
|
+
this.status = -1;
|
292
|
+
}
|
293
|
+
}).apply(that);
|
294
|
+
};
|
295
|
+
})(this);
|
296
|
+
|
297
|
+
if ( mockHandler.proxy ) {
|
298
|
+
// We're proxying this request and loading in an external file instead
|
299
|
+
_ajax({
|
300
|
+
global: false,
|
301
|
+
url: mockHandler.proxy,
|
302
|
+
type: mockHandler.proxyType,
|
303
|
+
data: mockHandler.data,
|
304
|
+
dataType: requestSettings.dataType === "script" ? "text/plain" : requestSettings.dataType,
|
305
|
+
complete: function(xhr) {
|
306
|
+
mockHandler.responseXML = xhr.responseXML;
|
307
|
+
mockHandler.responseText = xhr.responseText;
|
308
|
+
// Don't override the handler status/statusText if it's specified by the config
|
309
|
+
if (isDefaultSetting(mockHandler, 'status')) {
|
310
|
+
mockHandler.status = xhr.status;
|
311
|
+
}
|
312
|
+
if (isDefaultSetting(mockHandler, 'statusText')) {
|
313
|
+
mockHandler.statusText = xhr.statusText;
|
314
|
+
}
|
315
|
+
|
316
|
+
this.responseTimer = setTimeout(process, mockHandler.responseTime || 0);
|
317
|
+
}
|
318
|
+
});
|
319
|
+
} else {
|
320
|
+
// type == 'POST' || 'GET' || 'DELETE'
|
321
|
+
if ( requestSettings.async === false ) {
|
322
|
+
// TODO: Blocking delay
|
323
|
+
process();
|
324
|
+
} else {
|
325
|
+
this.responseTimer = setTimeout(process, mockHandler.responseTime || 50);
|
326
|
+
}
|
327
|
+
}
|
328
|
+
}
|
329
|
+
|
330
|
+
// Construct a mocked XHR Object
|
331
|
+
function xhr(mockHandler, requestSettings, origSettings, origHandler) {
|
332
|
+
// Extend with our default mockjax settings
|
333
|
+
mockHandler = $.extend(true, {}, $.mockjaxSettings, mockHandler);
|
334
|
+
|
335
|
+
if (typeof mockHandler.headers === 'undefined') {
|
336
|
+
mockHandler.headers = {};
|
337
|
+
}
|
338
|
+
if ( mockHandler.contentType ) {
|
339
|
+
mockHandler.headers['content-type'] = mockHandler.contentType;
|
340
|
+
}
|
341
|
+
|
342
|
+
return {
|
343
|
+
status: mockHandler.status,
|
344
|
+
statusText: mockHandler.statusText,
|
345
|
+
readyState: 1,
|
346
|
+
open: function() { },
|
347
|
+
send: function() {
|
348
|
+
origHandler.fired = true;
|
349
|
+
_xhrSend.call(this, mockHandler, requestSettings, origSettings);
|
350
|
+
},
|
351
|
+
abort: function() {
|
352
|
+
clearTimeout(this.responseTimer);
|
353
|
+
},
|
354
|
+
setRequestHeader: function(header, value) {
|
355
|
+
mockHandler.headers[header] = value;
|
356
|
+
},
|
357
|
+
getResponseHeader: function(header) {
|
358
|
+
// 'Last-modified', 'Etag', 'content-type' are all checked by jQuery
|
359
|
+
if ( mockHandler.headers && mockHandler.headers[header] ) {
|
360
|
+
// Return arbitrary headers
|
361
|
+
return mockHandler.headers[header];
|
362
|
+
} else if ( header.toLowerCase() == 'last-modified' ) {
|
363
|
+
return mockHandler.lastModified || (new Date()).toString();
|
364
|
+
} else if ( header.toLowerCase() == 'etag' ) {
|
365
|
+
return mockHandler.etag || '';
|
366
|
+
} else if ( header.toLowerCase() == 'content-type' ) {
|
367
|
+
return mockHandler.contentType || 'text/plain';
|
368
|
+
}
|
369
|
+
},
|
370
|
+
getAllResponseHeaders: function() {
|
371
|
+
var headers = '';
|
372
|
+
$.each(mockHandler.headers, function(k, v) {
|
373
|
+
headers += k + ': ' + v + "\n";
|
374
|
+
});
|
375
|
+
return headers;
|
376
|
+
}
|
377
|
+
};
|
378
|
+
}
|
379
|
+
|
380
|
+
// Process a JSONP mock request.
|
381
|
+
function processJsonpMock( requestSettings, mockHandler, origSettings ) {
|
382
|
+
// Handle JSONP Parameter Callbacks, we need to replicate some of the jQuery core here
|
383
|
+
// because there isn't an easy hook for the cross domain script tag of jsonp
|
384
|
+
|
385
|
+
processJsonpUrl( requestSettings );
|
386
|
+
|
387
|
+
requestSettings.dataType = "json";
|
388
|
+
if(requestSettings.data && CALLBACK_REGEX.test(requestSettings.data) || CALLBACK_REGEX.test(requestSettings.url)) {
|
389
|
+
createJsonpCallback(requestSettings, mockHandler, origSettings);
|
390
|
+
|
391
|
+
// We need to make sure
|
392
|
+
// that a JSONP style response is executed properly
|
393
|
+
|
394
|
+
var rurl = /^(\w+:)?\/\/([^\/?#]+)/,
|
395
|
+
parts = rurl.exec( requestSettings.url ),
|
396
|
+
remote = parts && (parts[1] && parts[1] !== location.protocol || parts[2] !== location.host);
|
397
|
+
|
398
|
+
requestSettings.dataType = "script";
|
399
|
+
if(requestSettings.type.toUpperCase() === "GET" && remote ) {
|
400
|
+
var newMockReturn = processJsonpRequest( requestSettings, mockHandler, origSettings );
|
401
|
+
|
402
|
+
// Check if we are supposed to return a Deferred back to the mock call, or just
|
403
|
+
// signal success
|
404
|
+
if(newMockReturn) {
|
405
|
+
return newMockReturn;
|
406
|
+
} else {
|
407
|
+
return true;
|
408
|
+
}
|
409
|
+
}
|
410
|
+
}
|
411
|
+
return null;
|
412
|
+
}
|
413
|
+
|
414
|
+
// Append the required callback parameter to the end of the request URL, for a JSONP request
|
415
|
+
function processJsonpUrl( requestSettings ) {
|
416
|
+
if ( requestSettings.type.toUpperCase() === "GET" ) {
|
417
|
+
if ( !CALLBACK_REGEX.test( requestSettings.url ) ) {
|
418
|
+
requestSettings.url += (/\?/.test( requestSettings.url ) ? "&" : "?") +
|
419
|
+
(requestSettings.jsonp || "callback") + "=?";
|
420
|
+
}
|
421
|
+
} else if ( !requestSettings.data || !CALLBACK_REGEX.test(requestSettings.data) ) {
|
422
|
+
requestSettings.data = (requestSettings.data ? requestSettings.data + "&" : "") + (requestSettings.jsonp || "callback") + "=?";
|
423
|
+
}
|
424
|
+
}
|
425
|
+
|
426
|
+
// Process a JSONP request by evaluating the mocked response text
|
427
|
+
function processJsonpRequest( requestSettings, mockHandler, origSettings ) {
|
428
|
+
// Synthesize the mock request for adding a script tag
|
429
|
+
var callbackContext = origSettings && origSettings.context || requestSettings,
|
430
|
+
newMock = null;
|
431
|
+
|
432
|
+
|
433
|
+
// If the response handler on the moock is a function, call it
|
434
|
+
if ( mockHandler.response && $.isFunction(mockHandler.response) ) {
|
435
|
+
mockHandler.response(origSettings);
|
436
|
+
} else {
|
437
|
+
|
438
|
+
// Evaluate the responseText javascript in a global context
|
439
|
+
if( typeof mockHandler.responseText === 'object' ) {
|
440
|
+
$.globalEval( '(' + JSON.stringify( mockHandler.responseText ) + ')');
|
441
|
+
} else {
|
442
|
+
$.globalEval( '(' + mockHandler.responseText + ')');
|
443
|
+
}
|
444
|
+
}
|
445
|
+
|
446
|
+
// Successful response
|
447
|
+
jsonpSuccess( requestSettings, callbackContext, mockHandler );
|
448
|
+
jsonpComplete( requestSettings, callbackContext, mockHandler );
|
449
|
+
|
450
|
+
// If we are running under jQuery 1.5+, return a deferred object
|
451
|
+
if($.Deferred){
|
452
|
+
newMock = new $.Deferred();
|
453
|
+
if(typeof mockHandler.responseText == "object"){
|
454
|
+
newMock.resolveWith( callbackContext, [mockHandler.responseText] );
|
455
|
+
}
|
456
|
+
else{
|
457
|
+
newMock.resolveWith( callbackContext, [$.parseJSON( mockHandler.responseText )] );
|
458
|
+
}
|
459
|
+
}
|
460
|
+
return newMock;
|
461
|
+
}
|
462
|
+
|
463
|
+
|
464
|
+
// Create the required JSONP callback function for the request
|
465
|
+
function createJsonpCallback( requestSettings, mockHandler, origSettings ) {
|
466
|
+
var callbackContext = origSettings && origSettings.context || requestSettings;
|
467
|
+
var jsonp = requestSettings.jsonpCallback || ("jsonp" + jsc++);
|
468
|
+
|
469
|
+
// Replace the =? sequence both in the query string and the data
|
470
|
+
if ( requestSettings.data ) {
|
471
|
+
requestSettings.data = (requestSettings.data + "").replace(CALLBACK_REGEX, "=" + jsonp + "$1");
|
472
|
+
}
|
473
|
+
|
474
|
+
requestSettings.url = requestSettings.url.replace(CALLBACK_REGEX, "=" + jsonp + "$1");
|
475
|
+
|
476
|
+
|
477
|
+
// Handle JSONP-style loading
|
478
|
+
window[ jsonp ] = window[ jsonp ] || function( tmp ) {
|
479
|
+
data = tmp;
|
480
|
+
jsonpSuccess( requestSettings, callbackContext, mockHandler );
|
481
|
+
jsonpComplete( requestSettings, callbackContext, mockHandler );
|
482
|
+
// Garbage collect
|
483
|
+
window[ jsonp ] = undefined;
|
484
|
+
|
485
|
+
try {
|
486
|
+
delete window[ jsonp ];
|
487
|
+
} catch(e) {}
|
488
|
+
|
489
|
+
if ( head ) {
|
490
|
+
head.removeChild( script );
|
491
|
+
}
|
492
|
+
};
|
493
|
+
}
|
494
|
+
|
495
|
+
// The JSONP request was successful
|
496
|
+
function jsonpSuccess(requestSettings, callbackContext, mockHandler) {
|
497
|
+
// If a local callback was specified, fire it and pass it the data
|
498
|
+
if ( requestSettings.success ) {
|
499
|
+
requestSettings.success.call( callbackContext, mockHandler.responseText || "", status, {} );
|
500
|
+
}
|
501
|
+
|
502
|
+
// Fire the global callback
|
503
|
+
if ( requestSettings.global ) {
|
504
|
+
trigger(requestSettings, "ajaxSuccess", [{}, requestSettings] );
|
505
|
+
}
|
506
|
+
}
|
507
|
+
|
508
|
+
// The JSONP request was completed
|
509
|
+
function jsonpComplete(requestSettings, callbackContext) {
|
510
|
+
// Process result
|
511
|
+
if ( requestSettings.complete ) {
|
512
|
+
requestSettings.complete.call( callbackContext, {} , status );
|
513
|
+
}
|
514
|
+
|
515
|
+
// The request was completed
|
516
|
+
if ( requestSettings.global ) {
|
517
|
+
trigger( "ajaxComplete", [{}, requestSettings] );
|
518
|
+
}
|
519
|
+
|
520
|
+
// Handle the global AJAX counter
|
521
|
+
if ( requestSettings.global && ! --$.active ) {
|
522
|
+
$.event.trigger( "ajaxStop" );
|
523
|
+
}
|
524
|
+
}
|
525
|
+
|
526
|
+
|
527
|
+
// The core $.ajax replacement.
|
528
|
+
function handleAjax( url, origSettings ) {
|
529
|
+
var mockRequest, requestSettings, mockHandler;
|
530
|
+
// console.log('handleAjax', url)
|
531
|
+
// If url is an object, simulate pre-1.5 signature
|
532
|
+
if ( typeof url === "object" ) {
|
533
|
+
origSettings = url;
|
534
|
+
url = undefined;
|
535
|
+
} else {
|
536
|
+
// work around to support 1.5 signature
|
537
|
+
origSettings.url = url;
|
538
|
+
}
|
539
|
+
|
540
|
+
// Extend the original settings for the request
|
541
|
+
requestSettings = $.extend(true, {}, $.ajaxSettings, origSettings);
|
542
|
+
|
543
|
+
// Iterate over our mock handlers (in registration order) until we find
|
544
|
+
// one that is willing to intercept the request
|
545
|
+
for(var k = 0; k < mockHandlers.length; k++) {
|
546
|
+
if ( !mockHandlers[k] ) {
|
547
|
+
continue;
|
548
|
+
}
|
549
|
+
|
550
|
+
mockHandler = getMockForRequest( mockHandlers[k], requestSettings );
|
551
|
+
if(!mockHandler) {
|
552
|
+
// No valid mock found for this request
|
553
|
+
continue;
|
554
|
+
}
|
555
|
+
|
556
|
+
mockedAjaxCalls.push(requestSettings);
|
557
|
+
|
558
|
+
// If logging is enabled, log the mock to the console
|
559
|
+
$.mockjaxSettings.log( mockHandler, requestSettings );
|
560
|
+
|
561
|
+
|
562
|
+
if ( requestSettings.dataType === "jsonp" ) {
|
563
|
+
if ((mockRequest = processJsonpMock( requestSettings, mockHandler, origSettings ))) {
|
564
|
+
// This mock will handle the JSONP request
|
565
|
+
return mockRequest;
|
566
|
+
}
|
567
|
+
}
|
568
|
+
|
569
|
+
|
570
|
+
// Removed to fix #54 - keep the mocking data object intact
|
571
|
+
//mockHandler.data = requestSettings.data;
|
572
|
+
|
573
|
+
mockHandler.cache = requestSettings.cache;
|
574
|
+
mockHandler.timeout = requestSettings.timeout;
|
575
|
+
mockHandler.global = requestSettings.global;
|
576
|
+
|
577
|
+
copyUrlParameters(mockHandler, origSettings);
|
578
|
+
|
579
|
+
(function(mockHandler, requestSettings, origSettings, origHandler) {
|
580
|
+
mockRequest = _ajax.call($, $.extend(true, {}, origSettings, {
|
581
|
+
// Mock the XHR object
|
582
|
+
xhr: function() { return xhr( mockHandler, requestSettings, origSettings, origHandler ); }
|
583
|
+
}));
|
584
|
+
})(mockHandler, requestSettings, origSettings, mockHandlers[k]);
|
585
|
+
|
586
|
+
return mockRequest;
|
587
|
+
}
|
588
|
+
|
589
|
+
// We don't have a mock request
|
590
|
+
if($.mockjaxSettings.throwUnmocked === true) {
|
591
|
+
throw('AJAX not mocked: ' + origSettings.url);
|
592
|
+
}
|
593
|
+
else { // trigger a normal request
|
594
|
+
return _ajax.apply($, [origSettings]);
|
595
|
+
}
|
596
|
+
}
|
597
|
+
|
598
|
+
/**
|
599
|
+
* Copies URL parameter values if they were captured by a regular expression
|
600
|
+
* @param {Object} mockHandler
|
601
|
+
* @param {Object} origSettings
|
602
|
+
*/
|
603
|
+
function copyUrlParameters(mockHandler, origSettings) {
|
604
|
+
//parameters aren't captured if the URL isn't a RegExp
|
605
|
+
if (!(mockHandler.url instanceof RegExp)) {
|
606
|
+
return;
|
607
|
+
}
|
608
|
+
//if no URL params were defined on the handler, don't attempt a capture
|
609
|
+
if (!mockHandler.hasOwnProperty('urlParams')) {
|
610
|
+
return;
|
611
|
+
}
|
612
|
+
var captures = mockHandler.url.exec(origSettings.url);
|
613
|
+
//the whole RegExp match is always the first value in the capture results
|
614
|
+
if (captures.length === 1) {
|
615
|
+
return;
|
616
|
+
}
|
617
|
+
captures.shift();
|
618
|
+
//use handler params as keys and capture resuts as values
|
619
|
+
var i = 0,
|
620
|
+
capturesLength = captures.length,
|
621
|
+
paramsLength = mockHandler.urlParams.length,
|
622
|
+
//in case the number of params specified is less than actual captures
|
623
|
+
maxIterations = Math.min(capturesLength, paramsLength),
|
624
|
+
paramValues = {};
|
625
|
+
for (i; i < maxIterations; i++) {
|
626
|
+
var key = mockHandler.urlParams[i];
|
627
|
+
paramValues[key] = captures[i];
|
628
|
+
}
|
629
|
+
origSettings.urlParams = paramValues;
|
630
|
+
}
|
631
|
+
|
632
|
+
|
633
|
+
// Public
|
634
|
+
|
635
|
+
$.extend({
|
636
|
+
ajax: handleAjax
|
637
|
+
});
|
638
|
+
|
639
|
+
$.mockjaxSettings = {
|
640
|
+
//url: null,
|
641
|
+
//type: 'GET',
|
642
|
+
log: function( mockHandler, requestSettings ) {
|
643
|
+
if ( mockHandler.logging === false ||
|
644
|
+
( typeof mockHandler.logging === 'undefined' && $.mockjaxSettings.logging === false ) ) {
|
645
|
+
return;
|
646
|
+
}
|
647
|
+
if ( window.console && console.log ) {
|
648
|
+
var message = 'MOCK ' + requestSettings.type.toUpperCase() + ': ' + requestSettings.url;
|
649
|
+
var request = $.extend({}, requestSettings);
|
650
|
+
|
651
|
+
if (typeof console.log === 'function') {
|
652
|
+
console.log(message, request);
|
653
|
+
} else {
|
654
|
+
try {
|
655
|
+
console.log( message + ' ' + JSON.stringify(request) );
|
656
|
+
} catch (e) {
|
657
|
+
console.log(message);
|
658
|
+
}
|
659
|
+
}
|
660
|
+
}
|
661
|
+
},
|
662
|
+
logging: true,
|
663
|
+
status: 200,
|
664
|
+
statusText: "OK",
|
665
|
+
responseTime: 500,
|
666
|
+
isTimeout: false,
|
667
|
+
throwUnmocked: false,
|
668
|
+
contentType: 'text/plain',
|
669
|
+
response: '',
|
670
|
+
responseText: '',
|
671
|
+
responseXML: '',
|
672
|
+
proxy: '',
|
673
|
+
proxyType: 'GET',
|
674
|
+
|
675
|
+
lastModified: null,
|
676
|
+
etag: '',
|
677
|
+
headers: {
|
678
|
+
etag: 'IJF@H#@923uf8023hFO@I#H#',
|
679
|
+
'content-type' : 'text/plain'
|
680
|
+
}
|
681
|
+
};
|
682
|
+
|
683
|
+
$.mockjax = function(settings) {
|
684
|
+
var i = mockHandlers.length;
|
685
|
+
mockHandlers[i] = settings;
|
686
|
+
return i;
|
687
|
+
};
|
688
|
+
$.mockjaxClear = function(i) {
|
689
|
+
if ( arguments.length == 1 ) {
|
690
|
+
mockHandlers[i] = null;
|
691
|
+
} else {
|
692
|
+
mockHandlers = [];
|
693
|
+
}
|
694
|
+
mockedAjaxCalls = [];
|
695
|
+
};
|
696
|
+
$.mockjax.handler = function(i) {
|
697
|
+
if ( arguments.length == 1 ) {
|
698
|
+
return mockHandlers[i];
|
699
|
+
}
|
700
|
+
};
|
701
|
+
$.mockjax.mockedAjaxCalls = function() {
|
702
|
+
return mockedAjaxCalls;
|
703
|
+
};
|
704
|
+
})(jQuery);
|
85
705
|
/**
|
86
706
|
* Sinon.JS 1.6.0, 2013/02/18
|
87
707
|
*
|