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

[Testcase] 修改request 源码、并增加对应的测试用例 #325

Open
wants to merge 1 commit into
base: dev
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/baidu/ajax/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ baidu.ajax.request = function (url, opt_options) {
var stat = xhr.status;
} catch (ex) {
// 在请求时,如果网络中断,Firefox会无法取得status
fire('failure');
fire('failure',ex.message);
return;
}

Expand All @@ -77,7 +77,7 @@ baidu.ajax.request = function (url, opt_options) {
|| stat == 1223) {
fire('success');
} else {
fire('failure');
fire('failure',stat);
}

/*
Expand Down Expand Up @@ -136,8 +136,9 @@ baidu.ajax.request = function (url, opt_options) {
*
* @ignore
* @param {String} type 事件类型
* @param {String} msg 提示信息
*/
function fire(type) {
function fire(type,msg) {
type = 'on' + type;
var handler = eventHandlers[type],
globelHandler = baidu.ajax[type];
Expand All @@ -149,7 +150,7 @@ baidu.ajax.request = function (url, opt_options) {
}

if (type != 'onsuccess') {
handler(xhr);
handler(xhr,msg);
} else {
//处理获取xhr.responseText导致出错的情况,比如请求图片地址.
try {
Expand Down Expand Up @@ -229,7 +230,7 @@ baidu.ajax.request = function (url, opt_options) {
stateChangeHandler();
}
} catch (ex) {
fire('failure');
fire('failure',ex.message);
}

return xhr;
Expand Down
12 changes: 12 additions & 0 deletions test/baidu/ajax/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,15 @@ test("ontimeout", function() {
}
});
});

var test404flag = false;
baidu.ajax.request('text.php', {
onfailure : function(xhr, msg) {
if(msg=='404'){
test404flag = true;
};
}
});
test("请求失败获得错误信息提示", function() {
ok(test404flag,'返回信息是404');
});