Skip to content

Commit b10949b

Browse files
committed
Added top level request stream tests
1 parent 914680e commit b10949b

File tree

3 files changed

+263
-36
lines changed

3 files changed

+263
-36
lines changed

HttpWebClient.UnitTests/MemoryStreamSocket.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public MemoryStreamSocket(StringBuilder requestText, string responseText)
5555
#region Public properties
5656
public bool Connected => true;
5757

58-
public int Available => (int)(_responseStream.Length - _responseStream.Position);
58+
public int Available { get { return (int)(_responseStream.Length - _responseStream.Position); } }
5959

6060
public int Timeout { get; set; }
6161
public bool NoDelay { get; set; }
@@ -64,6 +64,8 @@ public MemoryStreamSocket(StringBuilder requestText, string responseText)
6464
public IntPtr Handle { get { throw new NotImplementedException(); } }
6565

6666
public string RequestText { get { return _requestText.ToString(); } }
67+
public long Position { get { return _requestStream.Position; } }
68+
public long Length { get { return _requestStream.Length; } }
6769
#endregion
6870

6971
#region Public methods

HttpWebClient.UnitTests/TestHttpWebClientRequestStream.cs

Lines changed: 222 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public TestHttpWebClientRequestStream()
7070

7171
#region Tests
7272
[Fact]
73-
public void TestInitializedRequestStream()
73+
public void TestInitializedInnerRequestStream()
7474
{
7575
using (var socket = new MemoryStreamSocket())
7676
{
@@ -93,7 +93,7 @@ public void TestInitializedRequestStream()
9393
}
9494

9595
[Fact]
96-
public void TestRequestStreamWrite()
96+
public void TestInnerRequestStreamWrite()
9797
{
9898
using (var socket = new MemoryStreamSocket())
9999
{
@@ -114,7 +114,7 @@ public void TestRequestStreamWrite()
114114
}
115115

116116
[Fact]
117-
public void TestRequestStreamFailedWrite()
117+
public void TestInnerRequestStreamFailedWrite()
118118
{
119119
var socket = new Mock<IHttpWebClientSocket>();
120120
socket.Setup(s => s.Send(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<System.Net.Sockets.SocketFlags>())).Returns(0);
@@ -133,7 +133,7 @@ public void TestRequestStreamFailedWrite()
133133
}
134134

135135
[Fact]
136-
public void TestRequestStreamWriteThrowws()
136+
public void TestInnerRequestStreamWriteThrows()
137137
{
138138
var socket = new Mock<IHttpWebClientSocket>();
139139
socket.Setup(s => s.Send(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<System.Net.Sockets.SocketFlags>())).Throws<Exception>();
@@ -230,6 +230,224 @@ public void TestChunkedRequestStreamWrite()
230230
Assert.Equal(chunkInfo.TotalWritten, memStream.Length);
231231
Assert.Equal(chunkInfo.TotalWritten, memStream.Position);
232232
}
233+
234+
[Fact]
235+
public void TestInitializedRequestStream()
236+
{
237+
using (var socket = new MemoryStreamSocket())
238+
{
239+
var headers = new HttpWebClientHeaders();
240+
241+
using (var stream = new HttpWebClientRequestStream(socket, headers))
242+
{
243+
Assert.False(stream.CanRead);
244+
Assert.True(stream.CanWrite);
245+
Assert.False(stream.CanSeek);
246+
Assert.False(stream.CanTimeout);
247+
Assert.Equal(0, stream.Length);
248+
Assert.Equal(0, stream.Position);
249+
250+
Assert.Throws<NotImplementedException>(() => stream.Seek(100, SeekOrigin.End));
251+
Assert.Throws<NotImplementedException>(() => stream.ReadByte());
252+
Assert.Throws<NotImplementedException>(() => stream.SetLength(1024));
253+
Assert.Throws<NotImplementedException>(() => stream.Read(new byte[256], 0, 256));
254+
Assert.Throws<NotImplementedException>(() => { stream.Position = 1024; });
255+
}
256+
}
257+
}
258+
259+
[Fact]
260+
public void TestRequestStreamWriteChunked()
261+
{
262+
var expectedHeaders = "POST /uri HTTP/1.1\r\nHost: localhost:42\r\nContent-Type: text/ascii\r\nTransfer-Encoding: chunked\r\n\r\n";
263+
var chunkInfo = CalculateChunkInfo(_buffer.Length);
264+
265+
using (var socket = new MemoryStreamSocket())
266+
{
267+
var headers = new HttpWebClientHeaders();
268+
headers.Hostname = "localhost";
269+
headers.Port = 42;
270+
headers.Method = "POST";
271+
headers.Secure = false;
272+
headers.Uri = "/uri";
273+
headers["Content-Type"] = "text/ascii";
274+
275+
using (var stream = new HttpWebClientRequestStream(socket, headers))
276+
{
277+
Assert.Equal(0, stream.Length);
278+
Assert.Equal(0, stream.Position);
279+
280+
stream.Write(_buffer, 0, _buffer.Length);
281+
282+
Assert.True(socket.RequestText.StartsWith(expectedHeaders));
283+
284+
Assert.Equal(chunkInfo.ChunkedWritten, stream.Length - expectedHeaders.Length);
285+
Assert.Equal(chunkInfo.ChunkedWritten, stream.Position - expectedHeaders.Length);
286+
}
287+
288+
Assert.Equal(chunkInfo.TotalWritten, socket.Length - expectedHeaders.Length);
289+
Assert.Equal(chunkInfo.TotalWritten, socket.Position - expectedHeaders.Length);
290+
}
291+
}
292+
293+
[Fact]
294+
public void TestRequestStreamWriteLength()
295+
{
296+
var expectedHeaders =
297+
string.Format("POST /uri HTTP/1.1\r\nHost: localhost:42\r\nContent-Type: text/ascii\r\nContent-Length: {0}\r\n\r\n", _buffer.Length);
298+
299+
using (var socket = new MemoryStreamSocket())
300+
{
301+
var headers = new HttpWebClientHeaders();
302+
headers.Hostname = "localhost";
303+
headers.Port = 42;
304+
headers.Method = "POST";
305+
headers.Secure = false;
306+
headers.Uri = "/uri";
307+
headers.ContentLength = _buffer.Length;
308+
headers["Content-Type"] = "text/ascii";
309+
310+
using (var stream = new HttpWebClientRequestStream(socket, headers))
311+
{
312+
Assert.Equal(0, stream.Length);
313+
Assert.Equal(0, stream.Position);
314+
315+
stream.Write(_buffer, 0, _buffer.Length);
316+
317+
Assert.True(socket.RequestText.StartsWith(expectedHeaders));
318+
319+
Assert.Equal(_buffer.Length + expectedHeaders.Length, stream.Length);
320+
Assert.Equal(_buffer.Length + expectedHeaders.Length, stream.Position);
321+
}
322+
323+
Assert.Equal(_buffer.Length + expectedHeaders.Length, socket.Length);
324+
Assert.Equal(_buffer.Length + expectedHeaders.Length, socket.Position);
325+
}
326+
}
327+
328+
[Fact]
329+
public void TestRequestStreamWritePostEmptyBody()
330+
{
331+
var expectedHeaders =
332+
string.Format("POST /uri HTTP/1.1\r\nHost: localhost:42\r\nContent-Type: text/ascii\r\nContent-Length: 0\r\n\r\n", _buffer.Length);
333+
334+
using (var socket = new MemoryStreamSocket())
335+
{
336+
var headers = new HttpWebClientHeaders();
337+
headers.Hostname = "localhost";
338+
headers.Port = 42;
339+
headers.Method = "POST";
340+
headers.Secure = false;
341+
headers.Uri = "/uri";
342+
headers.ContentLength = _buffer.Length;
343+
headers["Content-Type"] = "text/ascii";
344+
345+
using (var stream = new HttpWebClientRequestStream(socket, headers))
346+
{
347+
Assert.Equal(0, stream.Length);
348+
Assert.Equal(0, stream.Position);
349+
}
350+
351+
Assert.True(socket.RequestText.StartsWith(expectedHeaders));
352+
Assert.Equal(expectedHeaders.Length, socket.Length);
353+
Assert.Equal(expectedHeaders.Length, socket.Position);
354+
}
355+
}
356+
357+
[Fact]
358+
public void TestRequestStreamWritePutEmptyBody()
359+
{
360+
var expectedHeaders =
361+
string.Format("PUT /uri HTTP/1.1\r\nHost: localhost:42\r\nContent-Type: text/ascii\r\nContent-Length: 0\r\n\r\n", _buffer.Length);
362+
363+
using (var socket = new MemoryStreamSocket())
364+
{
365+
var headers = new HttpWebClientHeaders();
366+
headers.Hostname = "localhost";
367+
headers.Port = 42;
368+
headers.Method = "PUT";
369+
headers.Secure = false;
370+
headers.Uri = "/uri";
371+
headers.ContentLength = _buffer.Length;
372+
headers["Content-Type"] = "text/ascii";
373+
374+
using (var stream = new HttpWebClientRequestStream(socket, headers))
375+
{
376+
Assert.Equal(0, stream.Length);
377+
Assert.Equal(0, stream.Position);
378+
}
379+
380+
Assert.True(socket.RequestText.StartsWith(expectedHeaders));
381+
Assert.Equal(expectedHeaders.Length, socket.Length);
382+
Assert.Equal(expectedHeaders.Length, socket.Position);
383+
}
384+
}
385+
386+
[Fact]
387+
public void TestRequestStreamFailedHeaderWrite()
388+
{
389+
var socket = new Mock<IHttpWebClientSocket>();
390+
socket.Setup(s => s.Send(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<System.Net.Sockets.SocketFlags>())).Returns(0);
391+
392+
var headers = new HttpWebClientHeaders();
393+
headers.Hostname = "localhost";
394+
headers.Port = 42;
395+
headers.Method = "PUT";
396+
headers.Secure = false;
397+
headers.Uri = "/uri";
398+
headers.ContentLength = _buffer.Length;
399+
headers["Content-Type"] = "text/ascii";
400+
401+
Assert.Throws<HttpWebClientRequestException>(() =>
402+
{
403+
using (var stream = new HttpWebClientRequestStream(socket.Object, headers))
404+
{
405+
Assert.Equal(0, stream.Length);
406+
Assert.Equal(0, stream.Position);
407+
}
408+
});
409+
}
410+
411+
[Fact]
412+
public void TestRequestStreamCloseThrows()
413+
{
414+
var socket = new Mock<IHttpWebClientSocket>();
415+
socket.Setup(s => s.Send(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<System.Net.Sockets.SocketFlags>())).Throws<Exception>();
416+
417+
var headers = new HttpWebClientHeaders();
418+
headers.Hostname = "localhost";
419+
headers.Port = 42;
420+
headers.Method = "PUT";
421+
headers.Secure = false;
422+
headers.Uri = "/uri";
423+
headers.ContentLength = _buffer.Length;
424+
headers["Content-Type"] = "text/ascii";
425+
426+
Assert.Throws<HttpWebClientRequestException>(() =>
427+
{
428+
using (var stream = new HttpWebClientRequestStream(socket.Object, headers))
429+
{
430+
Assert.Equal(0, stream.Length);
431+
Assert.Equal(0, stream.Position);
432+
}
433+
});
434+
}
435+
436+
[Fact]
437+
public void TestRequestStreamHeaderWriteThrows()
438+
{
439+
var socket = new Mock<Stream>();
440+
socket.Setup(s => s.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>())).Throws<Exception>();
441+
442+
Assert.Throws<HttpWebClientRequestException>(() =>
443+
{
444+
using (var stream = new HttpWebClientRequestStream(socket.Object, new HttpWebClientHeaders()))
445+
{
446+
Assert.Equal(0, stream.Length);
447+
Assert.Equal(0, stream.Position);
448+
}
449+
});
450+
}
233451
#endregion
234452

235453
#region Private methods

0 commit comments

Comments
 (0)