|
| 1 | +// The MIT License(MIT) |
| 2 | +// |
| 3 | +// Copyright(c) 2015-2017 Ripcord Software Ltd |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +using System; |
| 24 | +using System.IO; |
| 25 | +using System.Text; |
| 26 | +using System.Linq; |
| 27 | +using System.Collections.Generic; |
| 28 | +using System.Diagnostics.CodeAnalysis; |
| 29 | + |
| 30 | +using Xunit; |
| 31 | + |
| 32 | +using RipcordSoftware.HttpWebClient; |
| 33 | + |
| 34 | +namespace HttpWebClient.UnitTests |
| 35 | +{ |
| 36 | + [ExcludeFromCodeCoverage] |
| 37 | + public class TestHttpWebClientResponseStream |
| 38 | + { |
| 39 | + #region Private fields |
| 40 | + private const string HeaderText = |
| 41 | + "HTTP/1.1 200 OK\r\n" + |
| 42 | + "Date: Mon, 23 May 2005 22:38:34 GMT\r\n" + |
| 43 | + "Content-Type: text/html; charset=UTF-8\r\n" + |
| 44 | + "Content-Encoding: UTF-8\r\n" + |
| 45 | + "Content-Length: 138\r\n" + |
| 46 | + "Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT\r\n" + |
| 47 | + "Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)\r\n" + |
| 48 | + "ETag: \"3f80f-1b6-3e1cb03b\"\r\n" + |
| 49 | + "Accept-Ranges: bytes\r\n" + |
| 50 | + "Connection: close\r\n" + |
| 51 | + "\r\n"; |
| 52 | + |
| 53 | + private const string BodyText = |
| 54 | + "<html>\r\n" + |
| 55 | + "<head>\r\n" + |
| 56 | + " <title>An Example Page</title>\r\n" + |
| 57 | + "</head>\r\n" + |
| 58 | + "<body>\r\n" + |
| 59 | + " Hello World, this is a very simple HTML document.\r\n" + |
| 60 | + "</body>\r\n" + |
| 61 | + "</html>"; |
| 62 | + |
| 63 | + private const string FullResponseText = HeaderText + BodyText; |
| 64 | + |
| 65 | + private static readonly byte[] _headerBytes = Encoding.ASCII.GetBytes(HeaderText); |
| 66 | + private static readonly byte[] _bodyBytes = Encoding.ASCII.GetBytes(BodyText); |
| 67 | + private static readonly byte[] _fullResponseBytes = Encoding.ASCII.GetBytes(FullResponseText); |
| 68 | + #endregion |
| 69 | + |
| 70 | + #region Tests |
| 71 | + [Fact] |
| 72 | + public void TestInitializedHttpWebClientResponseStream() |
| 73 | + { |
| 74 | + var socket = new MemoryStreamSocket(); |
| 75 | + var memStream = new MemoryStream(); |
| 76 | + |
| 77 | + using (var stream = new HttpWebClientResponseStream(socket, memStream)) |
| 78 | + { |
| 79 | + Assert.True(stream.CanRead); |
| 80 | + Assert.False(stream.CanWrite); |
| 81 | + Assert.False(stream.CanSeek); |
| 82 | + Assert.False(stream.CanTimeout); |
| 83 | + Assert.Equal(0, stream.Length); |
| 84 | + Assert.Equal(0, stream.Position); |
| 85 | + Assert.Equal(0, stream.Available); |
| 86 | + Assert.Equal(0, stream.SocketAvailable); |
| 87 | + Assert.Equal(0, stream.BufferAvailable); |
| 88 | + Assert.Equal(0, stream.Read(new byte[256], 0, 256)); |
| 89 | + Assert.Equal(-1, stream.ReadByte()); |
| 90 | + Assert.Equal(0, stream.SocketReceive(new byte[256], 0, 256)); |
| 91 | + |
| 92 | + Assert.Throws<NotImplementedException>(() => stream.Seek(100, SeekOrigin.End)); |
| 93 | + Assert.Throws<NotImplementedException>(() => stream.SetLength(1024)); |
| 94 | + Assert.Throws<NotImplementedException>(() => stream.Write(new byte[256], 0, 256)); |
| 95 | + Assert.Throws<NotImplementedException>(() => { stream.Position = 1024; }); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + [Fact] |
| 100 | + public void TestHttpWebClientResponseStreamMemStreamRead() |
| 101 | + { |
| 102 | + var socket = new MemoryStreamSocket(); |
| 103 | + var memStream = new MemoryStream(_fullResponseBytes); |
| 104 | + |
| 105 | + Assert.False(socket.ForceClose); |
| 106 | + |
| 107 | + using (var stream = new HttpWebClientResponseStream(socket, memStream)) |
| 108 | + { |
| 109 | + Assert.Equal(0, stream.Length); |
| 110 | + Assert.Equal(0, stream.Position); |
| 111 | + Assert.Equal(_fullResponseBytes.Length, stream.Available); |
| 112 | + Assert.Equal(0, stream.SocketAvailable); |
| 113 | + Assert.Equal(_fullResponseBytes.Length, stream.BufferAvailable); |
| 114 | + Assert.Equal(256, stream.Read(new byte[256], 0, 256)); |
| 115 | + Assert.True(stream.ReadByte() >= 0); |
| 116 | + Assert.Equal(0, stream.SocketReceive(new byte[256], 0, 256)); |
| 117 | + |
| 118 | + stream.SocketForceClose = true; |
| 119 | + } |
| 120 | + |
| 121 | + Assert.True(socket.ForceClose); |
| 122 | + } |
| 123 | + |
| 124 | + [Fact] |
| 125 | + public void TestHttpWebClientResponseStreamMemStreamSetLengthThenRead() |
| 126 | + { |
| 127 | + var memStream = new MemoryStream(_fullResponseBytes); |
| 128 | + |
| 129 | + using (var stream = new HttpWebClientResponseStream(null, memStream, _fullResponseBytes.Length)) |
| 130 | + { |
| 131 | + Assert.Equal(_fullResponseBytes.Length, stream.Length); |
| 132 | + Assert.Equal(0, stream.Position); |
| 133 | + Assert.Equal(_fullResponseBytes.Length, stream.Available); |
| 134 | + Assert.Equal(0, stream.SocketAvailable); |
| 135 | + Assert.Equal(_fullResponseBytes.Length, stream.BufferAvailable); |
| 136 | + Assert.Equal(256, stream.Read(new byte[256], 0, 256)); |
| 137 | + Assert.True(stream.ReadByte() >= 0); |
| 138 | + Assert.Equal(0, stream.SocketReceive(new byte[256], 0, 256)); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + [Fact] |
| 143 | + public void TestHttpWebClientResponseStreamMemStreamReadToEnd() |
| 144 | + { |
| 145 | + var memStream = new MemoryStream(_fullResponseBytes); |
| 146 | + |
| 147 | + using (var stream = new HttpWebClientResponseStream(null, memStream)) |
| 148 | + { |
| 149 | + var buffer = new byte[256]; |
| 150 | + |
| 151 | + var response = new List<byte>(); |
| 152 | + var bytesRead = 0; |
| 153 | + while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) |
| 154 | + { |
| 155 | + response.AddRange(new ArraySegment<byte>(buffer, 0, bytesRead)); |
| 156 | + } |
| 157 | + |
| 158 | + Assert.Equal(_fullResponseBytes.Length, response.Count); |
| 159 | + Assert.True(response.SequenceEqual(_fullResponseBytes)); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + [Fact] |
| 164 | + public void TestHttpWebClientResponseStreamSocketRead() |
| 165 | + { |
| 166 | + var socket = new MemoryStreamSocket(null, FullResponseText); |
| 167 | + |
| 168 | + using (var stream = new HttpWebClientResponseStream(socket, null)) |
| 169 | + { |
| 170 | + Assert.Equal(0, stream.Length); |
| 171 | + Assert.Equal(0, stream.Position); |
| 172 | + Assert.Equal(_fullResponseBytes.Length, stream.Available); |
| 173 | + Assert.Equal(_fullResponseBytes.Length, stream.SocketAvailable); |
| 174 | + Assert.Equal(0, stream.BufferAvailable); |
| 175 | + Assert.Equal(64, stream.Read(new byte[64], 0, 64)); |
| 176 | + Assert.True(stream.ReadByte() >= 0); |
| 177 | + Assert.Equal(64, stream.SocketReceive(new byte[64], 0, 64)); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + [Fact] |
| 182 | + public void TestHttpWebClientResponseStreamSplitRead() |
| 183 | + { |
| 184 | + var socket = new MemoryStreamSocket(null, BodyText); |
| 185 | + var memStream = new MemoryStream(_headerBytes); |
| 186 | + |
| 187 | + using (var stream = new HttpWebClientResponseStream(socket, memStream)) |
| 188 | + { |
| 189 | + Assert.Equal(0, stream.Length); |
| 190 | + Assert.Equal(0, stream.Position); |
| 191 | + Assert.Equal(_fullResponseBytes.Length, stream.Available); |
| 192 | + Assert.Equal(_bodyBytes.Length, stream.SocketAvailable); |
| 193 | + Assert.Equal(_headerBytes.Length, stream.BufferAvailable); |
| 194 | + Assert.Equal(64, stream.Read(new byte[64], 0, 64)); |
| 195 | + Assert.True(stream.ReadByte() >= 0); |
| 196 | + Assert.Equal(64, stream.SocketReceive(new byte[64], 0, 64)); |
| 197 | + |
| 198 | + Assert.Equal(65, stream.Length); |
| 199 | + Assert.Equal(65, stream.Position); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + [Fact] |
| 204 | + public void TestHttpWebClientResponseStreamMemStreamPeek() |
| 205 | + { |
| 206 | + var socket = new MemoryStreamSocket(); |
| 207 | + var memStream = new MemoryStream(_fullResponseBytes); |
| 208 | + |
| 209 | + using (var stream = new HttpWebClientResponseStream(socket, memStream)) |
| 210 | + { |
| 211 | + Assert.Equal(0, stream.Length); |
| 212 | + Assert.Equal(0, stream.Position); |
| 213 | + Assert.Equal(_fullResponseBytes.Length, stream.Available); |
| 214 | + Assert.Equal(0, stream.SocketAvailable); |
| 215 | + Assert.Equal(_fullResponseBytes.Length, stream.BufferAvailable); |
| 216 | + |
| 217 | + Assert.Equal(256, stream.Read(new byte[256], 0, 256, true)); |
| 218 | + |
| 219 | + Assert.Equal(0, stream.Length); |
| 220 | + Assert.Equal(0, stream.Position); |
| 221 | + Assert.Equal(_fullResponseBytes.Length, stream.Available); |
| 222 | + Assert.Equal(0, stream.SocketAvailable); |
| 223 | + Assert.Equal(_fullResponseBytes.Length, stream.BufferAvailable); |
| 224 | + |
| 225 | + Assert.Equal(256, stream.Read(new byte[256], 0, 256)); |
| 226 | + |
| 227 | + Assert.Equal(256, stream.Length); |
| 228 | + Assert.Equal(256, stream.Position); |
| 229 | + Assert.Equal(_fullResponseBytes.Length - 256, stream.Available); |
| 230 | + Assert.Equal(0, stream.SocketAvailable); |
| 231 | + Assert.Equal(_fullResponseBytes.Length - 256, stream.BufferAvailable); |
| 232 | + } |
| 233 | + } |
| 234 | + #endregion |
| 235 | + } |
| 236 | +} |
0 commit comments