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