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