OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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. |
| 4 |
| 5 (function() { |
| 6 'use strict'; |
| 7 |
| 8 /** @type {boolean} */ |
| 9 var oldThrowOnAssert; |
| 10 |
| 11 QUnit.module('mock_xhr', { |
| 12 beforeEach: function() { |
| 13 oldThrowOnAssert = base.debug.throwOnAssert; |
| 14 base.debug.throwOnAssert = true; |
| 15 remoting.MockXhr.activate(); |
| 16 }, |
| 17 afterEach: function() { |
| 18 remoting.MockXhr.restore(); |
| 19 base.debug.throwOnAssert = oldThrowOnAssert; |
| 20 } |
| 21 }); |
| 22 |
| 23 QUnit.test('multiple calls to activate() fail', function(assert) { |
| 24 assert.throws(function() { |
| 25 remoting.MockXhr.activate(); |
| 26 }); |
| 27 }); |
| 28 |
| 29 QUnit.test('restore() without activate() fails', function(assert) { |
| 30 remoting.MockXhr.restore(); |
| 31 assert.throws(function() { |
| 32 remoting.MockXhr.restore(); |
| 33 }); |
| 34 remoting.MockXhr.activate(); |
| 35 }); |
| 36 |
| 37 /** |
| 38 * @param {string=} opt_url The URL to request. |
| 39 * @return {!Promise<!remoting.Xhr.Response>} |
| 40 */ |
| 41 function sendRequest(opt_url) { |
| 42 return new remoting.Xhr({ |
| 43 method: 'GET', |
| 44 url: opt_url || 'http://foo.com' |
| 45 }).start(); |
| 46 }; |
| 47 |
| 48 /** |
| 49 * @return {!Promise<!remoting.Xhr.Response>} |
| 50 */ |
| 51 function sendRequestForJson() { |
| 52 return new remoting.Xhr({ |
| 53 method: 'GET', |
| 54 url: 'http://foo.com', |
| 55 acceptJson: true |
| 56 }).start(); |
| 57 } |
| 58 |
| 59 QUnit.test('unhandled requests fail', function(assert) { |
| 60 assert.throws(sendRequest); |
| 61 }); |
| 62 |
| 63 QUnit.test('setEmptyResponseFor', function(assert) { |
| 64 remoting.MockXhr.setEmptyResponseFor('GET', 'http://foo.com'); |
| 65 var promise = sendRequest(); |
| 66 assert.throws(sendRequest); |
| 67 return promise.then(function(/** remoting.Xhr.Response */ result) { |
| 68 assert.equal(result.status, 204); |
| 69 assert.equal(result.getText(), ''); |
| 70 assert.throws(result.getJson.bind(result)); |
| 71 }); |
| 72 }); |
| 73 |
| 74 QUnit.test('setEmptyResponseFor with repeat', function(assert) { |
| 75 remoting.MockXhr.setEmptyResponseFor('GET', 'http://foo.com', true); |
| 76 var promise1 = sendRequest(); |
| 77 var promise2 = sendRequest(); |
| 78 return promise1.then(function(/** remoting.Xhr.Response */ result) { |
| 79 assert.equal(result.status, 204); |
| 80 assert.equal(result.getText(), ''); |
| 81 assert.throws(result.getJson.bind(result)); |
| 82 return promise2; |
| 83 }).then(function(/** remoting.Xhr.Response */ result) { |
| 84 assert.equal(result.status, 204); |
| 85 assert.equal(result.getText(), ''); |
| 86 assert.throws(result.getJson.bind(result)); |
| 87 }); |
| 88 }); |
| 89 |
| 90 QUnit.test('setEmptyResponseFor with RegExp', function(assert) { |
| 91 remoting.MockXhr.setEmptyResponseFor('GET', /foo/); |
| 92 var promise = sendRequest(); |
| 93 assert.throws(sendRequest); |
| 94 return promise.then(function(/** remoting.Xhr.Response */ result) { |
| 95 assert.equal(result.status, 204); |
| 96 assert.equal(result.getText(), ''); |
| 97 assert.throws(result.getJson.bind(result)); |
| 98 }); |
| 99 }); |
| 100 |
| 101 QUnit.test('setTextResponseFor', function(assert) { |
| 102 remoting.MockXhr.setTextResponseFor('GET', /foo/, 'first'); |
| 103 remoting.MockXhr.setTextResponseFor('GET', /foo/, 'second'); |
| 104 var promise1 = sendRequest(); |
| 105 var promise2 = sendRequest(); |
| 106 return promise1.then(function(/** remoting.Xhr.Response */ result) { |
| 107 assert.equal(result.status, 200); |
| 108 assert.equal(result.getText(), 'first'); |
| 109 assert.throws(result.getJson.bind(result)); |
| 110 return promise2; |
| 111 }).then(function(/** remoting.Xhr.Response */ result) { |
| 112 assert.equal(result.status, 200); |
| 113 assert.equal(result.getText(), 'second'); |
| 114 assert.throws(result.getJson.bind(result)); |
| 115 }); |
| 116 }); |
| 117 |
| 118 QUnit.test('setTextResponseFor with different URLs', function(assert) { |
| 119 remoting.MockXhr.setTextResponseFor('GET', /foo/, 'first'); |
| 120 remoting.MockXhr.setTextResponseFor('GET', /bar/, 'second'); |
| 121 var promise1 = sendRequest('http://bar.com'); |
| 122 var promise2 = sendRequest(); |
| 123 return promise1.then(function(/** remoting.Xhr.Response */ result) { |
| 124 assert.equal(result.status, 200); |
| 125 assert.equal(result.getText(), 'second'); |
| 126 assert.throws(result.getJson.bind(result)); |
| 127 return promise2; |
| 128 }).then(function(/** remoting.Xhr.Response */ result) { |
| 129 assert.equal(result.status, 200); |
| 130 assert.equal(result.getText(), 'first'); |
| 131 assert.throws(result.getJson.bind(result)); |
| 132 }); |
| 133 }); |
| 134 |
| 135 |
| 136 QUnit.test('setTextResponseFor with default', function(assert) { |
| 137 remoting.MockXhr.setTextResponseFor('GET', /foo/, 'specific'); |
| 138 remoting.MockXhr.setTextResponseFor('GET', null, 'default', true); |
| 139 var promise1 = sendRequest('http://bar.com'); |
| 140 var promise2 = sendRequest(); |
| 141 var promise3 = sendRequest(); |
| 142 return promise1.then(function(/** remoting.Xhr.Response */ result) { |
| 143 assert.equal(result.status, 200); |
| 144 assert.equal(result.getText(), 'default'); |
| 145 assert.throws(result.getJson.bind(result)); |
| 146 return promise2; |
| 147 }).then(function(/** remoting.Xhr.Response */ result) { |
| 148 assert.equal(result.status, 200); |
| 149 assert.equal(result.getText(), 'specific'); |
| 150 assert.throws(result.getJson.bind(result)); |
| 151 return promise3; |
| 152 }).then(function(/** remoting.Xhr.Response */ result) { |
| 153 assert.equal(result.status, 200); |
| 154 assert.equal(result.getText(), 'default'); |
| 155 assert.throws(result.getJson.bind(result)); |
| 156 }); |
| 157 }); |
| 158 |
| 159 |
| 160 QUnit.test('setJsonResponseFor', function(assert) { |
| 161 remoting.MockXhr.setJsonResponseFor('GET', 'http://foo.com', 'foo'); |
| 162 var promise = sendRequestForJson(); |
| 163 assert.throws(sendRequestForJson); |
| 164 return promise.then(function(/** remoting.Xhr.Response */ result) { |
| 165 assert.equal(result.status, 200); |
| 166 assert.equal(JSON.parse(result.getText()), 'foo'); |
| 167 assert.equal(result.getJson(), 'foo'); |
| 168 }); |
| 169 }); |
| 170 |
| 171 })(); |
OLD | NEW |