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