Skip to content

Commit 7ed2a84

Browse files
committed
fix nuysoft#82 options 只含有 url, type, body 三个属性
1 parent 9ac70a9 commit 7ed2a84

File tree

5 files changed

+169
-4
lines changed

5 files changed

+169
-4
lines changed

dist/mock-min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/mock-min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/mock.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8238,6 +8238,7 @@ return /******/ (function(modules) { // webpackBootstrap
82388238
// Initiates the request.
82398239
send: function send(data) {
82408240
var that = this
8241+
this.custom.options.body = data
82418242

82428243
// 原生 XHR
82438244
if (!this.match) {

src/mock/xhr/xhr.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ Util.extend(MockXMLHttpRequest.prototype, {
253253
// Initiates the request.
254254
send: function send(data) {
255255
var that = this
256+
this.custom.options.body = data
256257

257258
// 原生 XHR
258259
if (!this.match) {

test/test.mock.request.js

Lines changed: 165 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ describe('Request', function() {
100100
var that = this
101101
var url = 'rurl_function.json'
102102

103-
Mock.mock(/rurl_function\.json/, function( /*options*/ ) {
103+
Mock.mock(/rurl_function\.json/, function(options) {
104+
expect(options).to.not.equal(undefined)
105+
expect(options.url).to.be.equal(url)
106+
expect(options.type).to.be.equal('GET')
107+
expect(options.body).to.be.equal(null)
104108
return Mock.mock({
105109
'list|1-10': [{
106110
'id|+1': 1,
@@ -127,6 +131,85 @@ describe('Request', function() {
127131
})
128132
})
129133

134+
describe('Mock.mock( rurl, function(options) ) + GET + data', function() {
135+
it('', function(done) {
136+
var that = this
137+
var url = 'rurl_function.json'
138+
139+
Mock.mock(/rurl_function\.json/, function(options) {
140+
expect(options).to.not.equal(undefined)
141+
expect(options.url).to.be.equal(url + '?foo=1')
142+
expect(options.type).to.be.equal('GET')
143+
expect(options.body).to.be.equal(null)
144+
return Mock.mock({
145+
'list|1-10': [{
146+
'id|+1': 1,
147+
'email': '@EMAIL'
148+
}]
149+
})
150+
})
151+
152+
$.ajax({
153+
url: url,
154+
dataType: 'json',
155+
data: {
156+
foo: 1
157+
}
158+
}).done(function(data /*, status, jqXHR*/ ) {
159+
that.test.title += url + ' => ' + stringify(data)
160+
expect(data).to.have.property('list')
161+
.that.be.an('array').with.length.within(1, 10)
162+
_.each(data.list, function(item, index, list) {
163+
if (index > 0) expect(item.id).to.be.equal(list[index - 1].id + 1)
164+
})
165+
}).fail(function(jqXHR, textStatus, errorThrown) {
166+
console.log(jqXHR, textStatus, errorThrown)
167+
}).always(function() {
168+
done()
169+
})
170+
})
171+
})
172+
173+
describe('Mock.mock( rurl, function(options) ) + POST + data', function() {
174+
it('', function(done) {
175+
var that = this
176+
var url = 'rurl_function.json'
177+
178+
Mock.mock(/rurl_function\.json/, function(options) {
179+
expect(options).to.not.equal(undefined)
180+
expect(options.url).to.be.equal(url)
181+
expect(options.type).to.be.equal('POST')
182+
expect(options.body).to.be.equal('foo=1')
183+
return Mock.mock({
184+
'list|1-10': [{
185+
'id|+1': 1,
186+
'email': '@EMAIL'
187+
}]
188+
})
189+
})
190+
191+
$.ajax({
192+
url: url,
193+
type: 'post',
194+
dataType: 'json',
195+
data: {
196+
foo: 1
197+
}
198+
}).done(function(data /*, status, jqXHR*/ ) {
199+
that.test.title += url + ' => ' + stringify(data)
200+
expect(data).to.have.property('list')
201+
.that.be.an('array').with.length.within(1, 10)
202+
_.each(data.list, function(item, index, list) {
203+
if (index > 0) expect(item.id).to.be.equal(list[index - 1].id + 1)
204+
})
205+
}).fail(function(jqXHR, textStatus, errorThrown) {
206+
console.log(jqXHR, textStatus, errorThrown)
207+
}).always(function() {
208+
done()
209+
})
210+
})
211+
})
212+
130213
describe('Mock.mock( rurl, rtype, template )', function() {
131214
it('', function(done) {
132215
var that = this
@@ -193,12 +276,20 @@ describe('Request', function() {
193276
var url = 'rurl_rtype_function.json'
194277
var count = 0
195278

196-
Mock.mock(/rurl_rtype_function\.json/, /get/, function() {
279+
Mock.mock(/rurl_rtype_function\.json/, /get/, function(options) {
280+
expect(options).to.not.equal(undefined)
281+
expect(options.url).to.be.equal(url)
282+
expect(options.type).to.be.equal('GET')
283+
expect(options.body).to.be.equal(null)
197284
return {
198285
type: 'get'
199286
}
200287
})
201288
Mock.mock(/rurl_rtype_function\.json/, /post|put/, function(options) {
289+
expect(options).to.not.equal(undefined)
290+
expect(options.url).to.be.equal(url)
291+
expect(['POST', 'PUT']).to.include(options.type)
292+
expect(options.body).to.be.equal(null)
202293
return {
203294
type: options.type.toLowerCase()
204295
}
@@ -232,6 +323,78 @@ describe('Request', function() {
232323
}).done(success).always(complete)
233324

234325

326+
function success( /*data*/ ) {
327+
count++
328+
}
329+
330+
function complete() {
331+
if (count === 3) done()
332+
}
333+
334+
})
335+
})
336+
describe('Mock.mock( rurl, rtype, function(options) ) + data', function() {
337+
it('', function(done) {
338+
var that = this
339+
var url = 'rurl_rtype_function.json'
340+
var count = 0
341+
342+
Mock.mock(/rurl_rtype_function\.json/, /get/, function(options) {
343+
expect(options).to.not.equal(undefined)
344+
expect(options.url).to.be.equal(url + '?foo=1')
345+
expect(options.type).to.be.equal('GET')
346+
expect(options.body).to.be.equal(null)
347+
return {
348+
type: 'get'
349+
}
350+
})
351+
Mock.mock(/rurl_rtype_function\.json/, /post|put/, function(options) {
352+
expect(options).to.not.equal(undefined)
353+
expect(options.url).to.be.equal(url)
354+
expect(['POST', 'PUT']).to.include(options.type)
355+
expect(options.body).to.be.equal('foo=1')
356+
return {
357+
type: options.type.toLowerCase()
358+
}
359+
})
360+
361+
$.ajax({
362+
url: url,
363+
type: 'get',
364+
dataType: 'json',
365+
data: {
366+
foo: 1
367+
}
368+
}).done(function(data /*, status, jqXHR*/ ) {
369+
that.test.title += 'GET ' + url + ' => ' + stringify(data)
370+
expect(data).to.have.property('type', 'get')
371+
}).done(success).always(complete)
372+
373+
$.ajax({
374+
url: url,
375+
type: 'post',
376+
dataType: 'json',
377+
data: {
378+
foo: 1
379+
}
380+
}).done(function(data /*, status, jqXHR*/ ) {
381+
that.test.title += 'POST ' + url + ' => ' + stringify(data)
382+
expect(data).to.have.property('type', 'post')
383+
}).done(success).always(complete)
384+
385+
$.ajax({
386+
url: url,
387+
type: 'put',
388+
dataType: 'json',
389+
data: {
390+
foo: 1
391+
}
392+
}).done(function(data /*, status, jqXHR*/ ) {
393+
that.test.title += 'PUT ' + url + ' => ' + stringify(data)
394+
expect(data).to.have.property('type', 'put')
395+
}).done(success).always(complete)
396+
397+
235398
function success( /*data*/ ) {
236399
count++
237400
}

0 commit comments

Comments
 (0)