~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/openlayers/tests/Request.html

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<html>
 
2
<head>
 
3
    <script src="../lib/OpenLayers.js"></script>
 
4
    <script type="text/javascript">
 
5
    function setup() {
 
6
        window._xhr = OpenLayers.Request.XMLHttpRequest;
 
7
        var anon = new Function();
 
8
        OpenLayers.Request.XMLHttpRequest = function() {};
 
9
        OpenLayers.Request.XMLHttpRequest.prototype = {
 
10
            open: anon,
 
11
            setRequestHeader: anon,
 
12
            send: anon
 
13
        };
 
14
        OpenLayers.Request.XMLHttpRequest.DONE = 4;
 
15
    }
 
16
    function teardown() {
 
17
        OpenLayers.Request.XMLHttpRequest = window._xhr;
 
18
    }
 
19
    
 
20
    function test_issue(t) {
 
21
        setup();
 
22
 
 
23
        t.plan(22);
 
24
        var request, config;
 
25
        var proto = OpenLayers.Request.XMLHttpRequest.prototype;
 
26
        var issue = OpenLayers.Function.bind(OpenLayers.Request.issue,
 
27
                                             OpenLayers.Request);
 
28
 
 
29
        // test that issue returns a new XMLHttpRequest - 1 test
 
30
        request = issue();
 
31
        t.ok(request instanceof OpenLayers.Request.XMLHttpRequest,
 
32
             "returns an XMLHttpRequest instance");
 
33
        
 
34
        // test that issue calls xhr.open with correct args from config - 5 tests
 
35
        var _open = proto.open;
 
36
        config = {
 
37
            method: "foo",
 
38
            url: "http://nowhere",
 
39
            async: "bar",
 
40
            user: "uncle",
 
41
            password: "sam"
 
42
        };
 
43
        proto.open = function(method, url, async, user, password) {
 
44
            t.eq(method, config.method, "open called with correct method");
 
45
            t.eq(url, config.url, "open called with correct url");
 
46
            t.eq(async, config.async, "open called with correct async");
 
47
            t.eq(user, config.user, "open called with correct user");
 
48
            t.eq(password, config.password, "open called with correct password");
 
49
        };
 
50
        request = issue(config);
 
51
        
 
52
        // test that params are serialized as query string - 1 test
 
53
        config = {
 
54
            method: "GET",
 
55
            url: "http://example.com/",
 
56
            params: {"foo": "bar"}
 
57
        };
 
58
        proto.open = function(method, url, async, user, password) {
 
59
            t.eq(url, config.url + "?foo=bar", "params serialized as query string");
 
60
        };
 
61
        request = issue(config);
 
62
        
 
63
        // test that empty params object doesn't produce query string - 1 test
 
64
        config = {
 
65
            method: "GET",
 
66
            url: "http://example.com/",
 
67
            params: {}
 
68
        };
 
69
        proto.open = function(method, url, async, user, password) {
 
70
            t.eq(url, config.url, "empty params doesn't produce query string");
 
71
        }
 
72
        request = issue(config);
 
73
        
 
74
        // test that query string doesn't get two ? separators
 
75
        config = {
 
76
            method: "GET",
 
77
            url: "http://example.com/?existing=query",
 
78
            params: {"foo": "bar"}
 
79
        };
 
80
        proto.open = function(method, url, async, user, password) {
 
81
            t.eq(url, config.url + "&foo=bar", "existing query string gets extended with &");
 
82
        }
 
83
        request = issue(config);
 
84
 
 
85
        // reset open method
 
86
        proto.open = _open;
 
87
        
 
88
        // test that headers are correctly set - 4 tests
 
89
        var _setRequestHeader = proto.setRequestHeader;
 
90
        config = {
 
91
            headers: {
 
92
                foo: "bar",
 
93
                chicken: "soup"
 
94
            }
 
95
        };
 
96
        proto.setRequestHeader = function(key, value) {
 
97
            t.ok(key in config.headers, "setRequestHeader called with key: " + key);
 
98
            t.eq(value, config.headers[key], "setRequestHeader called with correct value: " + value);
 
99
        }
 
100
        request = issue(config);
 
101
        proto.setRequestHeader = _setRequestHeader;
 
102
        
 
103
        // test that callback is called (no scope) - 1 test
 
104
        var unbound = function(request) {
 
105
            t.ok(request instanceof OpenLayers.Request.XMLHttpRequest,
 
106
                 "unbound callback called with xhr instance");
 
107
        }
 
108
        config = {
 
109
            callback: unbound
 
110
        };
 
111
        request = issue(config);
 
112
        request.readyState = OpenLayers.Request.XMLHttpRequest.DONE;
 
113
        request.onreadystatechange();
 
114
 
 
115
        // test that callback is called (with scope) - 2 tests
 
116
        var obj = {};
 
117
        var bound = function(request) {
 
118
            t.ok(this === obj, "bound callback has correct scope");
 
119
            t.ok(request instanceof OpenLayers.Request.XMLHttpRequest,
 
120
                 "bound callback called with xhr instance");
 
121
        }
 
122
        config = {
 
123
            callback: bound,
 
124
            scope: obj
 
125
        };
 
126
        request = issue(config);
 
127
        request.readyState = 4;
 
128
        request.onreadystatechange();
 
129
 
 
130
        // test that send is called with data - 1 test
 
131
        var _send = proto.send;
 
132
        config = {
 
133
            method: "PUT",
 
134
            data: "bling"
 
135
        };
 
136
        proto.send = function(data) {
 
137
            t.eq(data, config.data, "send called with correct data");
 
138
        }
 
139
        request = issue(config);
 
140
        proto.send = _send;
 
141
        
 
142
        // test that optional success callback is only called with 200s and
 
143
        // failure is only called with non-200s
 
144
        var _send = proto.send;
 
145
        proto.send = function() {};
 
146
        
 
147
        config = {
 
148
            success: function(req) {
 
149
                t.ok(!req.status || (req.status >= 200 && req.status < 300),
 
150
                     "success callback called with " + req.status + " status");
 
151
            },
 
152
            failure: function(req) {
 
153
                t.ok(req.status && (req.status < 200 || req.status >= 300),
 
154
                     "failure callback called with " + req.status + " status");
 
155
            }
 
156
        };
 
157
        request = issue(config);
 
158
        request.readyState = 4;
 
159
        
 
160
        // mock up status 200 (1 test)
 
161
        request.status = 200;
 
162
        request.onreadystatechange();
 
163
        
 
164
        // mock up status 299 (1 test)
 
165
        request.status = 299;
 
166
        request.onreadystatechange();
 
167
        
 
168
        // mock up status 100 (1 test)
 
169
        request.status = 100;
 
170
        request.onreadystatechange();
 
171
 
 
172
        // mock up status 300 (1 test)
 
173
        request.status = 300;
 
174
        request.onreadystatechange();
 
175
        
 
176
        // mock up a status null (1 test)
 
177
        request.status = null;
 
178
        request.onreadystatechange();
 
179
 
 
180
        proto.send = _send;
 
181
 
 
182
        teardown();
 
183
    }
 
184
    
 
185
    function test_GET(t) {
 
186
        t.plan(1);
 
187
        var _issue = OpenLayers.Request.issue;
 
188
        OpenLayers.Request.issue = function(config) {
 
189
            t.eq(config.method, "GET", "calls issue with correct method");
 
190
        }
 
191
        OpenLayers.Request.GET();
 
192
        OpenLayers.Request.issue = _issue;
 
193
    }
 
194
    function test_POST(t) {
 
195
        t.plan(1);
 
196
        var _issue = OpenLayers.Request.issue;
 
197
        OpenLayers.Request.issue = function(config) {
 
198
            t.eq(config.method, "POST", "calls issue with correct method");
 
199
        }
 
200
        OpenLayers.Request.POST();
 
201
        OpenLayers.Request.issue = _issue;
 
202
    }
 
203
    function test_PUT(t) {
 
204
        t.plan(1);
 
205
        var _issue = OpenLayers.Request.issue;
 
206
        OpenLayers.Request.issue = function(config) {
 
207
            t.eq(config.method, "PUT", "calls issue with correct method");
 
208
        }
 
209
        OpenLayers.Request.PUT();
 
210
        OpenLayers.Request.issue = _issue;
 
211
    }
 
212
    function test_DELETE(t) {
 
213
        t.plan(1);
 
214
        var _issue = OpenLayers.Request.issue;
 
215
        OpenLayers.Request.issue = function(config) {
 
216
            t.eq(config.method, "DELETE", "calls issue with correct method");
 
217
        }
 
218
        OpenLayers.Request.DELETE();
 
219
        OpenLayers.Request.issue = _issue;
 
220
    }
 
221
    function test_HEAD(t) {
 
222
        t.plan(1);
 
223
        var _issue = OpenLayers.Request.issue;
 
224
        OpenLayers.Request.issue = function(config) {
 
225
            t.eq(config.method, "HEAD", "calls issue with correct method");
 
226
        }
 
227
        OpenLayers.Request.HEAD();
 
228
        OpenLayers.Request.issue = _issue;
 
229
    }
 
230
    function test_OPTIONS(t) {
 
231
        t.plan(1);
 
232
        var _issue = OpenLayers.Request.issue;
 
233
        OpenLayers.Request.issue = function(config) {
 
234
            t.eq(config.method, "OPTIONS", "calls issue with correct method");
 
235
        }
 
236
        OpenLayers.Request.OPTIONS();
 
237
        OpenLayers.Request.issue = _issue;
 
238
    }
 
239
    </script>
 
240
</head>
 
241
<body>
 
242
</body>
 
243
</html>