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