1
|
|
|
/* |
2
|
|
|
Jasmine-Ajax : a set of helpers for testing AJAX requests under the Jasmine |
3
|
|
|
BDD framework for JavaScript. |
4
|
|
|
|
5
|
|
|
Supports both Prototype.js and jQuery. |
6
|
|
|
|
7
|
|
|
http://github.com/pivotal/jasmine-ajax |
8
|
|
|
|
9
|
|
|
Jasmine Home page: http://pivotal.github.com/jasmine |
10
|
|
|
|
11
|
|
|
Copyright (c) 2008-2010 Pivotal Labs |
12
|
|
|
|
13
|
|
|
Permission is hereby granted, free of charge, to any person obtaining |
14
|
|
|
a copy of this software and associated documentation files (the |
15
|
|
|
"Software"), to deal in the Software without restriction, including |
16
|
|
|
without limitation the rights to use, copy, modify, merge, publish, |
17
|
|
|
distribute, sublicense, and/or sell copies of the Software, and to |
18
|
|
|
permit persons to whom the Software is furnished to do so, subject to |
19
|
|
|
the following conditions: |
20
|
|
|
|
21
|
|
|
The above copyright notice and this permission notice shall be |
22
|
|
|
included in all copies or substantial portions of the Software. |
23
|
|
|
|
24
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
25
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
26
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
27
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
28
|
|
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
29
|
|
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
30
|
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
31
|
|
|
|
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
// Jasmine-Ajax interface |
35
|
|
|
var ajaxRequests = []; |
36
|
|
|
|
37
|
|
|
function mostRecentAjaxRequest() { |
38
|
|
|
if (ajaxRequests.length > 0) { |
39
|
|
|
return ajaxRequests[ajaxRequests.length - 1]; |
40
|
|
|
} else { |
41
|
|
|
return null; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
function clearAjaxRequests() { |
46
|
|
|
ajaxRequests = []; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Fake XHR for mocking Ajax Requests & Responses |
50
|
|
|
function FakeXMLHttpRequest() { |
51
|
|
|
var extend = Object.extend || $.extend; |
52
|
|
|
extend(this, { |
53
|
|
|
requestHeaders: {}, |
54
|
|
|
|
55
|
|
|
open: function() { |
56
|
|
|
this.method = arguments[0]; |
57
|
|
|
this.url = arguments[1]; |
58
|
|
|
this.readyState = 1; |
59
|
|
|
}, |
60
|
|
|
|
61
|
|
|
setRequestHeader: function(header, value) { |
62
|
|
|
this.requestHeaders[header] = value; |
63
|
|
|
}, |
64
|
|
|
|
65
|
|
|
abort: function() { |
66
|
|
|
this.readyState = 0; |
67
|
|
|
}, |
68
|
|
|
|
69
|
|
|
readyState: 0, |
70
|
|
|
|
71
|
|
|
onreadystatechange: function(isTimeout) { |
|
|
|
|
72
|
|
|
}, |
73
|
|
|
|
74
|
|
|
status: null, |
75
|
|
|
|
76
|
|
|
send: function(data) { |
77
|
|
|
this.params = data; |
78
|
|
|
this.readyState = 2; |
79
|
|
|
}, |
80
|
|
|
|
81
|
|
|
getResponseHeader: function(name) { |
82
|
|
|
return this.responseHeaders[name]; |
83
|
|
|
}, |
84
|
|
|
|
85
|
|
|
getAllResponseHeaders: function() { |
86
|
|
|
var responseHeaders = []; |
87
|
|
|
for (var i in this.responseHeaders) { |
88
|
|
|
if (this.responseHeaders.hasOwnProperty(i)) { |
89
|
|
|
responseHeaders.push(i + ': ' + this.responseHeaders[i]); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
return responseHeaders.join('\r\n'); |
93
|
|
|
}, |
94
|
|
|
|
95
|
|
|
responseText: null, |
96
|
|
|
|
97
|
|
|
response: function(response) { |
98
|
|
|
this.status = response.status; |
99
|
|
|
this.responseText = response.responseText || ""; |
100
|
|
|
this.readyState = 4; |
101
|
|
|
this.responseHeaders = response.responseHeaders || |
102
|
|
|
{"Content-type": response.contentType || "application/json" }; |
103
|
|
|
// uncomment for jquery 1.3.x support |
104
|
|
|
// jasmine.Clock.tick(20); |
105
|
|
|
|
106
|
|
|
this.onreadystatechange(); |
107
|
|
|
}, |
108
|
|
|
responseTimeout: function() { |
109
|
|
|
this.readyState = 4; |
110
|
|
|
jasmine.Clock.tick(jQuery.ajaxSettings.timeout || 30000); |
|
|
|
|
111
|
|
|
this.onreadystatechange('timeout'); |
112
|
|
|
} |
113
|
|
|
}); |
114
|
|
|
|
115
|
|
|
return this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
jasmine.Ajax = { |
120
|
|
|
|
121
|
|
|
isInstalled: function() { |
122
|
|
|
return jasmine.Ajax.installed == true; |
|
|
|
|
123
|
|
|
}, |
124
|
|
|
|
125
|
|
|
assertInstalled: function() { |
126
|
|
|
if (!jasmine.Ajax.isInstalled()) { |
|
|
|
|
127
|
|
|
throw new Error("Mock ajax is not installed, use jasmine.Ajax.useMock()") |
128
|
|
|
} |
129
|
|
|
}, |
130
|
|
|
|
131
|
|
|
useMock: function() { |
132
|
|
|
if (!jasmine.Ajax.isInstalled()) { |
|
|
|
|
133
|
|
|
var spec = jasmine.getEnv().currentSpec; |
134
|
|
|
spec.after(jasmine.Ajax.uninstallMock); |
135
|
|
|
|
136
|
|
|
jasmine.Ajax.installMock(); |
137
|
|
|
} |
138
|
|
|
}, |
139
|
|
|
|
140
|
|
|
installMock: function() { |
141
|
|
|
if (typeof jQuery != 'undefined') { |
142
|
|
|
jasmine.Ajax.installJquery(); |
|
|
|
|
143
|
|
|
} else if (typeof Prototype != 'undefined') { |
|
|
|
|
144
|
|
|
jasmine.Ajax.installPrototype(); |
145
|
|
|
} else { |
146
|
|
|
throw new Error("jasmine.Ajax currently only supports jQuery and Prototype"); |
147
|
|
|
} |
148
|
|
|
jasmine.Ajax.installed = true; |
149
|
|
|
}, |
150
|
|
|
|
151
|
|
|
installJquery: function() { |
152
|
|
|
jasmine.Ajax.mode = 'jQuery'; |
|
|
|
|
153
|
|
|
jasmine.Ajax.real = jQuery.ajaxSettings.xhr; |
154
|
|
|
jQuery.ajaxSettings.xhr = jasmine.Ajax.jQueryMock; |
155
|
|
|
|
156
|
|
|
}, |
157
|
|
|
|
158
|
|
|
installPrototype: function() { |
159
|
|
|
jasmine.Ajax.mode = 'Prototype'; |
|
|
|
|
160
|
|
|
jasmine.Ajax.real = Ajax.getTransport; |
|
|
|
|
161
|
|
|
|
162
|
|
|
Ajax.getTransport = jasmine.Ajax.prototypeMock; |
163
|
|
|
}, |
164
|
|
|
|
165
|
|
|
uninstallMock: function() { |
166
|
|
|
jasmine.Ajax.assertInstalled(); |
|
|
|
|
167
|
|
|
if (jasmine.Ajax.mode == 'jQuery') { |
168
|
|
|
jQuery.ajaxSettings.xhr = jasmine.Ajax.real; |
169
|
|
|
} else if (jasmine.Ajax.mode == 'Prototype') { |
170
|
|
|
Ajax.getTransport = jasmine.Ajax.real; |
|
|
|
|
171
|
|
|
} |
172
|
|
|
jasmine.Ajax.reset(); |
173
|
|
|
}, |
174
|
|
|
|
175
|
|
|
reset: function() { |
176
|
|
|
jasmine.Ajax.installed = false; |
|
|
|
|
177
|
|
|
jasmine.Ajax.mode = null; |
178
|
|
|
jasmine.Ajax.real = null; |
179
|
|
|
}, |
180
|
|
|
|
181
|
|
|
jQueryMock: function() { |
182
|
|
|
var newXhr = new FakeXMLHttpRequest(); |
183
|
|
|
ajaxRequests.push(newXhr); |
184
|
|
|
return newXhr; |
185
|
|
|
}, |
186
|
|
|
|
187
|
|
|
prototypeMock: function() { |
188
|
|
|
return new FakeXMLHttpRequest(); |
189
|
|
|
}, |
190
|
|
|
|
191
|
|
|
installed: false, |
192
|
|
|
mode: null |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
// Jasmine-Ajax Glue code for Prototype.js |
197
|
|
|
if (typeof Prototype != 'undefined' && Ajax && Ajax.Request) { |
|
|
|
|
198
|
|
|
Ajax.Request.prototype.originalRequest = Ajax.Request.prototype.request; |
199
|
|
|
Ajax.Request.prototype.request = function(url) { |
200
|
|
|
this.originalRequest(url); |
201
|
|
|
ajaxRequests.push(this); |
202
|
|
|
}; |
203
|
|
|
|
204
|
|
|
Ajax.Request.prototype.response = function(responseOptions) { |
205
|
|
|
return this.transport.response(responseOptions); |
206
|
|
|
}; |
207
|
|
|
} |
208
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.