jasmine-ajax
Advanced tools
Comparing version 3.2.0 to 3.3.0
{ | ||
"name": "jasmine-ajax", | ||
"description": "A library for faking Ajax responses in your Jasmine suite.", | ||
"version": "3.2.0", | ||
"version": "3.3.0", | ||
"main": "lib/mock-ajax.js", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -33,2 +33,17 @@ /* | ||
//Module wrapper to support both browser and CommonJS environment | ||
(function (root, factory) { | ||
if (typeof exports === 'object' && typeof exports.nodeName !== 'string') { | ||
// CommonJS | ||
var jasmineRequire = require('jasmine-core'); | ||
module.exports = factory(root, function() { | ||
return jasmineRequire; | ||
}); | ||
} else { | ||
// Browser globals | ||
window.MockAjax = factory(root, getJasmineRequireObj); | ||
} | ||
}(typeof window !== 'undefined' ? window : global, function (global, getJasmineRequireObj) { | ||
// | ||
getJasmineRequireObj().ajax = function(jRequire) { | ||
@@ -273,3 +288,11 @@ var $ajax = {}; | ||
var iePropertiesThatCannotBeCopied = ['responseBody', 'responseText', 'responseXML', 'status', 'statusText', 'responseTimeout']; | ||
extend(FakeXMLHttpRequest, { | ||
UNSENT: 0, | ||
OPENED: 1, | ||
HEADERS_RECEIVED: 2, | ||
LOADING: 3, | ||
DONE: 4 | ||
}); | ||
var iePropertiesThatCannotBeCopied = ['responseBody', 'responseText', 'responseXML', 'status', 'statusText', 'responseTimeout', 'responseURL']; | ||
extend(FakeXMLHttpRequest.prototype, new global.XMLHttpRequest(), iePropertiesThatCannotBeCopied); | ||
@@ -282,3 +305,4 @@ extend(FakeXMLHttpRequest.prototype, { | ||
this.password = arguments[4]; | ||
this.readyState = 1; | ||
this.readyState = FakeXMLHttpRequest.OPENED; | ||
this.requestHeaders = {}; | ||
this.eventBus.trigger('readystatechange'); | ||
@@ -288,2 +312,6 @@ }, | ||
setRequestHeader: function(header, value) { | ||
if (this.readyState === 0) { | ||
throw new Error('DOMException: Failed to execute "setRequestHeader" on "XMLHttpRequest": The object\'s state must be OPENED.'); | ||
} | ||
if(this.requestHeaders.hasOwnProperty(header)) { | ||
@@ -301,3 +329,3 @@ this.requestHeaders[header] = [this.requestHeaders[header], value].join(', '); | ||
abort: function() { | ||
this.readyState = 0; | ||
this.readyState = FakeXMLHttpRequest.UNSENT; | ||
this.status = 0; | ||
@@ -311,3 +339,3 @@ this.statusText = "abort"; | ||
readyState: 0, | ||
readyState: FakeXMLHttpRequest.UNSENT, | ||
@@ -338,2 +366,7 @@ onloadstart: null, | ||
var stub = stubTracker.findStub(this.url, data, this.method); | ||
this.dispatchStub(stub); | ||
}, | ||
dispatchStub: function(stub) { | ||
if (stub) { | ||
@@ -346,2 +379,4 @@ if (stub.isReturn()) { | ||
this.responseTimeout(); | ||
} else if (stub.isCallFunction()) { | ||
this.responseCallFunction(stub); | ||
} | ||
@@ -364,4 +399,6 @@ } | ||
getResponseHeader: function(name) { | ||
var resultHeader = null; | ||
if (!this.responseHeaders) { return resultHeader; } | ||
name = name.toLowerCase(); | ||
var resultHeader; | ||
for(var i = 0; i < this.responseHeaders.length; i++) { | ||
@@ -381,2 +418,4 @@ var header = this.responseHeaders[i]; | ||
getAllResponseHeaders: function() { | ||
if (!this.responseHeaders) { return null; } | ||
var responseHeaders = []; | ||
@@ -393,2 +432,3 @@ for (var i = 0; i < this.responseHeaders.length; i++) { | ||
responseType: null, | ||
responseURL: null, | ||
@@ -400,3 +440,3 @@ responseValue: function() { | ||
case "text": | ||
return this.readyState >= 3 ? this.responseText : ""; | ||
return this.readyState >= FakeXMLHttpRequest.LOADING ? this.responseText : ""; | ||
case "json": | ||
@@ -415,3 +455,3 @@ return JSON.parse(this.responseText); | ||
respondWith: function(response) { | ||
if (this.readyState === 4) { | ||
if (this.readyState === FakeXMLHttpRequest.DONE) { | ||
throw new Error("FakeXMLHttpRequest already completed"); | ||
@@ -423,3 +463,3 @@ } | ||
this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType); | ||
this.readyState = 2; | ||
this.readyState = FakeXMLHttpRequest.HEADERS_RECEIVED; | ||
this.eventBus.trigger('readystatechange'); | ||
@@ -429,3 +469,4 @@ | ||
this.responseType = response.responseType || ""; | ||
this.readyState = 4; | ||
this.responseURL = response.responseURL || null; | ||
this.readyState = FakeXMLHttpRequest.DONE; | ||
this.responseXML = getResponseXml(response.responseText, this.getResponseHeader('content-type') || ''); | ||
@@ -449,6 +490,6 @@ if (this.responseXML) { | ||
responseTimeout: function() { | ||
if (this.readyState === 4) { | ||
if (this.readyState === FakeXMLHttpRequest.DONE) { | ||
throw new Error("FakeXMLHttpRequest already completed"); | ||
} | ||
this.readyState = 4; | ||
this.readyState = FakeXMLHttpRequest.DONE; | ||
jasmine.clock().tick(30000); | ||
@@ -462,6 +503,6 @@ this.eventBus.trigger('readystatechange'); | ||
responseError: function() { | ||
if (this.readyState === 4) { | ||
if (this.readyState === FakeXMLHttpRequest.DONE) { | ||
throw new Error("FakeXMLHttpRequest already completed"); | ||
} | ||
this.readyState = 4; | ||
this.readyState = FakeXMLHttpRequest.DONE; | ||
this.eventBus.trigger('readystatechange'); | ||
@@ -471,2 +512,8 @@ this.eventBus.trigger('progress'); | ||
this.eventBus.trigger('loadend'); | ||
}, | ||
responseCallFunction: function(stub) { | ||
stub.action = undefined; | ||
stub.functionToCall(stub, this); | ||
this.dispatchStub(stub); | ||
} | ||
@@ -498,2 +545,5 @@ }); | ||
this.uninstall = function() { | ||
if (global.XMLHttpRequest !== mockAjaxFunction) { | ||
throw "MockAjax not installed."; | ||
} | ||
global.XMLHttpRequest = realAjaxFunction; | ||
@@ -592,3 +642,4 @@ | ||
ERROR = 1, | ||
TIMEOUT = 2; | ||
TIMEOUT = 2, | ||
CALL = 3; | ||
@@ -620,2 +671,3 @@ function RequestStub(url, stubData, method) { | ||
this.responseHeaders = options.responseHeaders; | ||
this.responseURL = options.responseURL; | ||
}; | ||
@@ -643,2 +695,11 @@ | ||
this.andCallFunction = function(functionToCall) { | ||
this.action = CALL; | ||
this.functionToCall = functionToCall; | ||
}; | ||
this.isCallFunction = function() { | ||
return this.action === CALL; | ||
}; | ||
this.matches = function(fullUrl, data, method) { | ||
@@ -745,12 +806,8 @@ var urlMatches = false; | ||
(function() { | ||
var jRequire = getJasmineRequireObj(), | ||
MockAjax = jRequire.ajax(jRequire); | ||
if (typeof window === "undefined" && typeof exports === "object") { | ||
exports.MockAjax = MockAjax; | ||
jasmine.Ajax = new MockAjax(exports); | ||
} else { | ||
window.MockAjax = MockAjax; | ||
jasmine.Ajax = new MockAjax(window); | ||
} | ||
}()); | ||
var jRequire = getJasmineRequireObj(); | ||
var MockAjax = jRequire.ajax(jRequire); | ||
jasmine.Ajax = new MockAjax(global); | ||
return MockAjax; | ||
})); |
{ | ||
"name": "jasmine-ajax", | ||
"description": "A library for faking Ajax responses in your Jasmine suite", | ||
"version": "3.2.0", | ||
"version": "3.3.0", | ||
"main": "lib/mock-ajax.js", | ||
"license": "MIT", | ||
"url": "https://github.com/jasmine/jasmine-ajax", | ||
@@ -20,8 +19,9 @@ "contributors": [ | ||
"devDependencies": { | ||
"grunt": "0.4.0", | ||
"grunt": "~0.4.0", | ||
"grunt-cli": "~0.1.13", | ||
"grunt-contrib-concat": "~0.5.0", | ||
"grunt-contrib-jshint": "~0.1.1", | ||
"grunt-shell": "~0.2.1" | ||
"grunt-includes": "^0.5.1", | ||
"grunt-shell": "~0.2.1", | ||
"grunt-template": "^0.2.3" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44960
12
689
294
6