forked from jaubourg/jquery-jsonp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit.js
More file actions
123 lines (112 loc) · 2.56 KB
/
unit.js
File metadata and controls
123 lines (112 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
module( "main", { teardown: moduleTeardown } );
var hasDeferred = !!$.Deferred;
function testJSONP( name, outcome, options ) {
test( name, function() {
stop();
expect( ( options.expect || 0 ) + 1 + hasDeferred );
var xOptions = $.jsonp( $.extend( {}, options, {
success: function() {
ok( outcome === "success", "Success" );
},
error: function() {
ok( outcome === "error", "Error" );
},
complete: function() {
if ( options.complete ) {
options.complete.call( this );
}
start();
}
}) );
if ( hasDeferred ) {
xOptions.done(function() {
ok( outcome === "success", "Done" );
}).fail(function() {
ok( outcome === "error", "Fail" );
});
}
});
}
testJSONP( "success", "success", {
url: "http://gdata.youtube.com/feeds/api/users/julianaubourg?callback=?",
data: {
alt: "json-in-script"
}
});
testJSONP( "error (HTTP Code)", "error", {
url: "http://gdata.youtube.com/feeds/api/users/hgfusyggbvbdbrfvurgbirhtiytjrhjsrhk66?callback=?",
data: {
alt: "json-in-script"
}
});
if ( !window.opera || window.opera.version() < 11.60 ) {
testJSONP( "error (Syntax Error)", "error", {
expect: window.opera ? 0 : 1,
url: "data/syntax-error.js",
cache: true,
beforeSend: function() {
this.oldOnError = window.onerror;
window.onerror = function() {
ok( true, "Syntax Error Thrown" );
};
},
complete: function() {
window.onerror = this.oldOnError;
}
});
}
if ( !window.opera ) {
test( "error (Exception)", function() {
stop();
expect( 2 );
$.jsonp({
url: "http://gdata.youtube.com/feeds/api/users/julianaubourg?callback=?",
beforeSend: function() {
var oldOnError = window.onerror;
window.onerror = function( flag ) {
ok( flag !== undefined, "Exception Thrown" );
window.onerror = oldOnError;
start();
};
},
success: function() {
ok( true, "success" );
throw "an exception";
},
complete: function() {
window.onerror();
}
});
});
}
testJSONP( "error (callback not called)", "error", {
expect: 1,
url: "data/no-callback.js",
cache: true,
complete: function() {
strictEqual( window.bob, 33, "script was executed" );
window.bob = false;
}
});
test( "stress test", function() {
var num = 20,
count = num;
expect( num );
stop();
for ( ; num ; num-- ) {
$.jsonp({
url: "http://gdata.youtube.com/feeds/api/users/julianaubourg?callback=?",
success: function() {
ok( true, "success" );
},
error: function() {
ok( false, "error" );
},
complete: function() {
if ( !( --count ) ) {
start();
}
}
});
}
});