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 var fakeXhrs; | |
27 | |
28 module('dns_blackhole_checker', { | |
29 setup: function() { | |
30 fakeXhrs = []; | |
31 sinon.useFakeXMLHttpRequest().onCreate = function(xhr) { | |
32 fakeXhrs.push(xhr); | |
33 }; | |
34 | |
35 onStateChange = sinon.spy(); | |
36 onIncomingStanzaCallback = sinon.spy(); | |
37 signalStrategy = new remoting.MockSignalStrategy(); | |
38 checker = new remoting.DnsBlackholeChecker(signalStrategy); | |
39 | |
40 checker.setStateChangedCallback(onStateChange); | |
41 checker.setIncomingStanzaCallback(onIncomingStanzaCallback); | |
42 | |
43 sinon.assert.notCalled(onStateChange); | |
44 sinon.assert.notCalled(signalStrategy.connect); | |
45 checker.connect('server', 'username', 'authToken'); | |
46 sinon.assert.calledWith(signalStrategy.connect, 'server', 'username', | |
47 'authToken'); | |
48 | |
49 QUnit.equal(fakeXhrs.length, 1, 'exactly one XHR is issued'); | |
50 QUnit.equal( | |
51 fakeXhrs[0].url, remoting.DnsBlackholeChecker.URL_TO_REQUEST_, | |
52 'the correct URL is requested'); | |
53 }, | |
54 teardown: function() { | |
55 base.dispose(checker); | |
56 sinon.assert.calledWith(onStateChange, | |
57 remoting.SignalStrategy.State.CLOSED); | |
58 | |
59 onStateChange = null; | |
60 onIncomingStanzaCallback = null; | |
61 checker = null; | |
62 }, | |
63 }); | |
64 | |
65 test('success', | |
66 function() { | |
67 fakeXhrs[0].respond(200); | |
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); | |
76 sinon.assert.calledWith(onStateChange, state); | |
77 equal(checker.getState(), state); | |
78 }); | |
79 } | |
80 ); | |
81 | |
82 test('http response after connected', | |
83 function() { | |
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 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 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 test('connect failed', | |
109 function() { | |
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 test('blocked', | |
124 function() { | |
125 fakeXhrs[0].respond(400); | |
126 sinon.assert.calledWith(onStateChange, | |
127 remoting.SignalStrategy.State.FAILED); | |
128 equal(checker.getError().tag, remoting.Error.Tag.NOT_AUTHORIZED); | |
129 onStateChange.reset(); | |
130 | |
131 [ | |
132 remoting.SignalStrategy.State.CONNECTING, | |
133 remoting.SignalStrategy.State.HANDSHAKE, | |
134 remoting.SignalStrategy.State.CONNECTED | |
135 ].forEach(function(state) { | |
136 signalStrategy.setStateForTesting(state); | |
137 sinon.assert.notCalled(onStateChange); | |
138 equal(checker.getState(), remoting.SignalStrategy.State.FAILED); | |
139 }); | |
140 } | |
141 ); | |
142 | |
143 test('blocked after connected', | |
144 function() { | |
145 [ | |
146 remoting.SignalStrategy.State.CONNECTING, | |
147 remoting.SignalStrategy.State.HANDSHAKE, | |
148 ].forEach(function(state) { | |
149 signalStrategy.setStateForTesting(state); | |
150 sinon.assert.calledWith(onStateChange, state); | |
151 equal(checker.getState(), state); | |
152 }); | |
153 onStateChange.reset(); | |
154 | |
155 // Verify that DnsBlackholeChecker stays in HANDSHAKE state even if the | |
156 // signal strategy has connected. | |
157 signalStrategy.setStateForTesting(remoting.SignalStrategy.State.CONNECTED); | |
158 sinon.assert.notCalled(onStateChange); | |
159 equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE); | |
160 | |
161 // Verify that DnsBlackholeChecker goes to FAILED state after it gets the | |
162 // blocked HTTP response. | |
163 fakeXhrs[0].respond(400); | |
164 sinon.assert.calledWith(onStateChange, | |
165 remoting.SignalStrategy.State.FAILED); | |
166 equal(checker.getError().tag, remoting.Error.Tag.NOT_AUTHORIZED); | |
167 } | |
168 ); | |
169 | |
170 })(); | |
OLD | NEW |