forked from jeresig/env-js
-
Notifications
You must be signed in to change notification settings - Fork 76
/
spec.js
163 lines (135 loc) · 4.61 KB
/
spec.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* @todo: document
*/
var _load,
_start,
_count = 1,
_starttime = new Date().getTime(),
_endtime;
try{
_load = load;
_load('test/specs/qunit.js');
_start = QUnit.start;
}catch(e){
_load = _start = function(){};
}
QUnit.log = function(result, message){
if(console)console.log('(' + (_count++) + ')[' +
((!!result) ? 'PASS' : 'FAIL') + '] ' + message);
};
QUnit.done = function( fail, pass){
if(console){
_endtime = new Date().getTime();
console.log('\n\tRESULTS: ( of '+(pass+fail)+' total tests )');
console.log('\t\tPASSED: ' +pass);
console.log('\t\tFAILED: ' +fail);
console.log('\tCompleted in '+(_endtime-_starttime)+' milliseconds.\n');
}
};
QUnit.init();
_load('dist/platform/core.js');
_load('dist/platform/rhino.js');
_load('dist/console.js');
_load('dist/dom.js');
_load('dist/event.js');
_load('dist/html.js');
_load('dist/timer.js');
_load('dist/parser.js');
_load('dist/xhr.js');
try{
_load('local_settings.js');
}catch(e){
_load('settings.js');
}
module('xhr');
test('XMLHttpRequest Interfaces Available', function(){
expect(2);
ok(Location, 'Location defined');
ok(XMLHttpRequest, 'XMLHttpRequest defined');
});
// mock the global document object if not available
var expected_path = 'test/specs/xhr/spec.html';
try{
document;
}catch(e){
console.log('mocking global document object.');
document = new HTMLDocument(new DOMImplementation());
document.async = false;
console.log('mocking global document location.');
location = new Location(Envjs.location(expected_path, SETTINGS.AJAX_BASE), document);
document.baseURI = location.href;
location.reload();
}
test('Location', function(){
var href = SETTINGS.AJAX_BASE+expected_path;
equals(location.toString(), href, '.toString()');
equals(location.hash, '', '.hash');
equals(location.host, 'localhost:8080', '.host');
equals(location.hostname, 'localhost', '.hostname');
equals(location.href, href, '.href');
equals(location.pathname, '/env-js/'+expected_path, '.pathname');
equals(location.port, '8080', '.port');
equals(location.protocol, 'http:', '.protocol');
equals(location.search, '', '.search');
});
test('XMLHttpRequest sync', function(){
var xhr,
url;
url = SETTINGS.AJAX_BASE +'test/fixtures/simple.txt';
xhr = new XMLHttpRequest();
equals(xhr.readyState, 0, '.readyState');
xhr.open("GET", url, false);
equals(xhr.readyState, 1, '.readyState');
xhr.send();
equals(xhr.readyState, 4, '.readyState');
equals(xhr.responseText, 'Hello World', '.responseText');
equals(xhr.responseXML, null, '.responseXML');
equals(xhr.status, 200, '.status');
equals(xhr.statusText, 'OK', '.statusText');
});
test('XMLHttpRequest async', function(){
var xhr,
url;
url = SETTINGS.AJAX_BASE +'test/fixtures/simple.txt';
xhr = new XMLHttpRequest();
equals(xhr.readyState, 0, '.readyState');
equals(xhr.responseText, '', '.responseText');
equals(xhr.responseXML, null, '.responseXML');
equals(xhr.status, 0, '.status');
equals(xhr.statusText, '', '.statusText');
xhr.open("GET", url, true);
equals(xhr.readyState, 1, '.readyState');
xhr.onreadystatechange = function(){
if(xhr.readyState === 1){
equals(xhr.responseText, '', '.responseText');
equals(xhr.responseXML, null, '.responseXML');
equals(xhr.status, 200, '.status');
equals(xhr.statusText, 'OK', '.statusText');
}else if(xhr.readyState === 2){
equals(xhr.responseXML, null, '.responseXML');
equals(xhr.status, 200, '.status');
equals(xhr.statusText, 'OK', '.statusText');
}else if(xhr.readyState === 3){
equals(xhr.responseText, 'Hello World', '.responseText');
equals(xhr.responseXML, null, '.responseXML');
equals(xhr.status, 200, '.status');
equals(xhr.statusText, 'OK', '.statusText');
}else if(xhr.readyState === 4){
equals(xhr.readyState, 4, '.readyState');
equals(xhr.responseText, 'Hello World', '.responseText');
equals(xhr.responseXML, null, '.responseXML');
equals(xhr.status, 200, '.status');
equals(xhr.statusText, 'OK', '.statusText');
_start();
Envjs.wait();
}else {
ok(false, 'xhr failed');
_start();
Envjs.wait();
}
};
xhr.send();
stop();
});
_start();
Envjs.wait();