Skip to content

Commit

Permalink
fix #95
Browse files Browse the repository at this point in the history
  • Loading branch information
nuysoft committed Feb 2, 2016
1 parent 56bd67d commit 5f4d246
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 10 deletions.
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.

13 changes: 10 additions & 3 deletions dist/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ return /******/ (function(modules) { // webpackBootstrap
_mocked: {}
}

Mock.version = '1.0.1-beta1'
Mock.version = '1.0.1-beta2'

// 避免循环依赖
if (XHR) XHR.Mock = Mock
Expand Down Expand Up @@ -8089,7 +8089,7 @@ return /******/ (function(modules) { // webpackBootstrap
}

var XHR_EVENTS = 'readystatechange loadstart progress abort error load timeout loadend'.split(' ')

var XHR_REQUEST_PROPERTIES = 'timeout withCredentials'.split(' ')
var XHR_RESPONSE_PROPERTIES = 'readyState responseURL status statusText responseType response responseText responseXML'.split(' ')

// https://github.com/trek/FakeXMLHttpRequest/blob/master/fake_xml_http_request.js#L32
Expand Down Expand Up @@ -8207,7 +8207,7 @@ return /******/ (function(modules) { // webpackBootstrap

function handle(event) {
// 同步属性 NativeXMLHttpRequest => MockXMLHttpRequest
for (var i = 0, len = XHR_RESPONSE_PROPERTIES.length; i < len; i++) {
for (var i = 0; i < XHR_RESPONSE_PROPERTIES.length; i++) {
try {
that[XHR_RESPONSE_PROPERTIES[i]] = xhr[XHR_RESPONSE_PROPERTIES[i]]
} catch (e) {}
Expand All @@ -8231,6 +8231,13 @@ return /******/ (function(modules) { // webpackBootstrap
if (username) xhr.open(method, url, async, username, password)
else xhr.open(method, url, async)

// 同步属性 MockXMLHttpRequest => NativeXMLHttpRequest
for (var j = 0; j < XHR_REQUEST_PROPERTIES.length; j++) {
try {
xhr[XHR_REQUEST_PROPERTIES[j]] = that[XHR_REQUEST_PROPERTIES[j]]
} catch (e) {}
}

return
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "mockjs",
"title": "Mock.js",
"description": "生成随机数据 & 拦截 Ajax 请求",
"version": "1.0.1-beta1",
"version": "1.0.1-beta2",
"homepage": "http://mockjs.com/",
"keywords": [
"mock",
Expand Down
2 changes: 1 addition & 1 deletion src/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var Mock = {
_mocked: {}
}

Mock.version = '1.0.1-beta1'
Mock.version = '1.0.1-beta2'

// 避免循环依赖
if (XHR) XHR.Mock = Mock
Expand Down
11 changes: 9 additions & 2 deletions src/mock/xhr/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ var XHR_STATES = {
}

var XHR_EVENTS = 'readystatechange loadstart progress abort error load timeout loadend'.split(' ')

var XHR_REQUEST_PROPERTIES = 'timeout withCredentials'.split(' ')
var XHR_RESPONSE_PROPERTIES = 'readyState responseURL status statusText responseType response responseText responseXML'.split(' ')

// https://github.com/trek/FakeXMLHttpRequest/blob/master/fake_xml_http_request.js#L32
Expand Down Expand Up @@ -199,7 +199,7 @@ Util.extend(MockXMLHttpRequest.prototype, {

function handle(event) {
// 同步属性 NativeXMLHttpRequest => MockXMLHttpRequest
for (var i = 0, len = XHR_RESPONSE_PROPERTIES.length; i < len; i++) {
for (var i = 0; i < XHR_RESPONSE_PROPERTIES.length; i++) {
try {
that[XHR_RESPONSE_PROPERTIES[i]] = xhr[XHR_RESPONSE_PROPERTIES[i]]
} catch (e) {}
Expand All @@ -223,6 +223,13 @@ Util.extend(MockXMLHttpRequest.prototype, {
if (username) xhr.open(method, url, async, username, password)
else xhr.open(method, url, async)

// 同步属性 MockXMLHttpRequest => NativeXMLHttpRequest
for (var j = 0; j < XHR_REQUEST_PROPERTIES.length; j++) {
try {
xhr[XHR_REQUEST_PROPERTIES[j]] = that[XHR_REQUEST_PROPERTIES[j]]
} catch (e) {}
}

return
}

Expand Down
29 changes: 29 additions & 0 deletions test/test.mock.request.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,35 @@ describe('Request', function() {
})
})
})
describe('jQuery.ajax() XHR Fields', function() {
it('', function(done) {
var that = this
var url = Math.random()
var xhr
$.ajax({
xhr: function() {
xhr = $.ajaxSettings.xhr()
return xhr
},
url: url,
dataType: 'json',
xhrFields: {
timeout: 123,
withCredentials: true
}
}).done(function( /*data, textStatus, jqXHR*/ ) {
// 不会进入
}).fail(function(jqXHR /*, textStatus, errorThrown*/ ) {
// 浏览器 || PhantomJS
expect([404, 0]).to.include(jqXHR.status)
that.test.title += url + ' => ' + jqXHR.status
expect(xhr.timeout).to.be.equal(123)
expect(xhr.withCredentials).to.be.equal(true)
}).always(function() {
done()
})
})
})

describe('Mock.mock( rurl, template )', function() {
it('', function(done) {
Expand Down

0 comments on commit 5f4d246

Please sign in to comment.