test.mock.request.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /* global console, require, chai, describe, before, it */
  2. // 数据占位符定义(Data Placeholder Definition,DPD)
  3. var expect = chai.expect
  4. var Mock, $, _
  5. describe('Request', function() {
  6. before(function(done) {
  7. require(['mock', 'underscore', 'jquery'], function() {
  8. Mock = arguments[0]
  9. _ = arguments[1]
  10. $ = arguments[2]
  11. expect(Mock).to.not.equal(undefined)
  12. expect(_).to.not.equal(undefined)
  13. expect($).to.not.equal(undefined)
  14. done()
  15. })
  16. })
  17. function stringify(json) {
  18. return JSON.stringify(json /*, null, 4*/ )
  19. }
  20. describe('jQuery.ajax()', function() {
  21. it('', function(done) {
  22. var that = this
  23. var url = Math.random()
  24. $.ajax({
  25. url: url,
  26. dataType: 'json'
  27. }).done(function( /*data, textStatus, jqXHR*/ ) {
  28. // 不会进入
  29. }).fail(function(jqXHR /*, textStatus, errorThrown*/ ) {
  30. // 浏览器 || PhantomJS
  31. expect([404, 0]).to.include(jqXHR.status)
  32. that.test.title += url + ' => ' + jqXHR.status
  33. }).always(function() {
  34. done()
  35. })
  36. })
  37. })
  38. describe('jQuery.getScript()', function() {
  39. it('', function(done) {
  40. var that = this
  41. var url = './materiels/noop.js'
  42. $.getScript(url, function(script, textStatus, jqXHR) {
  43. expect(script).to.be.ok
  44. that.test.title += url + ' => ' + jqXHR.status + ' ' + textStatus
  45. done()
  46. })
  47. })
  48. })
  49. describe('jQuery.load()', function() {
  50. it('', function(done) {
  51. var that = this
  52. var url = './materiels/noop.html'
  53. $('<div>').load(url, function(responseText /*, textStatus, jqXHR*/ ) {
  54. expect(responseText).to.be.ok
  55. that.test.title += url + ' => ' + responseText
  56. done()
  57. })
  58. })
  59. })
  60. describe('jQuery.ajax() XHR Fields', function() {
  61. it('', function(done) {
  62. var that = this
  63. var url = Math.random()
  64. var xhr
  65. $.ajax({
  66. xhr: function() {
  67. xhr = $.ajaxSettings.xhr()
  68. return xhr
  69. },
  70. url: url,
  71. dataType: 'json',
  72. xhrFields: {
  73. timeout: 123,
  74. withCredentials: true
  75. }
  76. }).done(function( /*data, textStatus, jqXHR*/ ) {
  77. // 不会进入
  78. }).fail(function(jqXHR /*, textStatus, errorThrown*/ ) {
  79. // 浏览器 || PhantomJS
  80. expect([404, 0]).to.include(jqXHR.status)
  81. that.test.title += url + ' => ' + jqXHR.status
  82. expect(xhr.timeout).to.be.equal(123)
  83. expect(xhr.withCredentials).to.be.equal(true)
  84. }).always(function() {
  85. done()
  86. })
  87. })
  88. })
  89. describe('Mock.mock( rurl, template )', function() {
  90. it('', function(done) {
  91. var that = this
  92. var url = 'rurl_template.json'
  93. Mock.mock(/rurl_template.json/, {
  94. 'list|1-10': [{
  95. 'id|+1': 1,
  96. 'email': '@EMAIL'
  97. }]
  98. })
  99. Mock.setup({
  100. // timeout: 100,
  101. timeout: '10-50',
  102. })
  103. $.ajax({
  104. url: url,
  105. dataType: 'json'
  106. }).done(function(data /*, textStatus, jqXHR*/ ) {
  107. that.test.title += url + ' => ' + stringify(data)
  108. expect(data).to.have.property('list')
  109. .that.be.an('array').with.length.within(1, 10)
  110. _.each(data.list, function(item, index, list) {
  111. if (index > 0) expect(item.id).to.be.equal(list[index - 1].id + 1)
  112. })
  113. }).fail(function(jqXHR, textStatus, errorThrown) {
  114. console.log(jqXHR, textStatus, errorThrown)
  115. }).always(function() {
  116. done()
  117. })
  118. })
  119. })
  120. describe('Mock.mock( rurl, function(options) )', function() {
  121. it('', function(done) {
  122. var that = this
  123. var url = 'rurl_function.json'
  124. Mock.mock(/rurl_function\.json/, function(options) {
  125. expect(options).to.not.equal(undefined)
  126. expect(options.url).to.be.equal(url)
  127. expect(options.type).to.be.equal('GET')
  128. expect(options.body).to.be.equal(null)
  129. return Mock.mock({
  130. 'list|1-10': [{
  131. 'id|+1': 1,
  132. 'email': '@EMAIL'
  133. }]
  134. })
  135. })
  136. $.ajax({
  137. url: url,
  138. dataType: 'json'
  139. }).done(function(data /*, status, jqXHR*/ ) {
  140. that.test.title += url + ' => ' + stringify(data)
  141. expect(data).to.have.property('list')
  142. .that.be.an('array').with.length.within(1, 10)
  143. _.each(data.list, function(item, index, list) {
  144. if (index > 0) expect(item.id).to.be.equal(list[index - 1].id + 1)
  145. })
  146. }).fail(function(jqXHR, textStatus, errorThrown) {
  147. console.log(jqXHR, textStatus, errorThrown)
  148. }).always(function() {
  149. done()
  150. })
  151. })
  152. })
  153. describe('Mock.mock( rurl, function(options) ) + GET + data', function() {
  154. it('', function(done) {
  155. var that = this
  156. var url = 'rurl_function.json'
  157. Mock.mock(/rurl_function\.json/, function(options) {
  158. expect(options).to.not.equal(undefined)
  159. expect(options.url).to.be.equal(url + '?foo=1')
  160. expect(options.type).to.be.equal('GET')
  161. expect(options.body).to.be.equal(null)
  162. return Mock.mock({
  163. 'list|1-10': [{
  164. 'id|+1': 1,
  165. 'email': '@EMAIL'
  166. }]
  167. })
  168. })
  169. $.ajax({
  170. url: url,
  171. dataType: 'json',
  172. data: {
  173. foo: 1
  174. }
  175. }).done(function(data /*, status, jqXHR*/ ) {
  176. that.test.title += url + ' => ' + stringify(data)
  177. expect(data).to.have.property('list')
  178. .that.be.an('array').with.length.within(1, 10)
  179. _.each(data.list, function(item, index, list) {
  180. if (index > 0) expect(item.id).to.be.equal(list[index - 1].id + 1)
  181. })
  182. }).fail(function(jqXHR, textStatus, errorThrown) {
  183. console.log(jqXHR, textStatus, errorThrown)
  184. }).always(function() {
  185. done()
  186. })
  187. })
  188. })
  189. describe('Mock.mock( rurl, function(options) ) + POST + data', function() {
  190. it('', function(done) {
  191. var that = this
  192. var url = 'rurl_function.json'
  193. Mock.mock(/rurl_function\.json/, function(options) {
  194. expect(options).to.not.equal(undefined)
  195. expect(options.url).to.be.equal(url)
  196. expect(options.type).to.be.equal('POST')
  197. expect(options.body).to.be.equal('foo=1')
  198. return Mock.mock({
  199. 'list|1-10': [{
  200. 'id|+1': 1,
  201. 'email': '@EMAIL'
  202. }]
  203. })
  204. })
  205. $.ajax({
  206. url: url,
  207. type: 'post',
  208. dataType: 'json',
  209. data: {
  210. foo: 1
  211. }
  212. }).done(function(data /*, status, jqXHR*/ ) {
  213. that.test.title += url + ' => ' + stringify(data)
  214. expect(data).to.have.property('list')
  215. .that.be.an('array').with.length.within(1, 10)
  216. _.each(data.list, function(item, index, list) {
  217. if (index > 0) expect(item.id).to.be.equal(list[index - 1].id + 1)
  218. })
  219. }).fail(function(jqXHR, textStatus, errorThrown) {
  220. console.log(jqXHR, textStatus, errorThrown)
  221. }).always(function() {
  222. done()
  223. })
  224. })
  225. })
  226. describe('Mock.mock( rurl, rtype, template )', function() {
  227. it('', function(done) {
  228. var that = this
  229. var url = 'rurl_rtype_template.json'
  230. var count = 0
  231. Mock.mock(/rurl_rtype_template\.json/, 'get', {
  232. 'list|1-10': [{
  233. 'id|+1': 1,
  234. 'email': '@EMAIL',
  235. type: 'get'
  236. }]
  237. })
  238. Mock.mock(/rurl_rtype_template\.json/, 'post', {
  239. 'list|1-10': [{
  240. 'id|+1': 1,
  241. 'email': '@EMAIL',
  242. type: 'post'
  243. }]
  244. })
  245. $.ajax({
  246. url: url,
  247. type: 'get',
  248. dataType: 'json'
  249. }).done(function(data /*, status, jqXHR*/ ) {
  250. that.test.title += 'GET ' + url + ' => ' + stringify(data) + ' '
  251. expect(data).to.have.property('list')
  252. .that.be.an('array').with.length.within(1, 10)
  253. _.each(data.list, function(item, index, list) {
  254. if (index > 0) expect(item.id).to.be.equal(list[index - 1].id + 1)
  255. expect(item).to.have.property('type').equal('get')
  256. })
  257. }).done(success).always(complete)
  258. $.ajax({
  259. url: url,
  260. type: 'post',
  261. dataType: 'json'
  262. }).done(function(data /*, status, jqXHR*/ ) {
  263. that.test.title += 'POST ' + url + ' => ' + stringify(data) + ' '
  264. expect(data).to.have.property('list')
  265. .that.be.an('array').with.length.within(1, 10)
  266. _.each(data.list, function(item, index, list) {
  267. if (index > 0) expect(item.id).to.be.equal(list[index - 1].id + 1)
  268. expect(item).to.have.property('type').equal('post')
  269. })
  270. }).done(success).always(complete)
  271. function success( /*data*/ ) {
  272. count++
  273. }
  274. function complete() {
  275. if (count === 2) done()
  276. }
  277. })
  278. })
  279. describe('Mock.mock( rurl, rtype, function(options) )', function() {
  280. it('', function(done) {
  281. var that = this
  282. var url = 'rurl_rtype_function.json'
  283. var count = 0
  284. Mock.mock(/rurl_rtype_function\.json/, /get/, function(options) {
  285. expect(options).to.not.equal(undefined)
  286. expect(options.url).to.be.equal(url)
  287. expect(options.type).to.be.equal('GET')
  288. expect(options.body).to.be.equal(null)
  289. return {
  290. type: 'get'
  291. }
  292. })
  293. Mock.mock(/rurl_rtype_function\.json/, /post|put/, function(options) {
  294. expect(options).to.not.equal(undefined)
  295. expect(options.url).to.be.equal(url)
  296. expect(['POST', 'PUT']).to.include(options.type)
  297. expect(options.body).to.be.equal(null)
  298. return {
  299. type: options.type.toLowerCase()
  300. }
  301. })
  302. $.ajax({
  303. url: url,
  304. type: 'get',
  305. dataType: 'json'
  306. }).done(function(data /*, status, jqXHR*/ ) {
  307. that.test.title += 'GET ' + url + ' => ' + stringify(data)
  308. expect(data).to.have.property('type', 'get')
  309. }).done(success).always(complete)
  310. $.ajax({
  311. url: url,
  312. type: 'post',
  313. dataType: 'json'
  314. }).done(function(data /*, status, jqXHR*/ ) {
  315. that.test.title += 'POST ' + url + ' => ' + stringify(data)
  316. expect(data).to.have.property('type', 'post')
  317. }).done(success).always(complete)
  318. $.ajax({
  319. url: url,
  320. type: 'put',
  321. dataType: 'json'
  322. }).done(function(data /*, status, jqXHR*/ ) {
  323. that.test.title += 'PUT ' + url + ' => ' + stringify(data)
  324. expect(data).to.have.property('type', 'put')
  325. }).done(success).always(complete)
  326. function success( /*data*/ ) {
  327. count++
  328. }
  329. function complete() {
  330. if (count === 3) done()
  331. }
  332. })
  333. })
  334. describe('Mock.mock( rurl, rtype, function(options) ) + data', function() {
  335. it('', function(done) {
  336. var that = this
  337. var url = 'rurl_rtype_function.json'
  338. var count = 0
  339. Mock.mock(/rurl_rtype_function\.json/, /get/, function(options) {
  340. expect(options).to.not.equal(undefined)
  341. expect(options.url).to.be.equal(url + '?foo=1')
  342. expect(options.type).to.be.equal('GET')
  343. expect(options.body).to.be.equal(null)
  344. return {
  345. type: 'get'
  346. }
  347. })
  348. Mock.mock(/rurl_rtype_function\.json/, /post|put/, function(options) {
  349. expect(options).to.not.equal(undefined)
  350. expect(options.url).to.be.equal(url)
  351. expect(['POST', 'PUT']).to.include(options.type)
  352. expect(options.body).to.be.equal('foo=1')
  353. return {
  354. type: options.type.toLowerCase()
  355. }
  356. })
  357. $.ajax({
  358. url: url,
  359. type: 'get',
  360. dataType: 'json',
  361. data: {
  362. foo: 1
  363. }
  364. }).done(function(data /*, status, jqXHR*/ ) {
  365. that.test.title += 'GET ' + url + ' => ' + stringify(data)
  366. expect(data).to.have.property('type', 'get')
  367. }).done(success).always(complete)
  368. $.ajax({
  369. url: url,
  370. type: 'post',
  371. dataType: 'json',
  372. data: {
  373. foo: 1
  374. }
  375. }).done(function(data /*, status, jqXHR*/ ) {
  376. that.test.title += 'POST ' + url + ' => ' + stringify(data)
  377. expect(data).to.have.property('type', 'post')
  378. }).done(success).always(complete)
  379. $.ajax({
  380. url: url,
  381. type: 'put',
  382. dataType: 'json',
  383. data: {
  384. foo: 1
  385. }
  386. }).done(function(data /*, status, jqXHR*/ ) {
  387. that.test.title += 'PUT ' + url + ' => ' + stringify(data)
  388. expect(data).to.have.property('type', 'put')
  389. }).done(success).always(complete)
  390. function success( /*data*/ ) {
  391. count++
  392. }
  393. function complete() {
  394. if (count === 3) done()
  395. }
  396. })
  397. })
  398. describe('#105 addEventListener', function() {
  399. it('addEventListene => addEventListener', function(done) {
  400. var xhr = new Mock.XHR()
  401. expect(xhr.addEventListener).to.not.equal(undefined)
  402. expect(xhr.addEventListene).to.equal(undefined)
  403. done()
  404. })
  405. })
  406. })