r80133 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r80132‎ | r80133 | r80134 >
Date:00:07, 13 January 2011
Author:neilk
Status:deferred
Tags:
Comment:
updated mockjax
Modified paths:
  • /trunk/extensions/UploadWizard/test/jasmine/lib/appendto-jquery-mockjax/demo.html (deleted) (history)
  • /trunk/extensions/UploadWizard/test/jasmine/lib/appendto-jquery-mockjax/jquery.mockjax.js (modified) (history)
  • /trunk/extensions/UploadWizard/test/jasmine/lib/appendto-jquery-mockjax/jquery.mockjax.min.js (deleted) (history)
  • /trunk/extensions/UploadWizard/test/jasmine/lib/appendto-jquery-mockjax/mockjax.demo.js (deleted) (history)
  • /trunk/extensions/UploadWizard/test/jasmine/lib/appendto-jquery-mockjax/samples.js (deleted) (history)
  • /trunk/extensions/UploadWizard/test/jasmine/lib/appendto-jquery-mockjax/test.js (deleted) (history)
  • /trunk/extensions/UploadWizard/test/jasmine/lib/appendto-jquery-mockjax/test.json (deleted) (history)

Diff [purge]

Index: trunk/extensions/UploadWizard/test/jasmine/lib/appendto-jquery-mockjax/samples.js
@@ -1,16 +0,0 @@
2 -$.mockjaxSettings = {
3 - responseTime: 650
4 -};
5 -
6 -$.mockjax({
7 - url: '/some/url',
8 - responseTime: 1500
9 -});
10 -
11 -$.mockjax({
12 - url: '/another/url'
13 -});
14 -
15 -$.mockjax({
16 - url: '*',
17 -});
\ No newline at end of file
Index: trunk/extensions/UploadWizard/test/jasmine/lib/appendto-jquery-mockjax/demo.html
@@ -1,220 +0,0 @@
2 -<!DOCTYPE html>
3 -<html>
4 -<head>
5 - <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6 - <title>MockJax Demo</title>
7 - <style type="text/css">
8 - body {
9 - font-family: sans-serif;
10 - }
11 - dl dt {
12 - margin: 0;
13 - font-weight: bold;
14 - }
15 - dl dd {
16 - margin: 0 0 0.5em 0;
17 - }
18 - pre {
19 - padding: 1em;
20 - display: block;
21 - background: #EFEFEF;
22 - border: 1px dashed #656565;
23 - margin: 0 0 1em 0;
24 - }
25 - span.title {
26 - display: block;
27 - margin-top: 1em;
28 - font-size: 0.8em;
29 - }
30 - </style>
31 - <script src="lib/jquery-1.4.2.js"></script>
32 - <script src="lib/jquery.xmldom.js"></script>
33 - <script src="lib/json2.js"></script>
34 - <script src="jquery.mockjax.js"></script>
35 - <script type="text/javascript">
36 - $('body').delegate('input', 'click', function() {
37 - var fn = $(this).attr('id');
38 - if ( $.isFunction(window[fn]) ) {
39 - window[fn]();
40 - }
41 - });
42 - $('body').delegate('pre', 'click', function() {
43 - $.mockjaxClear();
44 - var script = $(this).html();
45 - eval(script);
46 - });
47 - </script>
48 -</head>
49 -<body>
50 - <p><a href="http://enterprisejquery.com/?p=106">Enterprise jQuery Blog Post about $.mockjax</a></p>
51 - <h2>What is jQuery.mockjax?</h2>
52 - <p>The Mockjax plugin for jQuery provides a decoupled non-invasive way to mock (or simulate) ajax requests throughout your application. It is possible to
53 - completely simulate the Ajax request/response cycle without any network traffic and without changing any production code. Below are a list of steps for making using
54 - of mockjax.</p>
55 -
56 - <ol>
57 - <li>Include jquery.mockjax.js jQuery Plugin</li>
58 - <li>Optionally include json2.js if not natively supported by your browser.</li>
59 - <li>Include jquery.xmldom.js if you're mocking XML inline.</li>
60 - <li>Include a file such as mockjax.js containing your mock definitions</li>
61 - </ol>
62 -
63 - <h2>Approaches to Mocking</h2>
64 -
65 - <p>There are a number of different approaches to mocking Ajax requests. Below is a list of request types and patterns
66 - supported by mockjax.</p>
67 -
68 - <dl>
69 - <dt>Mocking via Inline</dt>
70 - <dd>Mock response is included inline in the mock request definition.
71 - <span class="title">Simple Mock of JSON</span>
72 - <pre>
73 -$.mockjax({
74 - url: '/test/inline',
75 - dataType: 'json',
76 - responseTime: 2500,
77 - responseText: {
78 - say: 'Hello world!'
79 - }
80 -});
81 -
82 -// Normal ajax request in your application
83 -$.ajax({
84 - url: '/test/inline',
85 - dataType: 'json',
86 - success: function(json) {
87 - alert('You said: ' + json.say);
88 - }
89 -});
90 - </pre>
91 - </dd>
92 -
93 - <dt>Mocking via Proxy</dt>
94 - <dd>Mock response is contained in an external url, proxy attribute contains location of preferred response.
95 -<pre>
96 -$.mockjax({
97 - url: '/some/webservice',
98 - dataType: 'json',
99 - proxy: 'test.json'
100 -});
101 -
102 -// Normal ajax request in your application
103 -$.ajax({
104 - url: '/some/webservice',
105 - dataType: 'json',
106 - success: function(json) {
107 - alert('You said: ' + json.say);
108 - }
109 -});
110 -</pre>
111 - </dd>
112 -
113 - <dt>Mocking via Function</dt>
114 - <dd>There are two ways to mock via functions, the first provides the greatest flexability and allows for the
115 - developer to decide if they will intercept the request as a mock. In this situation, the developer should return
116 - a falsy value (false, undefined, or null) or an object literal containing the settings for this mock request.
117 -<pre>
118 -$.mockjax(function(settings) {
119 - if ( settings.dataType == 'json' ) {
120 - return {
121 - dataType: 'json',
122 - proxy: 'test.json'
123 - };
124 - }
125 - return false;
126 -});
127 -
128 -// Normal ajax request in your application
129 -$.ajax({
130 - url: '/some/webservice',
131 - dataType: 'json',
132 - success: function(json) {
133 - alert('You said: ' + json.say);
134 - }
135 -});
136 -</pre>
137 - <p>In the second form, a callback method is registered under the response attribute. The callback method is triggered
138 - right before the response is parsed. In the function body, the method has the opportunity to modify any attributes such
139 - as responseText or responseXML.</p>
140 -<pre>
141 -$.mockjax({
142 - url: '/some/webservice',
143 - dataType: 'json',
144 - response: function(settings) {
145 - this.responseText = { say: 'random ' + Math.random() };
146 - }
147 -});
148 -
149 -// Normal ajax request in your application
150 -$.ajax({
151 - url: '/some/webservice',
152 - dataType: 'json',
153 - success: function(json) {
154 - alert('You said: ' + json.say);
155 - }
156 -});
157 -</pre>
158 - </dd>
159 -
160 - </dl>
161 -
162 - <h2>Mocking Various Data Types</h2>
163 - <p></p>
164 -
165 - <h3>Mocking JSON</h3>
166 - <pre>
167 - $.mockjax({
168 - url: '/some/json',
169 - dataType: 'json',
170 - responseText: {
171 - say: "JSON Here"
172 - }
173 - });
174 -
175 - // Normal ajax request in your application
176 - $.ajax({
177 - url: '/some/json',
178 - dataType: 'json',
179 - success: function(json) {
180 - alert('You said: ' + json.say);
181 - }
182 - });
183 - </pre>
184 -
185 - <h3>Mocking XML</h3>
186 - <pre>
187 - $.mockjax({
188 - url: '/some/xml',
189 - dataType: 'xml',
190 - responseXML: '<document><say>Hello world XML</say></document>'
191 - });
192 -
193 - // Normal ajax request in your application
194 - $.ajax({
195 - url: '/some/xml',
196 - dataType: 'xml',
197 - success: function(xml) {
198 - alert('You said: ' + $(xml).find('document say').text() );
199 - }
200 - });
201 - </pre>
202 -
203 - <h3>Mocking HTML</h3>
204 - <pre>
205 - $.mockjax({
206 - url: '/some/webservice',
207 - dataType: 'html',
208 - responseText: '<div>Hello there</div>'
209 - });
210 -
211 - // Normal ajax request in your application
212 - $.ajax({
213 - url: '/some/webservice',
214 - dataType: 'html',
215 - success: function(html) {
216 - alert('You said: ' + html);
217 - }
218 - });
219 - </pre>
220 -</body>
221 -</html>
Index: trunk/extensions/UploadWizard/test/jasmine/lib/appendto-jquery-mockjax/jquery.mockjax.min.js
@@ -1,22 +0,0 @@
2 -/*!
3 - * MockJax - Mock for Ajax requests
4 - *
5 - * Version: 1.3.1
6 - * Released: 2010-08-11
7 - * Source: http://github.com/appendto/jquery-mockjax
8 - * Plugin: mockjax
9 - * Author: Jonathan Sharp (http://jdsharp.com)
10 - * License: MIT,GPL
11 - *
12 - * Copyright (c) 2010 appendTo LLC.
13 - * Dual licensed under the MIT or GPL licenses.
14 - * http://appendto.com/open-source-licenses
15 - */
16 -(function(c){var m=c.ajax,f=[];c.extend({ajax:function(j){var a=jQuery.extend(true,{},jQuery.ajaxSettings,j),n=false;c.each(f,function(g){if(f[g]){var b=null;if(c.isFunction(f[g]))b=f[g](a);else{b=f[g];if(c.isFunction(b.url.test))b.url.test(a.url)||(b=null);else{g=b.url.indexOf("*");if(b.url!="*"&&b.url!=a.url&&g==-1||g>-1&&b.url.substr(0,g)!=a.url.substr(0,g))b=null}if(b){if(b.data&&a.data){var l=false;(function d(h,k){c.each(h,function(i){if(k[i]===undefined)return l=false;else{l=true;if(typeof k[i]==
17 -"object")return d(h[i],k[i]);else return l=c.isFunction(h[i].test)?h[i].test(k[i]):h[i]==k[i]}})})(b.data,a.data);if(l==false)b=null}if(b&&b.type&&b.type!=a.type)b=null}}if(b){typeof console!=="undefined"&&console.log&&console.log("MOCK GET: "+a.url);n=true;if(a.dataType==="jsonp"){if(type==="GET")e.test(a.url)||(a.url+=(rquery.test(a.url)?"&":"?")+(a.jsonp||"callback")+"=?");else if(!a.data||!e.test(a.data))a.data=(a.data?a.data+"&":"")+(a.jsonp||"callback")+"=?";a.dataType="json"}var e=/=\?(&|$)/;
18 -if(a.dataType==="json"&&(a.data&&e.test(a.data)||e.test(a.url))){jsonp=a.jsonpCallback||"jsonp"+jsc++;if(a.data)a.data=(a.data+"").replace(e,"="+jsonp+"$1");a.url=a.url.replace(e,"="+jsonp+"$1");a.dataType="script";window[jsonp]=window[jsonp]||function(d){data=d;o();p();window[jsonp]=undefined;try{delete window[jsonp]}catch(h){}head&&head.removeChild(script)}}e=(e=/^(\w+:)?\/\/([^\/?#]+)/.exec(a.url))&&(e[1]&&e[1]!==location.protocol||e[2]!==location.host);if(a.dataType==="script"&&a.type==="GET"&&
19 -e){var q=j&&j.context||a,o=function(){if(a.success)a.success.call(q,b.response?b.response.toString():b.responseText||"",status,{});if(a.global){var d=[{},a];(a.context?jQuery(a.context):jQuery.event).trigger("ajaxSuccess",d)}},p=function(){a.complete&&a.complete.call(q,{},status);if(a.global){var d=[{},a];(a.context?jQuery(a.context):jQuery.event).trigger("ajaxComplete",d)}a.global&&!--jQuery.active&&jQuery.event.trigger("ajaxStop")};c.globalEval(b.responseText);o();p();return false}m.call(c,c.extend(true,
20 -{},j,{xhr:function(){b=c.extend({},c.mockjaxSettings,b);return{status:b.status,readyState:1,open:function(){},send:function(){var d=c.proxy(function(){this.status=b.status;this.readyState=4;c.isFunction(b.response)&&b.response();if(a.dataType=="json"&&typeof b.responseText=="object")this.responseText=JSON.stringify(b.responseText);else if(a.dataType=="xml")this.responseXML=c.xmlDOM&&typeof b.responseXML=="string"?c.xmlDOM(b.responseXML)[0]:b.responseXML;else this.responseText=b.responseText;this.onreadystatechange(b.isTimeout?
21 -"timeout":undefined)},this);if(b.proxy)m({global:false,url:b.proxy,type:b.type,data:b.data,dataType:a.dataType,complete:function(h){b.responseXML=h.responseXML;b.responseText=h.responseText;d()}});else if(a.async===false)d();else this.responseTimer=setTimeout(d,b.responseTime||50)},abort:function(){clearTimeout(this.responseTimer)},setRequestHeader:function(){},getResponseHeader:function(d){if(b.headers&&b.headers[d])return b.headers[d];else if(d=="Last-modified")return b.lastModified||(new Date).toString();
22 -else if(d=="Etag")return b.etag||"";else if(d=="content-type")return b.contentType||"text/plain"}}}}));return false}}});if(!n)return m.apply(c,arguments)}});c.mockjaxSettings={status:200,responseTime:500,isTimeout:false,contentType:"text/plain",response:"",responseText:"",responseXML:"",proxy:"",lastModified:null,etag:"",headers:{etag:"IJF@H#@923uf8023hFO@I#H#","content-type":"text/plain"}};c.mockjax=function(j){var a=f.length;f[a]=j;return a};c.mockjaxClear=function(j){if(arguments.length==1)f[j]=
23 -null;else f=[]}})(jQuery);
Index: trunk/extensions/UploadWizard/test/jasmine/lib/appendto-jquery-mockjax/test.json
@@ -1 +0,0 @@
2 -{ "say" : "I'm a json file!" }
Index: trunk/extensions/UploadWizard/test/jasmine/lib/appendto-jquery-mockjax/mockjax.demo.js
@@ -1,33 +0,0 @@
2 -
3 -// Example of a getScript or dataType == 'script'
4 -$.mockjax({
5 - url: 'example.com/foo',
6 - response: function() {
7 - // This method is executed when a script request comes in
8 - }
9 -});
10 -
11 -
12 -
13 -/*
14 - * This file contains the mock ajax requests
15 - */
16 -$.mockjax({
17 - url: '/rest/search/store',
18 - contentType: 'text/json',
19 - responseText: {
20 - status: 'success',
21 - data: {
22 - paging: {
23 - current: 1,
24 - total: 5
25 - },
26 - results: [
27 - 'product-001',
28 - 'product-002',
29 - 'product-003',
30 - 'product-004'
31 - ]
32 - }
33 - }
34 -});
\ No newline at end of file
Index: trunk/extensions/UploadWizard/test/jasmine/lib/appendto-jquery-mockjax/test.js
@@ -1,54 +0,0 @@
2 -(function($) {
3 - $(function() {
4 - $.ajax({
5 - url: 'test.json',
6 - success: function(data) {
7 - $('ul').append('<li>test.json: completed (' + data.test + ')</li>');
8 - }
9 - });
10 -
11 - $.mockjax({
12 - url: 'test.json',
13 - contentType: 'text/json',
14 - responseText: { "test": "mock message" }
15 - });
16 -
17 - $.ajax({
18 - url: 'test.json',
19 - dataType: 'json',
20 - success: function(data) {
21 - $('ul').append('<li>test.json: completed (' + data.test + ')</li>');
22 - },
23 - error: function(xhr, status, error) {
24 - alert('error: ' + status + ' ' + error);
25 - },
26 - complete: function() {
27 - }
28 - });
29 -
30 - $.mockjax({
31 - url: 'http://google.com',
32 - responseText: 'alert("Hello world");'
33 - });
34 -
35 - $.mockjax({
36 - url: 'http://another-cross-domain.com',
37 - responseText: function() {
38 - alert("Get script mock");
39 - }
40 - });
41 -
42 - $.ajax({
43 - url: 'http://google.com',
44 - dataType: 'script',
45 - success: function(data) {
46 - $('ul').append('<li>script: completed (' + data.test + ')</li>');
47 - },
48 - error: function(xhr, status, error) {
49 - alert('error: ' + status + ' ' + error);
50 - },
51 - complete: function() {
52 - }
53 - });
54 - });
55 -})(jQuery);
Index: trunk/extensions/UploadWizard/test/jasmine/lib/appendto-jquery-mockjax/jquery.mockjax.js
@@ -1,12 +1,13 @@
22 /*!
3 - * MockJax - Mock for Ajax requests
 3+ * MockJax - jQuery Plugin to Mock Ajax requests
44 *
5 - * Version: 1.3.1
6 - * Released: 2010-08-11
7 - * Source: http://github.com/appendto/jquery-mockjax
8 - * Plugin: mockjax
9 - * Author: Jonathan Sharp (http://jdsharp.com)
10 - * License: MIT,GPL
 5+ * Version: 1.3.3
 6+ * Released: 2010-11-05
 7+ * Source: http://github.com/appendto/jquery-mockjax
 8+ * Docs: http://enterprisejquery.com/2010/07/mock-your-ajax-requests-with-mockjax-for-rapid-development
 9+ * Plugin: mockjax
 10+ * Author: Jonathan Sharp (http://jdsharp.com)
 11+ * License: MIT,GPL
1112 *
1213 * Copyright (c) 2010 appendTo LLC.
1314 * Dual licensed under the MIT or GPL licenses.
@@ -55,6 +56,12 @@
5657 var identical = false;
5758 // Deep inspect the identity of the objects
5859 (function ident(mock, live) {
 60+ // Test for situations where the data is a querystring (not an object)
 61+ if (typeof live === 'string') {
 62+ // Querystring may be a regex
 63+ identical = $.isFunction( mock.test ) ? mock.test(live) : mock == live;
 64+ return identical;
 65+ }
5966 $.each(mock, function(k, v) {
6067 if ( live[k] === undefined ) {
6168 identical = false;
@@ -88,14 +95,14 @@
8996 }
9097 if ( m ) {
9198 if ( typeof console !== 'undefined' && console.log ) {
92 - console.log('MOCK GET: ' + s.url);
 99+ console.log('MOCK ' + s.type + ': ' + s.url);
93100 }
94101 mock = true;
95102
96103 // Handle JSONP Parameter Callbacks, we need to replicate some of the jQuery core here
97104 // because there isn't an easy hook for the cross domain script tag of jsonp
98105 if ( s.dataType === "jsonp" ) {
99 - if ( type === "GET" ) {
 106+ if ( s.type.toUpperCase() === "GET" ) {
100107 if ( !jsre.test( s.url ) ) {
101108 s.url += (rquery.test( s.url ) ? "&" : "?") + (s.jsonp || "callback") + "=?";
102109 }
@@ -144,7 +151,7 @@
145152 remote = parts && (parts[1] && parts[1] !== location.protocol || parts[2] !== location.host);
146153
147154 // Test if we are going to create a script tag (if so, intercept & mock)
148 - if ( s.dataType === "script" && s.type === "GET" && remote ) {
 155+ if ( s.dataType === "script" && s.type.toUpperCase() === "GET" && remote ) {
149156 // Synthesize the mock request for adding a script tag
150157 var callbackContext = origSettings && origSettings.context || s;
151158
@@ -181,16 +188,16 @@
182189 (s.context ? jQuery(s.context) : jQuery.event).trigger(type, args);
183190 }
184191
185 - //if ( m.response && $.isFunction(m.response) ) {
186 - // m.response();
187 - //} else {
 192+ if ( m.response && $.isFunction(m.response) ) {
 193+ m.response(origSettings);
 194+ } else {
188195 $.globalEval(m.responseText);
189 - //}
 196+ }
190197 success();
191198 complete();
192199 return false;
193200 }
194 - _ajax.call($, $.extend(true, {}, origSettings, {
 201+ mock = _ajax.call($, $.extend(true, {}, origSettings, {
195202 // Mock the XHR object
196203 xhr: function() {
197204 // Extend with our default mockjax settings
@@ -209,7 +216,7 @@
210217 // We have an executable function, call it to give
211218 // the mock a chance to update it's data
212219 if ( $.isFunction(m.response) ) {
213 - m.response();
 220+ m.response(origSettings);
214221 }
215222 // Copy over our mock to our xhr object before passing control back to
216223 // jQuery's onreadystatechange callback
@@ -239,7 +246,7 @@
240247 complete: function(xhr, txt) {
241248 m.responseXML = xhr.responseXML;
242249 m.responseText = xhr.responseText;
243 - process();
 250+ this.responseTimer = setTimeout(process, m.responseTime || 0);
244251 }
245252 });
246253 } else {
@@ -278,21 +285,23 @@
279286 // We don't have a mock request, trigger a normal request
280287 if ( !mock ) {
281288 return _ajax.apply($, arguments);
 289+ } else {
 290+ return mock;
282291 }
283292 }
284293 });
285294
286295 $.mockjaxSettings = {
287 - //url: null,
288 - //type: 'GET',
289 - status: 200,
290 - responseTime: 500,
291 - isTimeout: false,
292 - contentType: 'text/plain',
293 - response: '',
294 - responseText: '',
295 - responseXML: '',
296 - proxy: '',
 296+ //url: null,
 297+ //type: 'GET',
 298+ status: 200,
 299+ responseTime: 500,
 300+ isTimeout: false,
 301+ contentType: 'text/plain',
 302+ response: '',
 303+ responseText: '',
 304+ responseXML: '',
 305+ proxy: '',
297306
298307 lastModified: null,
299308 etag: '',

Status & tagging log