-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ajax): ensure post sending values
- refactors ajax to send posted data properly - adds logic for serializing data to url-formdata - creates custom XMLHttpRequest mock to enable better testing and integrate better with the library - ensures proper content type setting - corrects tests that were false positives because of jasmine-ajax
- Loading branch information
Showing
3 changed files
with
249 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,120 @@ | ||
var jasmineCore = require('jasmine-core'); | ||
var root = require('../../dist/cjs/util/root').root; | ||
|
||
// jasmine-ajax need this | ||
global.getJasmineRequireObj = function () { | ||
return jasmineCore; | ||
var requests = []; | ||
var recentRequest = null; | ||
|
||
function MockXMLHttpRequest() { | ||
this.previousRequest = recentRequest; | ||
recentRequest = this; | ||
requests.push(this); | ||
this.requestHeaders = {}; | ||
this.responseType = ''; | ||
this.eventHandlers = []; | ||
this.readyState = 0; | ||
} | ||
|
||
MockXMLHttpRequest.prototype = { | ||
send: function (data) { | ||
this.data = data; | ||
}, | ||
|
||
open: function (method, url, async, user, password) { | ||
this.method = method; | ||
this.url = url; | ||
this.async = async; | ||
this.user = user; | ||
this.password = password; | ||
this.readyState = 1; | ||
this.triggerEvent('readystatechange'); | ||
}, | ||
|
||
setRequestHeader: function (key, value) { | ||
this.requestHeaders[key] = value; | ||
}, | ||
|
||
addEventListener: function (name, handler) { | ||
this.eventHandlers.push({ name: name, handler: handler }); | ||
}, | ||
|
||
removeEventListener: function (name, handler) { | ||
for (var i = this.eventHandlers.length - 1; i--;) { | ||
var eh = this.eventHandlers[i]; | ||
if (eh.name === name && eh.handler === handler) { | ||
this.eventHandlers.splice(i, 1); | ||
} | ||
} | ||
}, | ||
|
||
throwError: function (err) { | ||
// TODO: something better with errors | ||
this.triggerEvent('error'); | ||
}, | ||
|
||
respondWith: function (response) { | ||
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': | ||
try { | ||
this.response = JSON.parse(response.responseText); | ||
} catch (err) { | ||
throw new Error('unable to JSON.parse: \n' + response.responseText); | ||
} | ||
break; | ||
case 'text': | ||
this.response = response.responseText; | ||
break; | ||
default: | ||
throw new Error('unhandled type "' + this.responseType + '"'); | ||
} | ||
} | ||
// TODO: pass better event to onload. | ||
this.triggerEvent('load'); | ||
this.triggerEvent('readystatechange'); | ||
}, | ||
|
||
triggerEvent: function (name, eventObj) { | ||
// TODO: create a better default event | ||
e = eventObj || {}; | ||
|
||
if (this['on' + name]) { | ||
this['on' + name](e); | ||
} | ||
|
||
this.eventHandlers.forEach(function (eh) { | ||
if (eh.name === name) { | ||
eh.handler.call(this, e); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
MockXMLHttpRequest.mostRecent = function () { | ||
return recentRequest; | ||
}; | ||
|
||
// XMLHttpRequest in node | ||
global.XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; | ||
MockXMLHttpRequest.allRequests = function () { | ||
return requests; | ||
}; | ||
|
||
var gXHR; | ||
var rXHR; | ||
|
||
global.setupMockXHR = function () { | ||
gXHR = global.XMLHttpRequest; | ||
rXHR = root.XMLHttpRequest; | ||
global.XMLHttpRequest = MockXMLHttpRequest; | ||
root.XMLHttpRequest = MockXMLHttpRequest; | ||
}; | ||
|
||
var w = global.window; | ||
global.window = global; | ||
require.call(global, 'jasmine-ajax'); | ||
global.window = w; | ||
global.teardownMockXHR = function () { | ||
global.XMLHttpRequest = gXHR; | ||
root.XMLHttpRequest = rXHR; | ||
requests.length = 0; | ||
recentRequest = null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.