public async Task ConnectWithUriNullThrowsArgumentNullException() { // ARRANGE var mock = new HassWebSocketMock(); // Get the default state hass client and we add no response messages await using var hassClient = mock.GetHassClient(); await Assert.ThrowsAsync <ArgumentNullException>( async() => await hassClient.ConnectAsync(null, "lss", false).ConfigureAwait(false)).ConfigureAwait(false); }
public async Task ConnectWithStateOtherThanOpenShouldReturnFalse(WebSocketState state) { // ARRANGE var mock = new HassWebSocketMock(); // Get the default state hass client await using var hassClient = mock.GetHassClient(); // Set Closed state to fake mock.SetupGet(x => x.State).Returns(state); // ACT and ASSERT Assert.False(await hassClient.ConnectAsync(new Uri("ws://anyurldoesntmatter.org"), "FAKETOKEN", false).ConfigureAwait(false)); }
public async Task ConnectTimeoutReturnsFalse() { // ARRANGE var mock = new HassWebSocketMock(); // Get the default state hass client and we add no response messages await using var hassClient = mock.GetHassClient(); // Set the timeout to a very low value for testing purposes hassClient.SocketTimeout = 20; // ACT AND ASSERT Assert.False(await hassClient.ConnectAsync(new Uri("ws://localhost:8192/api/websocket"), "TOKEN", false).ConfigureAwait(false)); }
public async Task WrongMessagesFromHassShouldReturnFalse() { // ARRANGE var mock = new HassWebSocketMock(); // Get the default state hass client await using var hassClient = mock.GetHassClient(); // First message from Home Assistant is auth required mock.AddResponse(@"{""type"": ""auth_required""}"); // Next one we fake it is auth ok mock.AddResponse(@"{""type"": ""result""}"); // ACT and ASSERT // Calls connect without getting the states initially Assert.False(await hassClient.ConnectAsync(new Uri("ws://anyurldoesntmatter.org"), "FAKETOKEN", false).ConfigureAwait(false)); }
public async Task ConnectWithAuthFailLogsErrorAndReturnFalse() { // ARRANGE var mock = new HassWebSocketMock(); // Get the default state hass client await using var hassClient = mock.GetHassClient(); // First message from Home Assistant is auth required mock.AddResponse(@"{""type"": ""auth_required""}"); // Next one we fake it is auth ok mock.AddResponse(@"{""type"": ""auth_invalid""}"); // ACT and ASSERT // Calls connect without getting the states initially Assert.False(await hassClient.ConnectAsync(new Uri("ws://anyurldoesntmatter.org"), "FAKETOKEN", false).ConfigureAwait(false)); // Make sure we logged the error. mock.Logger.AssertLogged(LogLevel.Error, Times.AtLeastOnce()); }
public async Task ConnectWithSslShouldStartWithWss() { // ARRANGE var mock = new HassWebSocketMock(); // Get the default state hass client and we add no response messages await using var hassClient = mock.GetHassClient(); // First message from Home Assistant is auth required mock.AddResponse(@"{""type"": ""auth_required""}"); // Next one we fake it is auth ok mock.AddResponse(@"{""type"": ""auth_ok""}"); // ACT and ASSERT // Connect with ssl await hassClient.ConnectAsync("localhost", 8123, true, "FAKETOKEN", false).ConfigureAwait(false); mock.Verify( n => n.ConnectAsync(new Uri("wss://localhost:8123/api/websocket"), It.IsAny <CancellationToken>()), Times.Once); }