Skip to content

Commit eb4877b

Browse files
committed
feat: add http status support
1 parent c4d7cba commit eb4877b

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/mock/xhr/xhr.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,15 @@ Util.extend(MockXMLHttpRequest.prototype, {
285285
that.readyState = MockXMLHttpRequest.LOADING
286286
that.dispatchEvent(new Event('readystatechange' /*, false, false, that*/ ))
287287

288-
that.status = 200
289-
that.statusText = HTTP_STATUS_CODES[200]
290-
291288
// fix #92 #93 by @qddegtya
292289
that.response = that.responseText = JSON.stringify(
293290
convert(that.custom.template, that.custom.options),
294291
null, 4
295292
)
296293

294+
that.status = that.custom.options.status || 200
295+
that.statusText = HTTP_STATUS_CODES[that.status]
296+
297297
that.readyState = MockXMLHttpRequest.DONE
298298
that.dispatchEvent(new Event('readystatechange' /*, false, false, that*/ ))
299299
that.dispatchEvent(new Event('load' /*, false, false, that*/ ));
@@ -435,8 +435,15 @@ function find(options) {
435435

436436
// 数据模板 => 响应数据
437437
function convert(item, options) {
438-
return Util.isFunction(item.template) ?
439-
item.template(options) : MockXMLHttpRequest.Mock.mock(item.template)
438+
if (Util.isFunction(item.template)) {
439+
var data = item.template(options)
440+
// 数据模板中的返回参构造处理
441+
// _status 控制返回状态码
442+
data._status && data._status !== 0 && (options.status = data._status)
443+
delete data._status
444+
return data
445+
}
446+
return MockXMLHttpRequest.Mock.mock(item.template)
440447
}
441448

442-
module.exports = MockXMLHttpRequest
449+
module.exports = MockXMLHttpRequest

0 commit comments

Comments
 (0)