/test/DotNetty.Codecs.Http.Tests/WebSockets/WebSocketFrameAggregatorTest.cs

https://github.com/Azure/DotNetty · C# · 140 lines · 116 code · 22 blank · 2 comment · 3 complexity · 8e47c31407ad644f34058cff949419c1 MD5 · raw file

  1. // Copyright (c) Microsoft. All rights reserved.
  2. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  3. namespace DotNetty.Codecs.Http.Tests.WebSockets
  4. {
  5. using System;
  6. using System.Text;
  7. using DotNetty.Buffers;
  8. using DotNetty.Codecs.Http.WebSockets;
  9. using DotNetty.Common.Utilities;
  10. using DotNetty.Transport.Channels.Embedded;
  11. using Xunit;
  12. public class WebSocketFrameAggregatorTest
  13. {
  14. readonly byte[] content1 = Encoding.UTF8.GetBytes("Content1");
  15. readonly byte[] content2 = Encoding.UTF8.GetBytes("Content2");
  16. readonly byte[] content3 = Encoding.UTF8.GetBytes("Content3");
  17. readonly byte[] aggregatedContent;
  18. public WebSocketFrameAggregatorTest()
  19. {
  20. this.aggregatedContent = new byte[this.content1.Length + this.content2.Length + this.content3.Length];
  21. Array.Copy(this.content1, 0, this.aggregatedContent, 0, this.content1.Length);
  22. Array.Copy(this.content2, 0, this.aggregatedContent, this.content1.Length, this.content2.Length);
  23. Array.Copy(this.content3, 0, this.aggregatedContent, this.content1.Length + this.content2.Length, this.content3.Length);
  24. }
  25. [Fact]
  26. public void AggregationBinary()
  27. {
  28. var channel = new EmbeddedChannel(new WebSocketFrameAggregator(int.MaxValue));
  29. channel.WriteInbound(new BinaryWebSocketFrame(true, 1, Unpooled.WrappedBuffer(this.content1)));
  30. channel.WriteInbound(new BinaryWebSocketFrame(false, 0, Unpooled.WrappedBuffer(this.content1)));
  31. channel.WriteInbound(new ContinuationWebSocketFrame(false, 0, Unpooled.WrappedBuffer(this.content2)));
  32. channel.WriteInbound(new PingWebSocketFrame(Unpooled.WrappedBuffer(this.content1)));
  33. channel.WriteInbound(new PongWebSocketFrame(Unpooled.WrappedBuffer(this.content1)));
  34. channel.WriteInbound(new ContinuationWebSocketFrame(true, 0, Unpooled.WrappedBuffer(this.content3)));
  35. Assert.True(channel.Finish());
  36. var frame = channel.ReadInbound<BinaryWebSocketFrame>();
  37. Assert.True(frame.IsFinalFragment);
  38. Assert.Equal(1, frame.Rsv);
  39. Assert.Equal(this.content1, ToBytes(frame.Content));
  40. var frame2 = channel.ReadInbound<PingWebSocketFrame>();
  41. Assert.True(frame2.IsFinalFragment);
  42. Assert.Equal(0, frame2.Rsv);
  43. Assert.Equal(this.content1, ToBytes(frame2.Content));
  44. var frame3 = channel.ReadInbound<PongWebSocketFrame>();
  45. Assert.True(frame3.IsFinalFragment);
  46. Assert.Equal(0, frame3.Rsv);
  47. Assert.Equal(this.content1, ToBytes(frame3.Content));
  48. var frame4 = channel.ReadInbound<BinaryWebSocketFrame>();
  49. Assert.True(frame4.IsFinalFragment);
  50. Assert.Equal(0, frame4.Rsv);
  51. Assert.Equal(this.aggregatedContent, ToBytes(frame4.Content));
  52. Assert.Null(channel.ReadInbound<object>());
  53. }
  54. [Fact]
  55. public void AggregationText()
  56. {
  57. var channel = new EmbeddedChannel(new WebSocketFrameAggregator(int.MaxValue));
  58. channel.WriteInbound(new TextWebSocketFrame(true, 1, Unpooled.WrappedBuffer(this.content1)));
  59. channel.WriteInbound(new TextWebSocketFrame(false, 0, Unpooled.WrappedBuffer(this.content1)));
  60. channel.WriteInbound(new ContinuationWebSocketFrame(false, 0, Unpooled.WrappedBuffer(this.content2)));
  61. channel.WriteInbound(new PingWebSocketFrame(Unpooled.WrappedBuffer(this.content1)));
  62. channel.WriteInbound(new PongWebSocketFrame(Unpooled.WrappedBuffer(this.content1)));
  63. channel.WriteInbound(new ContinuationWebSocketFrame(true, 0, Unpooled.WrappedBuffer(this.content3)));
  64. Assert.True(channel.Finish());
  65. var frame = channel.ReadInbound<TextWebSocketFrame>();
  66. Assert.True(frame.IsFinalFragment);
  67. Assert.Equal(1, frame.Rsv);
  68. Assert.Equal(this.content1, ToBytes(frame.Content));
  69. var frame2 = channel.ReadInbound<PingWebSocketFrame>();
  70. Assert.True(frame2.IsFinalFragment);
  71. Assert.Equal(0, frame2.Rsv);
  72. Assert.Equal(this.content1, ToBytes(frame2.Content));
  73. var frame3 = channel.ReadInbound<PongWebSocketFrame>();
  74. Assert.True(frame3.IsFinalFragment);
  75. Assert.Equal(0, frame3.Rsv);
  76. Assert.Equal(this.content1, ToBytes(frame3.Content));
  77. var frame4 = channel.ReadInbound<TextWebSocketFrame>();
  78. Assert.True(frame4.IsFinalFragment);
  79. Assert.Equal(0, frame4.Rsv);
  80. Assert.Equal(this.aggregatedContent, ToBytes(frame4.Content));
  81. Assert.Null(channel.ReadInbound<object>());
  82. }
  83. [Fact]
  84. public void TextFrameTooBig()
  85. {
  86. var channel = new EmbeddedChannel(new WebSocketFrameAggregator(8));
  87. channel.WriteInbound(new BinaryWebSocketFrame(true, 1, Unpooled.WrappedBuffer(this.content1)));
  88. channel.WriteInbound(new BinaryWebSocketFrame(false, 0, Unpooled.WrappedBuffer(this.content1)));
  89. Assert.Throws<TooLongFrameException>(() =>
  90. channel.WriteInbound(new ContinuationWebSocketFrame(false, 0, Unpooled.WrappedBuffer(this.content2))));
  91. channel.WriteInbound(new ContinuationWebSocketFrame(false, 0, Unpooled.WrappedBuffer(this.content2)));
  92. channel.WriteInbound(new ContinuationWebSocketFrame(true, 0, Unpooled.WrappedBuffer(this.content2)));
  93. channel.WriteInbound(new BinaryWebSocketFrame(true, 1, Unpooled.WrappedBuffer(this.content1)));
  94. channel.WriteInbound(new BinaryWebSocketFrame(false, 0, Unpooled.WrappedBuffer(this.content1)));
  95. Assert.Throws<TooLongFrameException>(() =>
  96. channel.WriteInbound(new ContinuationWebSocketFrame(false, 0, Unpooled.WrappedBuffer(this.content2))));
  97. channel.WriteInbound(new ContinuationWebSocketFrame(false, 0, Unpooled.WrappedBuffer(this.content2)));
  98. channel.WriteInbound(new ContinuationWebSocketFrame(true, 0, Unpooled.WrappedBuffer(this.content2)));
  99. for (;;)
  100. {
  101. var msg = channel.ReadInbound<object>();
  102. if (msg == null)
  103. {
  104. break;
  105. }
  106. ReferenceCountUtil.Release(msg);
  107. }
  108. channel.Finish();
  109. }
  110. static byte[] ToBytes(IByteBuffer buf)
  111. {
  112. var bytes = new byte[buf.ReadableBytes];
  113. buf.ReadBytes(bytes);
  114. buf.Release();
  115. return bytes;
  116. }
  117. }
  118. }