Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / remoting / webapp / unittests / xhr_unittest.js
blob804f71594d5ea52662c6fc7e81ccf5c03397d086
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.
5 (function() {
7 'use strict';
9 module('xhr', {
10 setup: function() {
12 teardown: function() {
14 });
16 test('urlencodeParamHash', function() {
17 QUnit.equal(
18 remoting.xhr.urlencodeParamHash({}),
19 '');
20 QUnit.equal(
21 remoting.xhr.urlencodeParamHash({'key': 'value'}),
22 'key=value');
23 QUnit.equal(
24 remoting.xhr.urlencodeParamHash({'key /?=&': 'value /?=&'}),
25 'key%20%2F%3F%3D%26=value%20%2F%3F%3D%26');
26 QUnit.equal(
27 remoting.xhr.urlencodeParamHash({'k1': 'v1', 'k2': 'v2'}),
28 'k1=v1&k2=v2');
29 });
31 asyncTest('basic GET', function() {
32 sinon.useFakeXMLHttpRequest();
33 var request = remoting.xhr.start({
34 method: 'GET',
35 url: 'http://foo.com',
36 onDone: function(xhr) {
37 QUnit.ok(xhr === request);
38 QUnit.equal(xhr.status, 200);
39 QUnit.equal(xhr.responseText, 'body');
40 QUnit.start();
42 });
43 QUnit.equal(request.method, 'GET');
44 QUnit.equal(request.url, 'http://foo.com');
45 QUnit.equal(request.withCredentials, false);
46 QUnit.equal(request.requestBody, null);
47 QUnit.ok(!('Content-type' in request.requestHeaders));
48 request.respond(200, {}, 'body');
49 });
51 asyncTest('GET with param string', function() {
52 sinon.useFakeXMLHttpRequest();
53 var request = remoting.xhr.start({
54 method: 'GET',
55 url: 'http://foo.com',
56 onDone: function(xhr) {
57 QUnit.ok(xhr === request);
58 QUnit.equal(xhr.status, 200);
59 QUnit.equal(xhr.responseText, 'body');
60 QUnit.start();
62 urlParams: 'the_param_string'
63 });
64 QUnit.equal(request.method, 'GET');
65 QUnit.equal(request.url, 'http://foo.com?the_param_string');
66 QUnit.equal(request.withCredentials, false);
67 QUnit.equal(request.requestBody, null);
68 QUnit.ok(!('Content-type' in request.requestHeaders));
69 request.respond(200, {}, 'body');
70 });
72 asyncTest('GET with param object', function() {
73 sinon.useFakeXMLHttpRequest();
74 var request = remoting.xhr.start({
75 method: 'GET',
76 url: 'http://foo.com',
77 onDone: function(xhr) {
78 QUnit.ok(xhr === request);
79 QUnit.equal(xhr.status, 200);
80 QUnit.equal(xhr.responseText, 'body');
81 QUnit.start();
83 urlParams: {'a': 'b', 'c': 'd'}
84 });
85 QUnit.equal(request.method, 'GET');
86 QUnit.equal(request.url, 'http://foo.com?a=b&c=d');
87 QUnit.equal(request.withCredentials, false);
88 QUnit.equal(request.requestBody, null);
89 QUnit.ok(!('Content-type' in request.requestHeaders));
90 request.respond(200, {}, 'body');
91 });
93 asyncTest('GET with headers', function() {
94 sinon.useFakeXMLHttpRequest();
95 var request = remoting.xhr.start({
96 method: 'GET',
97 url: 'http://foo.com',
98 onDone: function(xhr) {
99 QUnit.ok(xhr === request);
100 QUnit.equal(xhr.status, 200);
101 QUnit.equal(xhr.responseText, 'body');
102 QUnit.start();
104 headers: {'Header1': 'headerValue1', 'Header2': 'headerValue2'}
106 QUnit.equal(request.method, 'GET');
107 QUnit.equal(request.url, 'http://foo.com');
108 QUnit.equal(request.withCredentials, false);
109 QUnit.equal(request.requestBody, null);
110 QUnit.equal(
111 request.requestHeaders['Header1'],
112 'headerValue1');
113 QUnit.equal(
114 request.requestHeaders['Header2'],
115 'headerValue2');
116 QUnit.ok(!('Content-type' in request.requestHeaders));
117 request.respond(200, {}, 'body');
121 asyncTest('GET with credentials', function() {
122 sinon.useFakeXMLHttpRequest();
123 var request = remoting.xhr.start({
124 method: 'GET',
125 url: 'http://foo.com',
126 onDone: function(xhr) {
127 QUnit.ok(xhr === request);
128 QUnit.equal(xhr.status, 200);
129 QUnit.equal(xhr.responseText, 'body');
130 QUnit.start();
132 withCredentials: true
134 QUnit.equal(request.method, 'GET');
135 QUnit.equal(request.url, 'http://foo.com');
136 QUnit.equal(request.withCredentials, true);
137 QUnit.equal(request.requestBody, null);
138 QUnit.ok(!('Content-type' in request.requestHeaders));
139 request.respond(200, {}, 'body');
142 asyncTest('POST with text content', function() {
143 sinon.useFakeXMLHttpRequest();
144 var request = remoting.xhr.start({
145 method: 'POST',
146 url: 'http://foo.com',
147 onDone: function(xhr) {
148 QUnit.ok(xhr === request);
149 QUnit.equal(xhr.status, 200);
150 QUnit.equal(xhr.responseText, 'body');
151 QUnit.start();
153 textContent: 'the_content_string'
155 QUnit.equal(request.method, 'POST');
156 QUnit.equal(request.url, 'http://foo.com');
157 QUnit.equal(request.withCredentials, false);
158 QUnit.equal(request.requestBody, 'the_content_string');
159 QUnit.ok(!('Content-type' in request.requestHeaders));
160 request.respond(200, {}, 'body');
163 asyncTest('POST with form content', function() {
164 sinon.useFakeXMLHttpRequest();
165 var request = remoting.xhr.start({
166 method: 'POST',
167 url: 'http://foo.com',
168 onDone: function(xhr) {
169 QUnit.ok(xhr === request);
170 QUnit.equal(xhr.status, 200);
171 QUnit.equal(xhr.responseText, 'body');
172 QUnit.start();
174 formContent: {'a': 'b', 'c': 'd'}
176 QUnit.equal(request.method, 'POST');
177 QUnit.equal(request.url, 'http://foo.com');
178 QUnit.equal(request.withCredentials, false);
179 QUnit.equal(request.requestBody, 'a=b&c=d');
180 QUnit.equal(
181 request.requestHeaders['Content-type'],
182 'application/x-www-form-urlencoded');
183 request.respond(200, {}, 'body');
186 asyncTest('defaultResponse 200', function() {
187 sinon.useFakeXMLHttpRequest();
189 var onDone = function() {
190 QUnit.ok(true);
191 QUnit.start();
194 var onError = function(error) {
195 QUnit.ok(false);
196 QUnit.start();
199 var request = remoting.xhr.start({
200 method: 'POST',
201 url: 'http://foo.com',
202 onDone: remoting.xhr.defaultResponse(onDone, onError)
204 request.respond(200);
208 asyncTest('defaultResponse 404', function() {
209 sinon.useFakeXMLHttpRequest();
211 var onDone = function() {
212 QUnit.ok(false);
213 QUnit.start();
216 var onError = function(error) {
217 QUnit.ok(true);
218 QUnit.start();
221 var request = remoting.xhr.start({
222 method: 'POST',
223 url: 'http://foo.com',
224 onDone: remoting.xhr.defaultResponse(onDone, onError)
226 request.respond(404);
229 })();