OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /** | |
6 * @fileoverview | |
7 * @suppress {checkTypes|checkVars|reportUnknownTypes} | |
8 */ | |
9 | |
10 (function() { | |
11 | |
12 'use strict'; | |
13 | |
14 module('xhr', { | |
15 setup: function() { | |
16 }, | |
17 teardown: function() { | |
18 } | |
19 }); | |
20 | |
21 test('urlencodeParamHash', function() { | |
22 QUnit.equal( | |
23 remoting.xhr.urlencodeParamHash({}), | |
24 ''); | |
25 QUnit.equal( | |
26 remoting.xhr.urlencodeParamHash({'key': 'value'}), | |
27 'key=value'); | |
28 QUnit.equal( | |
29 remoting.xhr.urlencodeParamHash({'key /?=&': 'value /?=&'}), | |
30 'key%20%2F%3F%3D%26=value%20%2F%3F%3D%26'); | |
31 QUnit.equal( | |
32 remoting.xhr.urlencodeParamHash({'k1': 'v1', 'k2': 'v2'}), | |
33 'k1=v1&k2=v2'); | |
34 }); | |
35 | |
36 asyncTest('basic GET', function() { | |
37 sinon.useFakeXMLHttpRequest(); | |
38 var request = remoting.xhr.start({ | |
39 method: 'GET', | |
40 url: 'http://foo.com', | |
41 onDone: function(xhr) { | |
42 QUnit.ok(xhr === request); | |
43 QUnit.equal(xhr.status, 200); | |
44 QUnit.equal(xhr.responseText, 'body'); | |
45 QUnit.start(); | |
46 } | |
47 }); | |
48 QUnit.equal(request.method, 'GET'); | |
49 QUnit.equal(request.url, 'http://foo.com'); | |
50 QUnit.equal(request.withCredentials, false); | |
51 QUnit.equal(request.requestBody, null); | |
52 QUnit.ok(!('Content-type' in request.requestHeaders)); | |
53 request.respond(200, {}, 'body'); | |
54 }); | |
55 | |
56 asyncTest('GET with param string', function() { | |
57 sinon.useFakeXMLHttpRequest(); | |
58 var request = remoting.xhr.start({ | |
59 method: 'GET', | |
60 url: 'http://foo.com', | |
61 onDone: function(xhr) { | |
62 QUnit.ok(xhr === request); | |
63 QUnit.equal(xhr.status, 200); | |
64 QUnit.equal(xhr.responseText, 'body'); | |
65 QUnit.start(); | |
66 }, | |
67 urlParams: 'the_param_string' | |
68 }); | |
69 QUnit.equal(request.method, 'GET'); | |
70 QUnit.equal(request.url, 'http://foo.com?the_param_string'); | |
71 QUnit.equal(request.withCredentials, false); | |
72 QUnit.equal(request.requestBody, null); | |
73 QUnit.ok(!('Content-type' in request.requestHeaders)); | |
74 request.respond(200, {}, 'body'); | |
75 }); | |
76 | |
77 asyncTest('GET with param object', function() { | |
78 sinon.useFakeXMLHttpRequest(); | |
79 var request = remoting.xhr.start({ | |
80 method: 'GET', | |
81 url: 'http://foo.com', | |
82 onDone: function(xhr) { | |
83 QUnit.ok(xhr === request); | |
84 QUnit.equal(xhr.status, 200); | |
85 QUnit.equal(xhr.responseText, 'body'); | |
86 QUnit.start(); | |
87 }, | |
88 urlParams: {'a': 'b', 'c': 'd'} | |
89 }); | |
90 QUnit.equal(request.method, 'GET'); | |
91 QUnit.equal(request.url, 'http://foo.com?a=b&c=d'); | |
92 QUnit.equal(request.withCredentials, false); | |
93 QUnit.equal(request.requestBody, null); | |
94 QUnit.ok(!('Content-type' in request.requestHeaders)); | |
95 request.respond(200, {}, 'body'); | |
96 }); | |
97 | |
98 asyncTest('GET with headers', function() { | |
99 sinon.useFakeXMLHttpRequest(); | |
100 var request = remoting.xhr.start({ | |
101 method: 'GET', | |
102 url: 'http://foo.com', | |
103 onDone: function(xhr) { | |
104 QUnit.ok(xhr === request); | |
105 QUnit.equal(xhr.status, 200); | |
106 QUnit.equal(xhr.responseText, 'body'); | |
107 QUnit.start(); | |
108 }, | |
109 headers: {'Header1': 'headerValue1', 'Header2': 'headerValue2'} | |
110 }); | |
111 QUnit.equal(request.method, 'GET'); | |
112 QUnit.equal(request.url, 'http://foo.com'); | |
113 QUnit.equal(request.withCredentials, false); | |
114 QUnit.equal(request.requestBody, null); | |
115 QUnit.equal( | |
116 request.requestHeaders['Header1'], | |
117 'headerValue1'); | |
118 QUnit.equal( | |
119 request.requestHeaders['Header2'], | |
120 'headerValue2'); | |
121 QUnit.ok(!('Content-type' in request.requestHeaders)); | |
122 request.respond(200, {}, 'body'); | |
123 }); | |
124 | |
125 | |
126 asyncTest('GET with credentials', function() { | |
127 sinon.useFakeXMLHttpRequest(); | |
128 var request = remoting.xhr.start({ | |
129 method: 'GET', | |
130 url: 'http://foo.com', | |
131 onDone: function(xhr) { | |
132 QUnit.ok(xhr === request); | |
133 QUnit.equal(xhr.status, 200); | |
134 QUnit.equal(xhr.responseText, 'body'); | |
135 QUnit.start(); | |
136 }, | |
137 withCredentials: true | |
138 }); | |
139 QUnit.equal(request.method, 'GET'); | |
140 QUnit.equal(request.url, 'http://foo.com'); | |
141 QUnit.equal(request.withCredentials, true); | |
142 QUnit.equal(request.requestBody, null); | |
143 QUnit.ok(!('Content-type' in request.requestHeaders)); | |
144 request.respond(200, {}, 'body'); | |
145 }); | |
146 | |
147 asyncTest('POST with text content', function() { | |
148 sinon.useFakeXMLHttpRequest(); | |
149 var request = remoting.xhr.start({ | |
150 method: 'POST', | |
151 url: 'http://foo.com', | |
152 onDone: function(xhr) { | |
153 QUnit.ok(xhr === request); | |
154 QUnit.equal(xhr.status, 200); | |
155 QUnit.equal(xhr.responseText, 'body'); | |
156 QUnit.start(); | |
157 }, | |
158 textContent: 'the_content_string' | |
159 }); | |
160 QUnit.equal(request.method, 'POST'); | |
161 QUnit.equal(request.url, 'http://foo.com'); | |
162 QUnit.equal(request.withCredentials, false); | |
163 QUnit.equal(request.requestBody, 'the_content_string'); | |
164 QUnit.ok(!('Content-type' in request.requestHeaders)); | |
165 request.respond(200, {}, 'body'); | |
166 }); | |
167 | |
168 asyncTest('POST with form content', function() { | |
169 sinon.useFakeXMLHttpRequest(); | |
170 var request = remoting.xhr.start({ | |
171 method: 'POST', | |
172 url: 'http://foo.com', | |
173 onDone: function(xhr) { | |
174 QUnit.ok(xhr === request); | |
175 QUnit.equal(xhr.status, 200); | |
176 QUnit.equal(xhr.responseText, 'body'); | |
177 QUnit.start(); | |
178 }, | |
179 formContent: {'a': 'b', 'c': 'd'} | |
180 }); | |
181 QUnit.equal(request.method, 'POST'); | |
182 QUnit.equal(request.url, 'http://foo.com'); | |
183 QUnit.equal(request.withCredentials, false); | |
184 QUnit.equal(request.requestBody, 'a=b&c=d'); | |
185 QUnit.equal( | |
186 request.requestHeaders['Content-type'], | |
187 'application/x-www-form-urlencoded'); | |
188 request.respond(200, {}, 'body'); | |
189 }); | |
190 | |
191 asyncTest('defaultResponse 200', function() { | |
192 sinon.useFakeXMLHttpRequest(); | |
193 | |
194 var onDone = function() { | |
195 QUnit.ok(true); | |
196 QUnit.start(); | |
197 }; | |
198 | |
199 var onError = function(error) { | |
200 QUnit.ok(false); | |
201 QUnit.start(); | |
202 }; | |
203 | |
204 var request = remoting.xhr.start({ | |
205 method: 'POST', | |
206 url: 'http://foo.com', | |
207 onDone: remoting.xhr.defaultResponse(onDone, onError) | |
208 }); | |
209 request.respond(200); | |
210 }); | |
211 | |
212 | |
213 asyncTest('defaultResponse 404', function() { | |
214 sinon.useFakeXMLHttpRequest(); | |
215 | |
216 var onDone = function() { | |
217 QUnit.ok(false); | |
218 QUnit.start(); | |
219 }; | |
220 | |
221 var onError = function(error) { | |
222 QUnit.ok(true); | |
223 QUnit.start(); | |
224 }; | |
225 | |
226 var request = remoting.xhr.start({ | |
227 method: 'POST', | |
228 url: 'http://foo.com', | |
229 onDone: remoting.xhr.defaultResponse(onDone, onError) | |
230 }); | |
231 request.respond(404); | |
232 }); | |
233 | |
234 })(); | |
OLD | NEW |