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: 支持设置responseHeader等属性 #453

Open
wants to merge 2 commits into
base: refactoring
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions dist/mock-min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mock-min.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions dist/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -8279,6 +8279,9 @@ return /******/ (function(modules) { // webpackBootstrap
}
})

// 拓展this.custom属性, 用于设置responseHeaders等属性
Util.extend(this.custom, MockXMLHttpRequest._settings)

this.custom.timeout = function(timeout) {
if (typeof timeout === 'number') return timeout
if (typeof timeout === 'string' && !~timeout.indexOf('-')) return parseInt(timeout, 10)
Expand Down Expand Up @@ -8537,6 +8540,7 @@ return /******/ (function(modules) { // webpackBootstrap

module.exports = MockXMLHttpRequest


/***/ })
/******/ ])
});
Expand Down
8 changes: 5 additions & 3 deletions src/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ if (XHR) XHR.Mock = Mock
* Mock.mock( rurl, function(options) )
* Mock.mock( rurl, rtype, template )
* Mock.mock( rurl, rtype, function(options) )
* Mock.mock( rurl, rtype, function(options), config )

根据数据模板生成模拟数据。
*/
Mock.mock = function(rurl, rtype, template) {
Mock.mock = function(rurl, rtype, template, config) {
// Mock.mock(template)
if (arguments.length === 1) {
return Handler.gen(rurl)
Expand All @@ -59,9 +60,10 @@ Mock.mock = function(rurl, rtype, template) {
Mock._mocked[rurl + (rtype || '')] = {
rurl: rurl,
rtype: rtype,
template: template
template: template,
config: config
}
return Mock
}

module.exports = Mock
module.exports = Mock
7 changes: 6 additions & 1 deletion src/mock/xhr/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ Util.extend(MockXMLHttpRequest.prototype, {
}
})

// 拓展this.custom属性, 用于设置responseHeaders等属性
Util.extend(this.custom, MockXMLHttpRequest._settings)

this.custom.timeout = function(timeout) {
if (typeof timeout === 'number') return timeout
if (typeof timeout === 'string' && !~timeout.indexOf('-')) return parseInt(timeout, 10)
Expand Down Expand Up @@ -235,6 +238,8 @@ Util.extend(MockXMLHttpRequest.prototype, {

// 找到了匹配的数据模板,开始拦截 XHR 请求
this.match = true
// 设置单个请求的属性
Util.extend(this.custom, item.config)
this.custom.template = item
this.readyState = MockXMLHttpRequest.OPENED
this.dispatchEvent(new Event('readystatechange' /*, false, false, this*/ ))
Expand Down Expand Up @@ -439,4 +444,4 @@ function convert(item, options) {
item.template(options) : MockXMLHttpRequest.Mock.mock(item.template)
}

module.exports = MockXMLHttpRequest
module.exports = MockXMLHttpRequest