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.
9 var onStateChange = null;
10 var onIncomingStanzaCallback = null;
12 var signalStrategy = null;
15 module('dns_blackhole_checker', {
18 sinon.useFakeXMLHttpRequest().onCreate = function(xhr) {
23 onStateChange = sinon.spy();
24 onIncomingStanzaCallback = sinon.spy();
25 signalStrategy = new remoting.MockSignalStrategy();
26 checker = new remoting.DnsBlackholeChecker(signalStrategy);
28 checker.setStateChangedCallback(onStateChange);
29 checker.setIncomingStanzaCallback(onIncomingStanzaCallback);
31 sinon.assert.notCalled(onStateChange);
32 sinon.assert.notCalled(signalStrategy.connect);
33 checker.connect('server', 'username', 'authToken');
34 sinon.assert.calledWith(signalStrategy.connect, 'server', 'username',
37 QUnit.equal(fakeXhrs.length, 1, 'exactly one XHR is issued');
39 fakeXhrs[0].url, remoting.DnsBlackholeChecker.URL_TO_REQUEST_,
40 'the correct URL is requested');
42 teardown: function() {
43 base.dispose(checker);
44 sinon.assert.calledWith(onStateChange,
45 remoting.SignalStrategy.State.CLOSED);
48 onIncomingStanzaCallback = null;
55 fakeXhrs[0].respond(200);
56 sinon.assert.notCalled(onStateChange);
59 remoting.SignalStrategy.State.CONNECTING,
60 remoting.SignalStrategy.State.HANDSHAKE,
61 remoting.SignalStrategy.State.CONNECTED
62 ].forEach(function(state) {
63 signalStrategy.setStateForTesting(state);
64 sinon.assert.calledWith(onStateChange, state);
65 equal(checker.getState(), state);
70 test('http response after connected',
73 remoting.SignalStrategy.State.CONNECTING,
74 remoting.SignalStrategy.State.HANDSHAKE,
75 ].forEach(function(state) {
76 signalStrategy.setStateForTesting(state);
77 sinon.assert.calledWith(onStateChange, state);
78 equal(checker.getState(), state);
80 onStateChange.reset();
82 // Verify that DnsBlackholeChecker stays in HANDSHAKE state even if the
83 // signal strategy has connected.
84 signalStrategy.setStateForTesting(remoting.SignalStrategy.State.CONNECTED);
85 sinon.assert.notCalled(onStateChange);
86 equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE);
88 // Verify that DnsBlackholeChecker goes to CONNECTED state after the
89 // the HTTP request has succeeded.
90 fakeXhrs[0].respond(200);
91 sinon.assert.calledWith(onStateChange,
92 remoting.SignalStrategy.State.CONNECTED);
96 test('connect failed',
98 fakeXhrs[0].respond(200);
99 sinon.assert.notCalled(onStateChange);
102 remoting.SignalStrategy.State.CONNECTING,
103 remoting.SignalStrategy.State.FAILED
104 ].forEach(function(state) {
105 signalStrategy.setStateForTesting(state);
106 sinon.assert.calledWith(onStateChange, state);
113 fakeXhrs[0].respond(400);
114 sinon.assert.calledWith(onStateChange,
115 remoting.SignalStrategy.State.FAILED);
116 equal(checker.getError(), remoting.Error.NOT_AUTHORIZED);
117 onStateChange.reset();
120 remoting.SignalStrategy.State.CONNECTING,
121 remoting.SignalStrategy.State.HANDSHAKE,
122 remoting.SignalStrategy.State.CONNECTED
123 ].forEach(function(state) {
124 signalStrategy.setStateForTesting(state);
125 sinon.assert.notCalled(onStateChange);
126 equal(checker.getState(), remoting.SignalStrategy.State.FAILED);
131 test('blocked after connected',
134 remoting.SignalStrategy.State.CONNECTING,
135 remoting.SignalStrategy.State.HANDSHAKE,
136 ].forEach(function(state) {
137 signalStrategy.setStateForTesting(state);
138 sinon.assert.calledWith(onStateChange, state);
139 equal(checker.getState(), state);
141 onStateChange.reset();
143 // Verify that DnsBlackholeChecker stays in HANDSHAKE state even if the
144 // signal strategy has connected.
145 signalStrategy.setStateForTesting(remoting.SignalStrategy.State.CONNECTED);
146 sinon.assert.notCalled(onStateChange);
147 equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE);
149 // Verify that DnsBlackholeChecker goes to FAILED state after it gets the
150 // blocked HTTP response.
151 fakeXhrs[0].respond(400);
152 sinon.assert.calledWith(onStateChange,
153 remoting.SignalStrategy.State.FAILED);
154 equal(checker.getError(), remoting.Error.NOT_AUTHORIZED);