Skip to content

FIX wrong success for an erronous fetch, after an abort of a legit fetch #22

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

Open
wants to merge 2 commits into
base: master
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
3 changes: 3 additions & 0 deletions src/jquery.jsonp.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@

} else {

// reset lastValue (from stale aborts)
lastValue = void 0;

// Install the generic callback
// (BEWARE: global namespace pollution ahoy)
win[ successCallbackName ] = genericCallback;
Expand Down
113 changes: 113 additions & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,116 @@ test( "stress test", function() {
});
}
});



function findHead(url1, url2) {
function rquote(str) {
return str.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
};
var t, obj,
reg1 = new RegExp(rquote(url1), "g"),
reg2 = url2 ? new RegExp(rquote(url2), "g") : null,
head = $( "head" )[ 0 ] || document.documentElement,
res = [],
children = head.children;
for (t=0;t<children.length; t++) {
obj = children[t];

if (!obj)
continue;
if ( /_jqjsp[0-9]+/.test(obj.id) && (reg1.test(obj.src) || (reg2 && reg2.test(obj.src)))) {
//console.log("FOUND "+t+" "+obj.src + " " + obj.id);
res.push(obj);
}
}
return res;
}


test( "cache", function() {
var url = "http://gdata.youtube.com/feeds/api/users/julianaubourg?_nx=x&callback=?";
urlpat = "http://gdata.youtube.com/feeds/api/users/julianaubourg?_nx=x&";

$.jsonp({
url: url,
cache:false, // do append random
complete: function() {
start();
}
});
var scr = findHead(urlpat);
ok( scr.length === 1);
ok( /_[0-9]+=$/.test(scr[0].src), "cache (browser) on");
stop();


url = "http://gdata.youtube.com/feeds/api/users/julianaubourg?_nx=z&callback=?";
urlpat = "http://gdata.youtube.com/feeds/api/users/julianaubourg?_nx=z&";
$.jsonp({
url: url,
cache:true, // do not append random
complete: function() {
start();
}
});

var scr = findHead(urlpat);
ok( scr.length === 1);
ok( ! /_[0-9]+=$/.test(scr[0].src), "cache (browser) off");

stop();
});


test( "abort", function() {
var url = "http://gdata.youtube.com/feeds/api/users/julianaubourg?_nx=x&callback=?";
urlpat = "http://gdata.youtube.com/feeds/api/users/julianaubourg?_nx=x&",
xOpts = $.jsonp({
url: url,
cache:false, // do append random
complete: function() {
start();
},
error: function(x ,t) {
ok(t === "aborted", "Legit Request aborted");
},
success: function() {
ok(false, "Legit Request aborted"); // should fail
},
});
var scr = findHead(urlpat);
ok( scr.length === 1, "script tag created");
stop();

xOpts.abort();
var scr = findHead(urlpat);
ok( scr.length === 0, "script tag deleted");

// wait for the anyway aborted to be fetched
setTimeout(function(){
start();
// CHECK for fetch 404
url = "http://willx.notBeExisting.ccc";
urlpat = "http://willx.notBeExisting.ccc";

$.jsonp({
url: url,
cache:true, // do not append random
complete: function() {
start();
},
success: function() {
ok(false, "Request failed"); // should fail
},
error: function(x, t) {
ok(true, "Request failed"); // should fail
ok(t === "error", "error type");
}
});
stop();

},2000);

});