| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 * TODO(garykac): Create interface for SignalStrategy. | 7 * TODO(garykac): Create interface for SignalStrategy. |
| 8 * @suppress {checkTypes|checkVars|reportUnknownTypes|visibility} | 8 * @suppress {checkTypes|checkVars|reportUnknownTypes|visibility} |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 (function() { | 11 (function() { |
| 12 | 12 |
| 13 'use strict'; | 13 'use strict'; |
| 14 | 14 |
| 15 /** @type {(sinon.Spy|function(remoting.SignalStrategy.State))} */ | 15 /** @type {(sinon.Spy|function(remoting.SignalStrategy.State))} */ |
| 16 var onStateChange = null; | 16 var onStateChange = null; |
| 17 | 17 |
| 18 /** @type {(sinon.Spy|function(Element):void)} */ | 18 /** @type {(sinon.Spy|function(Element):void)} */ |
| 19 var onIncomingStanzaCallback = null; | 19 var onIncomingStanzaCallback = null; |
| 20 | 20 |
| 21 /** @type {remoting.DnsBlackholeChecker} */ | 21 /** @type {remoting.DnsBlackholeChecker} */ |
| 22 var checker = null; | 22 var checker = null; |
| 23 | 23 |
| 24 /** @type {remoting.MockSignalStrategy} */ | 24 /** @type {remoting.MockSignalStrategy} */ |
| 25 var signalStrategy = null; | 25 var signalStrategy = null; |
| 26 var fakeXhrs; | 26 |
| 27 /** @type {sinon.FakeXhr} */ |
| 28 var fakeXhr = null; |
| 27 | 29 |
| 28 QUnit.module('dns_blackhole_checker', { | 30 QUnit.module('dns_blackhole_checker', { |
| 29 beforeEach: function(assert) { | 31 beforeEach: function(assert) { |
| 30 fakeXhrs = []; | |
| 31 sinon.useFakeXMLHttpRequest().onCreate = function(xhr) { | 32 sinon.useFakeXMLHttpRequest().onCreate = function(xhr) { |
| 32 fakeXhrs.push(xhr); | 33 QUnit.equal(fakeXhr, null, 'exactly one XHR is issued'); |
| 34 fakeXhr = xhr; |
| 33 }; | 35 }; |
| 34 | 36 |
| 35 onStateChange = sinon.spy(); | 37 onStateChange = sinon.spy(); |
| 36 onIncomingStanzaCallback = sinon.spy(); | 38 onIncomingStanzaCallback = sinon.spy(); |
| 37 signalStrategy = new remoting.MockSignalStrategy(); | 39 signalStrategy = new remoting.MockSignalStrategy(); |
| 38 checker = new remoting.DnsBlackholeChecker(signalStrategy); | 40 checker = new remoting.DnsBlackholeChecker(signalStrategy); |
| 39 | 41 |
| 40 checker.setStateChangedCallback(onStateChange); | 42 checker.setStateChangedCallback(onStateChange); |
| 41 checker.setIncomingStanzaCallback(onIncomingStanzaCallback); | 43 checker.setIncomingStanzaCallback(onIncomingStanzaCallback); |
| 42 | 44 |
| 43 sinon.assert.notCalled(onStateChange); | 45 sinon.assert.notCalled(onStateChange); |
| 44 sinon.assert.notCalled(signalStrategy.connect); | 46 sinon.assert.notCalled(signalStrategy.connect); |
| 45 checker.connect('server', 'username', 'authToken'); | 47 checker.connect('server', 'username', 'authToken'); |
| 46 sinon.assert.calledWith(signalStrategy.connect, 'server', 'username', | 48 sinon.assert.calledWith(signalStrategy.connect, 'server', 'username', |
| 47 'authToken'); | 49 'authToken'); |
| 48 | 50 |
| 49 assert.equal(fakeXhrs.length, 1, 'exactly one XHR is issued'); | |
| 50 assert.equal( | 51 assert.equal( |
| 51 fakeXhrs[0].url, remoting.DnsBlackholeChecker.URL_TO_REQUEST_, | 52 fakeXhr.url, remoting.DnsBlackholeChecker.URL_TO_REQUEST_, |
| 52 'the correct URL is requested'); | 53 'the correct URL is requested'); |
| 53 }, | 54 }, |
| 54 afterEach: function() { | 55 afterEach: function() { |
| 55 base.dispose(checker); | 56 base.dispose(checker); |
| 56 sinon.assert.calledWith(onStateChange, | 57 sinon.assert.calledWith(onStateChange, |
| 57 remoting.SignalStrategy.State.CLOSED); | 58 remoting.SignalStrategy.State.CLOSED); |
| 58 | 59 |
| 59 onStateChange = null; | 60 onStateChange = null; |
| 60 onIncomingStanzaCallback = null; | 61 onIncomingStanzaCallback = null; |
| 61 checker = null; | 62 checker = null; |
| 62 }, | 63 fakeXhr = null; |
| 64 } |
| 63 }); | 65 }); |
| 64 | 66 |
| 65 QUnit.test('success', | 67 QUnit.test('success', |
| 66 function(assert) { | 68 function(assert) { |
| 67 fakeXhrs[0].respond(200); | 69 function checkState(state) { |
| 68 sinon.assert.notCalled(onStateChange); | |
| 69 | |
| 70 [ | |
| 71 remoting.SignalStrategy.State.CONNECTING, | |
| 72 remoting.SignalStrategy.State.HANDSHAKE, | |
| 73 remoting.SignalStrategy.State.CONNECTED | |
| 74 ].forEach(function(state) { | |
| 75 signalStrategy.setStateForTesting(state); | 70 signalStrategy.setStateForTesting(state); |
| 76 sinon.assert.calledWith(onStateChange, state); | 71 sinon.assert.calledWith(onStateChange, state); |
| 77 assert.equal(checker.getState(), state); | 72 assert.equal(checker.getState(), state); |
| 73 } |
| 74 |
| 75 return base.SpyPromise.run(function() { |
| 76 fakeXhr.respond(200); |
| 77 }).then(function() { |
| 78 sinon.assert.notCalled(onStateChange); |
| 79 checkState(remoting.SignalStrategy.State.CONNECTING); |
| 80 checkState(remoting.SignalStrategy.State.HANDSHAKE); |
| 81 checkState(remoting.SignalStrategy.State.CONNECTED); |
| 82 }); |
| 83 }); |
| 84 |
| 85 QUnit.test('http response after connected', |
| 86 function(assert) { |
| 87 function checkState(state) { |
| 88 signalStrategy.setStateForTesting(state); |
| 89 sinon.assert.calledWith(onStateChange, state); |
| 90 assert.equal(checker.getState(), state); |
| 91 } |
| 92 |
| 93 checkState(remoting.SignalStrategy.State.CONNECTING); |
| 94 checkState(remoting.SignalStrategy.State.HANDSHAKE); |
| 95 onStateChange.reset(); |
| 96 |
| 97 // Verify that DnsBlackholeChecker stays in HANDSHAKE state even if the |
| 98 // signal strategy has connected. |
| 99 return base.SpyPromise.run(function() { |
| 100 signalStrategy.setStateForTesting( |
| 101 remoting.SignalStrategy.State.CONNECTED); |
| 102 }).then(function() { |
| 103 sinon.assert.notCalled(onStateChange); |
| 104 assert.equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE); |
| 105 |
| 106 // Verify that DnsBlackholeChecker goes to CONNECTED state after the |
| 107 // the HTTP request has succeeded. |
| 108 return base.SpyPromise.run(function() { |
| 109 fakeXhr.respond(200); |
| 110 }); |
| 111 }).then(function() { |
| 112 sinon.assert.calledWith(onStateChange, |
| 113 remoting.SignalStrategy.State.CONNECTED); |
| 114 }); |
| 115 }); |
| 116 |
| 117 QUnit.test('connect failed', |
| 118 function(assert) { |
| 119 function checkState(state) { |
| 120 signalStrategy.setStateForTesting(state); |
| 121 sinon.assert.calledWith(onStateChange, state); |
| 122 }; |
| 123 |
| 124 return base.SpyPromise.run(function() { |
| 125 fakeXhr.respond(200); |
| 126 }).then(function() { |
| 127 sinon.assert.notCalled(onStateChange); |
| 128 checkState(remoting.SignalStrategy.State.CONNECTING); |
| 129 checkState(remoting.SignalStrategy.State.FAILED); |
| 130 }); |
| 131 }); |
| 132 |
| 133 QUnit.test('blocked', |
| 134 function(assert) { |
| 135 function checkState(state) { |
| 136 assert.equal(checker.getError().getTag(), |
| 137 remoting.Error.Tag.NOT_AUTHORIZED); |
| 138 onStateChange.reset(); |
| 139 signalStrategy.setStateForTesting(state); |
| 140 sinon.assert.notCalled(onStateChange); |
| 141 assert.equal(checker.getState(), |
| 142 checker.getState(), |
| 143 remoting.SignalStrategy.State.FAILED, |
| 144 'checker state is still FAILED'); |
| 145 }; |
| 146 |
| 147 return base.SpyPromise.run(function() { |
| 148 fakeXhr.respond(400); |
| 149 }).then(function() { |
| 150 sinon.assert.calledWith( |
| 151 onStateChange, remoting.SignalStrategy.State.FAILED); |
| 152 assert.equal( |
| 153 checker.getError().getTag(), |
| 154 remoting.Error.Tag.NOT_AUTHORIZED, |
| 155 'checker error is NOT_AUTHORIZED'); |
| 156 checkState(remoting.SignalStrategy.State.CONNECTING); |
| 157 checkState(remoting.SignalStrategy.State.HANDSHAKE); |
| 158 checkState(remoting.SignalStrategy.State.FAILED); |
| 159 }); |
| 160 }); |
| 161 |
| 162 QUnit.test('blocked after connected', |
| 163 function(assert) { |
| 164 function checkState(state) { |
| 165 signalStrategy.setStateForTesting(state); |
| 166 sinon.assert.calledWith(onStateChange, state); |
| 167 assert.equal(checker.getState(), state); |
| 168 }; |
| 169 |
| 170 checkState(remoting.SignalStrategy.State.CONNECTING); |
| 171 checkState(remoting.SignalStrategy.State.HANDSHAKE); |
| 172 onStateChange.reset(); |
| 173 |
| 174 // Verify that DnsBlackholeChecker stays in HANDSHAKE state even |
| 175 // if the signal strategy has connected. |
| 176 return base.SpyPromise.run(function() { |
| 177 signalStrategy.setStateForTesting( |
| 178 remoting.SignalStrategy.State.CONNECTED); |
| 179 }).then(function() { |
| 180 sinon.assert.notCalled(onStateChange); |
| 181 assert.equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE); |
| 182 |
| 183 // Verify that DnsBlackholeChecker goes to FAILED state after it |
| 184 // gets the blocked HTTP response. |
| 185 return base.SpyPromise.run(function() { |
| 186 fakeXhr.respond(400); |
| 187 }); |
| 188 }).then(function() { |
| 189 sinon.assert.calledWith(onStateChange, |
| 190 remoting.SignalStrategy.State.FAILED); |
| 191 assert.ok(checker.getError().hasTag(remoting.Error.Tag.NOT_AUTHORIZED)); |
| 78 }); | 192 }); |
| 79 } | 193 } |
| 80 ); | 194 ); |
| 81 | 195 |
| 82 QUnit.test('http response after connected', | |
| 83 function(assert) { | |
| 84 [ | |
| 85 remoting.SignalStrategy.State.CONNECTING, | |
| 86 remoting.SignalStrategy.State.HANDSHAKE, | |
| 87 ].forEach(function(state) { | |
| 88 signalStrategy.setStateForTesting(state); | |
| 89 sinon.assert.calledWith(onStateChange, state); | |
| 90 assert.equal(checker.getState(), state); | |
| 91 }); | |
| 92 onStateChange.reset(); | |
| 93 | |
| 94 // Verify that DnsBlackholeChecker stays in HANDSHAKE state even if the | |
| 95 // signal strategy has connected. | |
| 96 signalStrategy.setStateForTesting(remoting.SignalStrategy.State.CONNECTED); | |
| 97 sinon.assert.notCalled(onStateChange); | |
| 98 assert.equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE); | |
| 99 | |
| 100 // Verify that DnsBlackholeChecker goes to CONNECTED state after the | |
| 101 // the HTTP request has succeeded. | |
| 102 fakeXhrs[0].respond(200); | |
| 103 sinon.assert.calledWith(onStateChange, | |
| 104 remoting.SignalStrategy.State.CONNECTED); | |
| 105 } | |
| 106 ); | |
| 107 | |
| 108 QUnit.test('connect failed', | |
| 109 function(assert) { | |
| 110 fakeXhrs[0].respond(200); | |
| 111 sinon.assert.notCalled(onStateChange); | |
| 112 | |
| 113 [ | |
| 114 remoting.SignalStrategy.State.CONNECTING, | |
| 115 remoting.SignalStrategy.State.FAILED | |
| 116 ].forEach(function(state) { | |
| 117 signalStrategy.setStateForTesting(state); | |
| 118 sinon.assert.calledWith(onStateChange, state); | |
| 119 }); | |
| 120 } | |
| 121 ); | |
| 122 | |
| 123 QUnit.test('blocked', | |
| 124 function(assert) { | |
| 125 fakeXhrs[0].respond(400); | |
| 126 sinon.assert.calledWith(onStateChange, | |
| 127 remoting.SignalStrategy.State.FAILED); | |
| 128 assert.equal(checker.getError().getTag(), | |
| 129 remoting.Error.Tag.NOT_AUTHORIZED); | |
| 130 onStateChange.reset(); | |
| 131 | |
| 132 [ | |
| 133 remoting.SignalStrategy.State.CONNECTING, | |
| 134 remoting.SignalStrategy.State.HANDSHAKE, | |
| 135 remoting.SignalStrategy.State.CONNECTED | |
| 136 ].forEach(function(state) { | |
| 137 signalStrategy.setStateForTesting(state); | |
| 138 sinon.assert.notCalled(onStateChange); | |
| 139 assert.equal(checker.getState(), remoting.SignalStrategy.State.FAILED); | |
| 140 }); | |
| 141 } | |
| 142 ); | |
| 143 | |
| 144 QUnit.test('blocked after connected', | |
| 145 function(assert) { | |
| 146 [ | |
| 147 remoting.SignalStrategy.State.CONNECTING, | |
| 148 remoting.SignalStrategy.State.HANDSHAKE, | |
| 149 ].forEach(function(state) { | |
| 150 signalStrategy.setStateForTesting(state); | |
| 151 sinon.assert.calledWith(onStateChange, state); | |
| 152 assert.equal(checker.getState(), state); | |
| 153 }); | |
| 154 onStateChange.reset(); | |
| 155 | |
| 156 // Verify that DnsBlackholeChecker stays in HANDSHAKE state even if the | |
| 157 // signal strategy has connected. | |
| 158 signalStrategy.setStateForTesting(remoting.SignalStrategy.State.CONNECTED); | |
| 159 sinon.assert.notCalled(onStateChange); | |
| 160 assert.equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE); | |
| 161 | |
| 162 // Verify that DnsBlackholeChecker goes to FAILED state after it gets the | |
| 163 // blocked HTTP response. | |
| 164 fakeXhrs[0].respond(400); | |
| 165 sinon.assert.calledWith(onStateChange, | |
| 166 remoting.SignalStrategy.State.FAILED); | |
| 167 assert.ok(checker.getError().hasTag(remoting.Error.Tag.NOT_AUTHORIZED)); | |
| 168 } | |
| 169 ); | |
| 170 | |
| 171 })(); | 196 })(); |
| OLD | NEW |