|
| 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.Text; |
| 25 | +using System.Diagnostics.CodeAnalysis; |
| 26 | +using System.IO; |
| 27 | +using System.Net.Sockets; |
| 28 | + |
| 29 | +using Xunit; |
| 30 | + |
| 31 | +using RipcordSoftware.HttpWebClient; |
| 32 | + |
| 33 | +namespace HttpWebClient.UnitTests |
| 34 | +{ |
| 35 | + [Collection("HttpWebClientContainer")] |
| 36 | + [ExcludeFromCodeCoverage] |
| 37 | + public class TestHttpWebClientRequest |
| 38 | + { |
| 39 | + public class TestSocket : IHttpWebClientSocket |
| 40 | + { |
| 41 | + private readonly MemoryStream _responseStream; |
| 42 | + private readonly MemoryStream _requestStream; |
| 43 | + private readonly StringBuilder _requestText; |
| 44 | + |
| 45 | + public TestSocket(string host, int port, int timeout, StringBuilder requestText, string responseText) |
| 46 | + { |
| 47 | + _requestText = requestText; |
| 48 | + _requestStream = new MemoryStream(); |
| 49 | + _responseStream = new MemoryStream(Encoding.ASCII.GetBytes(responseText)); |
| 50 | + } |
| 51 | + |
| 52 | + public bool Connected => true; |
| 53 | + |
| 54 | + public int Available => (int)(_responseStream.Length - _responseStream.Position); |
| 55 | + |
| 56 | + public int Timeout { get; set; } |
| 57 | + public bool NoDelay { get; set; } |
| 58 | + public bool ForceClose { protected get; set; } |
| 59 | + |
| 60 | + public IntPtr Handle => throw new NotImplementedException(); |
| 61 | + |
| 62 | + public void Close() { } |
| 63 | + |
| 64 | + public void Dispose() { } |
| 65 | + |
| 66 | + public void Flush() { } |
| 67 | + |
| 68 | + public void KeepAliveOnClose(int? timeout = default(int?)) { } |
| 69 | + |
| 70 | + public int Receive(byte[] buffer, int offset, int count, bool peek = false, SocketFlags flags = SocketFlags.None) |
| 71 | + { |
| 72 | + var read = _responseStream.Read(buffer, offset, count); |
| 73 | + if (read > 0 && peek) |
| 74 | + { |
| 75 | + _responseStream.Position -= read; |
| 76 | + } |
| 77 | + |
| 78 | + return read; |
| 79 | + } |
| 80 | + |
| 81 | + public int Send(byte[] buffer, int offset, int count, SocketFlags flags = SocketFlags.None) |
| 82 | + { |
| 83 | + _requestText.Append(Encoding.ASCII.GetString(buffer, offset, count)); |
| 84 | + |
| 85 | + _requestStream.Write(buffer, offset, count); |
| 86 | + return count; |
| 87 | + } |
| 88 | + |
| 89 | + public string RequestText => _requestText.ToString(); |
| 90 | + } |
| 91 | + |
| 92 | + public TestHttpWebClientRequest() |
| 93 | + { |
| 94 | + HttpWebClientContainer.Clear(); |
| 95 | + } |
| 96 | + |
| 97 | + [Fact] |
| 98 | + public void TestRequestHeadersFromUrl() |
| 99 | + { |
| 100 | + var request = new HttpWebClientRequest("http://localhost:42/uri"); |
| 101 | + Assert.Equal("localhost", request.Headers.Hostname); |
| 102 | + Assert.Equal(42, request.Headers.Port); |
| 103 | + Assert.Equal("/uri", request.Headers.Uri); |
| 104 | + Assert.False(request.Headers.Secure); |
| 105 | + Assert.Equal("GET", request.Headers.Method); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public void TestRequestHeadersFromSecureUrl() |
| 110 | + { |
| 111 | + var request = new HttpWebClientRequest("https://www.test.com/hello/world"); |
| 112 | + Assert.Equal("www.test.com", request.Headers.Hostname); |
| 113 | + Assert.Equal(443, request.Headers.Port); |
| 114 | + Assert.Equal("/hello/world", request.Headers.Uri); |
| 115 | + Assert.True(request.Headers.Secure); |
| 116 | + Assert.Equal("GET", request.Headers.Method); |
| 117 | + } |
| 118 | + |
| 119 | + [Fact] |
| 120 | + public void TestSimpleRequestResponse() |
| 121 | + { |
| 122 | + var requestText = new StringBuilder(); |
| 123 | + var responseText = "HTTP/1.1 200 OK\r\nContent-Length: 11\r\nContent-Type: text/ascii\r\nConnection: close\r\n\r\nhello world"; |
| 124 | + |
| 125 | + HttpWebClientContainer.Register<IHttpWebClientSocket>((h, p, t) => { return new TestSocket((string)h, (int)p, (int)t, requestText, responseText); }); |
| 126 | + |
| 127 | + var request = new HttpWebClientRequest("http://www.test.com/hello/world"); |
| 128 | + using (var response = request.GetResponse()) |
| 129 | + { |
| 130 | + Assert.Equal(200, response.StatusCode); |
| 131 | + Assert.Equal("OK", response.StatusDescription); |
| 132 | + Assert.Equal(11, response.ContentLength); |
| 133 | + Assert.Equal("text/ascii", response.ContentType); |
| 134 | + Assert.Null(response.TransferEncoding); |
| 135 | + Assert.Null(response.KeepAlive); |
| 136 | + Assert.Null(response.KeepAliveTimeout); |
| 137 | + Assert.Equal("close", response.Connection); |
| 138 | + |
| 139 | + using (var responseStream = response.GetResponseStream()) |
| 140 | + { |
| 141 | + using (var stream = new StreamReader(responseStream)) |
| 142 | + { |
| 143 | + var body = stream.ReadToEnd(); |
| 144 | + Assert.Equal("hello world", body); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + Assert.True(requestText.Length > 0); |
| 150 | + Assert.Equal("GET /hello/world HTTP/1.1\r\nHost: www.test.com:80\r\nUser-Agent: Mozilla/5.0 RSHttpWebClient/1.0\r\nAccept: */*\r\nAccept-Encoding: gzip, deflate\r\n\r\n", requestText.ToString()); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments