Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add http status code support #346

Open
wants to merge 1 commit into
base: refactoring
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/mock/xhr/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,15 @@ Util.extend(MockXMLHttpRequest.prototype, {
that.readyState = MockXMLHttpRequest.LOADING
that.dispatchEvent(new Event('readystatechange' /*, false, false, that*/ ))

that.status = 200
that.statusText = HTTP_STATUS_CODES[200]

// fix #92 #93 by @qddegtya
that.response = that.responseText = JSON.stringify(
convert(that.custom.template, that.custom.options),
null, 4
)

that.status = that.custom.options.status || 200
that.statusText = HTTP_STATUS_CODES[that.status]

that.readyState = MockXMLHttpRequest.DONE
that.dispatchEvent(new Event('readystatechange' /*, false, false, that*/ ))
that.dispatchEvent(new Event('load' /*, false, false, that*/ ));
Expand Down Expand Up @@ -435,8 +435,15 @@ function find(options) {

// 数据模板 => 响应数据
function convert(item, options) {
return Util.isFunction(item.template) ?
item.template(options) : MockXMLHttpRequest.Mock.mock(item.template)
if (Util.isFunction(item.template)) {
var data = item.template(options)
// 数据模板中的返回参构造处理
// _status 控制返回状态码
data._status && data._status !== 0 && (options.status = data._status)
delete data._status
return data
}
return MockXMLHttpRequest.Mock.mock(item.template)
}

module.exports = MockXMLHttpRequest
module.exports = MockXMLHttpRequest