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