public async Task ProcessMessageReturnWhenRemoteClosed() { //ARRANGE var wsFactory = new ClientWebSocketFactory(); var ws = wsFactory.New(); var cancelSource = new CancellationTokenSource(); // DisposeAsync will close websocket await ws.ConnectAsync(new Uri("ws://127.0.0.1:5001/api/websocket"), CancellationToken.None).ConfigureAwait(false); // ACT await using var pipe = WebSocketMessagePipeline <HassMessageBase> .CreateWebSocketMessagePipeline(ws); // First read the first sent var msg = await pipe.GetNextMessageAsync(cancelSource.Token).ConfigureAwait(false); Assert.Equal("auth_required", msg.Type); var task = pipe.GetNextMessageAsync(cancelSource.Token).ConfigureAwait(false); await Task.Delay(100); await pipe.SendMessageAsync(new HassMessageBase { Type = "fake_disconnect_test" }, cancelSource.Token); var x = await Assert.ThrowsAsync <OperationCanceledException>(async() => await task); Assert.True(x is object); Assert.False(pipe.IsValid); }
public async Task WrongMessagesFromHassShouldReturnFalse() { // ARRANGE var mock = new HassWebSocketMock(); // First message from Home Assistant is auth required mock.AddResponse(@"{""type"": ""any_kind_of_type""}"); await using var pipe = new WebSocketMessagePipeline <HassMessageBase>(mock.Object); // ACT var msg = await pipe.GetNextMessageAsync(CancellationToken.None); Assert.Equal("any_kind_of_type", msg.Type); }
public async Task ProcessMessageGetCorrectResult() { //ARRANGE var wsFactory = new ClientWebSocketFactory(); var ws = wsFactory.New(); // DisposeAsync will close websocket await ws.ConnectAsync(new Uri("ws://127.0.0.1:5001/api/websocket"), CancellationToken.None).ConfigureAwait(false); // ACT await using var pipe = WebSocketMessagePipeline <HassMessageBase> .CreateWebSocketMessagePipeline(ws); var x = await pipe.GetNextMessageAsync(CancellationToken.None).ConfigureAwait(false); // await ws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Close", CancellationToken.None).ConfigureAwait(false); // ASSERT Assert.Equal("auth_required", x.Type); }
public async Task ProcessBigMessageGetCorrectResult() { //ARRANGE var wsFactory = new ClientWebSocketFactory(); var ws = wsFactory.New(); var cancelSource = new CancellationTokenSource(); // DisposeAsync will close websocket await ws.ConnectAsync(new Uri("ws://127.0.0.1:5001/api/websocket"), CancellationToken.None).ConfigureAwait(false); // ACT await using var pipe = WebSocketMessagePipeline <HassMessageBase> .CreateWebSocketMessagePipeline(ws); _ = await pipe.GetNextMessageAsync(CancellationToken.None).ConfigureAwait(false); await pipe.SendMessageAsync(new HassMessageBase { Type = "get_states" }, cancelSource.Token); var msg = await pipe.GetNextMessageAsync(CancellationToken.None).ConfigureAwait(false); // ASSERT Assert.Equal("result", msg.Type); }