public async Task TestGetNextMessageAsyncBigMessageInChunks() { // ARRANGE var sb = new StringBuilder(8192); sb.Append(@"{ ""BigChunkedMessage"": """); sb.Append(new string('A', 8180)); sb.Append(@"""}"); WsMock.AddResponse(sb.ToString()); // ACT var msg = await DefaultPipeline.GetNextMessageAsync <ChunkedMessagesTestClass>(CancellationToken.None) .ConfigureAwait(false); msg.BigChunkedMessage .Should() .HaveLength(8180); WsMock.Verify(n => n.ReceiveAsync( It.IsAny <Memory <byte> >(), It.IsAny <CancellationToken>()), Times.AtLeast(2)); }
public async Task TestCloseAsyncCallsCloseWhenOpen() { //ACT await DefaultPipeline.CloseAsync().ConfigureAwait(false); WsMock.Verify(n => n.CloseAsync(It.IsAny <WebSocketCloseStatus>(), It.IsAny <string>(), It.IsAny <CancellationToken>())); }
public async Task TestSendMessageAsync() { // ACT await DefaultPipeline.SendMessageAsync <object>(new { someMessage = "hello" }, CancellationToken.None) .ConfigureAwait(false); // ASSERT WsMock.Verify(n => n.SendAsync( new ArraySegment <byte>(Encoding.UTF8.GetBytes("{\"someMessage\":\"hello\"}")), It.IsAny <WebSocketMessageType>(), It.IsAny <bool>(), It.IsAny <CancellationToken>() ), Times.Once); }
public async Task TestGetNextMessageAsyncOnRemoteClosingWebsocketShouldThrowException() { // ARRANGE WsMock.Setup(n => n.ReceiveAsync(It.IsAny <Memory <byte> >(), It.IsAny <CancellationToken>())) .ReturnsAsync((Memory <byte> _, CancellationToken _) => { // Simulate a close from remote WsMock.SetupGet(n => n.State).Returns(WebSocketState.CloseReceived); return(new ValueWebSocketReceiveResult(0, WebSocketMessageType.Close, true)); }); // ACT AND ASSERT // The operation should be cancelled when remote closes websocket await Assert.ThrowsAsync <OperationCanceledException>(async() => await DefaultPipeline.GetNextMessageAsync <HassMessage>(CancellationToken.None).ConfigureAwait(false)); // CloseOutput should always be called when // a close frame are sent from the remote websocket WsMock.Verify(n => n.CloseOutputAsync(It.IsAny <WebSocketCloseStatus>(), It.IsAny <string>(), It.IsAny <CancellationToken>())); }