Skip to content

Commit c85c003

Browse files
authored
Merge pull request #323 from reinrl/patch-2
[WIP] Allow a list of possible return statuses (as a number or a string) which will get randomly picked from
2 parents bb490b0 + 7d9db46 commit c85c003

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,23 @@ $.mockjax({
479479
});
480480
```
481481

482+
The ability to provide an array of possible response statuses (from which the response
483+
for a given request will be randomly picked from):
484+
485+
```javascript
486+
// Randomly fail
487+
$.mockjax({
488+
url: "/restful/api",
489+
status: [200,400,500]
490+
});
491+
492+
// Randomly fail (with a preference towards success)
493+
$.mockjax({
494+
url: "/restful/api",
495+
status: [200,400,200,500,200]
496+
});
497+
```
498+
482499
These forced error status codes will be handled just as if the server had
483500
returned the error: the `error` callback will get executed with the proper
484501
arguments.

src/jquery.mockjax.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,14 @@
275275
} else {
276276
this.responseText = mockHandler.responseText;
277277
}
278-
if( typeof mockHandler.status === 'number' || typeof mockHandler.status === 'string' ) {
278+
279+
if ($.isArray(mockHandler.status)) {
280+
var idxStatus = Math.floor(Math.random() * mockHandler.status.length)
281+
this.status = mockHandler.status[idxStatus];
282+
} else if (typeof mockHandler.status === 'number' || typeof mockHandler.status === 'string') {
279283
this.status = mockHandler.status;
280284
}
285+
281286
if( typeof mockHandler.statusText === 'string') {
282287
this.statusText = mockHandler.statusText;
283288
}

test/test-core.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,58 @@
247247
});
248248
});
249249

250+
t('Dynamic response status callback - list of statuses as an array (mix of successes and failures)', function(assert) {
251+
var done = assert.async();
252+
var possibleStatuses = [200,201,204,400,404,500];
253+
var returnedStatuses = [];
254+
var maxNumLoops = possibleStatuses.length * 50;
255+
var numLoopsComplete = 0;
256+
257+
$.mockjax({
258+
url: '/response-callback',
259+
response: function() {
260+
this.status = possibleStatuses;
261+
this.statusText = 'Internal Server Error';
262+
}
263+
});
264+
265+
var completeCallback = function(xhr) {
266+
assert.notEqual($.inArray(xhr.status, possibleStatuses), -1, 'Dynamically set random response status found');
267+
268+
// add this to our array of returned statuses (if it isn't there already)
269+
if($.inArray(xhr.status, returnedStatuses) === -1) {
270+
returnedStatuses.push(xhr.status);
271+
}
272+
273+
// increment counter
274+
numLoopsComplete++;
275+
276+
// if we made it this far without matching all possible statuses, fail!
277+
if (numLoopsComplete >= maxNumLoops) {
278+
assert.equal(returnedStatuses.length, possibleStatuses.length, "Did not randomly return all possible statuses (only returned: " + returnedStatuses.toString() + ")");
279+
280+
done();
281+
}
282+
283+
// we short-circuited early, so we can be done
284+
if (returnedStatuses.length === possibleStatuses.length) {
285+
done();
286+
}
287+
}
288+
289+
do {
290+
$.ajax({
291+
url: '/response-callback',
292+
dataType: 'text',
293+
async: false,
294+
data: {
295+
response: 'Hello world'
296+
},
297+
complete: completeCallback
298+
});
299+
} while ((numLoopsComplete < maxNumLoops) && (possibleStatuses.length !== returnedStatuses.length));
300+
});
301+
250302
t('Default Response Settings', function(assert) {
251303
var done = assert.async();
252304

0 commit comments

Comments
 (0)