public void op_ToString_whenPost() { var expected = new StringBuilder(); expected.AppendLine("POST / HTTP/1.1"); expected.AppendLine("Content-Length: 4"); expected.AppendLine("Content-Type: text/plain; charset=UTF-8"); expected.AppendLine("Host: www.example.com"); expected.AppendLine("Connection: keep-alive"); expected.AppendLine(string.Empty); expected.Append("text"); string actual; try { var locator = new Mock <IServiceLocator>(); locator .Setup(e => e.GetInstance <IMediaType>("text/plain")) .Returns(new TextPlain()) .Verifiable(); ServiceLocator.SetLocatorProvider(() => locator.Object); actual = HttpRequest.FromString(expected.ToString()).ToString(); locator.VerifyAll(); } finally { ServiceLocator.SetLocatorProvider(null); } Assert.Equal(expected.ToString(), actual); }
public void op_Write_TextWriter_whenGet() { var expected = new StringBuilder(); expected.AppendLine("GET / HTTP/1.1"); expected.AppendLine("Host: www.example.com"); expected.AppendLine("Connection: close"); expected.AppendLine(string.Empty); var obj = HttpRequest.FromString(expected.ToString()); using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { obj.Write(writer); writer.Flush(); stream.Position = 0; using (var reader = new StreamReader(stream)) { Assert.Equal(expected.ToString(), reader.ReadToEnd()); } } } }
public void op_FromString_string_whenRequestLine() { const string line = "GET http://example.com/ HTTP/1.1"; var obj = HttpRequest.FromString(line); Assert.Equal(line, obj.Line); Assert.Empty(obj.Headers.ToList()); Assert.Null(obj.Body); }
public void op_ToString_whenGet() { var expected = new StringBuilder(); expected.AppendLine("GET / HTTP/1.1"); expected.AppendLine("Host: www.example.com"); expected.AppendLine("Connection: close"); expected.AppendLine(string.Empty); var actual = HttpRequest.FromString(expected.ToString()).ToString(); Assert.Equal(expected.ToString(), actual); }
public void prop_UserAgent_get() { const string expected = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"; var obj = HttpRequest.FromString("GET http://example.com/ HTTP/1.1"); Assert.Null(obj.UserAgent); obj.Headers.Add(new HttpHeader(HttpRequestHeaders.UserAgent, expected)); var actual = obj.UserAgent; Assert.Equal(expected, actual); }
public void prop_UserAgent_set() { const string expected = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"; var buffer = new StringBuilder(); buffer.AppendLine("GET http://example.com/ HTTP/1.1"); var obj = HttpRequest.FromString(buffer.ToString()); obj.UserAgent = expected; var actual = obj.UserAgent; Assert.Equal(expected, actual); }
public void op_Write_TextWriter_whenPost() { var expected = new StringBuilder(); expected.AppendLine("POST / HTTP/1.1"); expected.AppendLine("Content-Length: 4"); expected.AppendLine("Content-Type: text/plain; charset=UTF-8"); expected.AppendLine("Host: www.example.com"); expected.AppendLine("Connection: keep-alive"); expected.AppendLine(string.Empty); expected.Append("text"); HttpRequest obj; try { var locator = new Mock <IServiceLocator>(); locator .Setup(e => e.GetInstance <IMediaType>("text/plain")) .Returns(new TextPlain()) .Verifiable(); ServiceLocator.SetLocatorProvider(() => locator.Object); obj = HttpRequest.FromString(expected.ToString()); locator.VerifyAll(); } finally { ServiceLocator.SetLocatorProvider(null); } using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { obj.Write(writer); writer.Flush(); stream.Position = 0; using (var reader = new StreamReader(stream)) { Assert.Equal(expected.ToString(), reader.ReadToEnd()); } } } }
public void op_FromString_string_whenPost() { RequestLine requestLine = "POST / HTTP/1.1"; HttpHeader contentLength = "Content-Length: 4"; HttpHeader contentType = "Content-Type: text/plain; charset=UTF-8"; HttpHeader host = "Host: www.example.com"; HttpHeader connection = "Connection: keep-alive"; var value = new StringBuilder(); value.AppendLine(requestLine); value.AppendLine(contentLength); value.AppendLine(contentType); value.AppendLine(host); value.AppendLine(connection); value.AppendLine(string.Empty); value.Append("text"); HttpRequest obj; try { var locator = new Mock <IServiceLocator>(); locator .Setup(e => e.GetInstance <IMediaType>("text/plain")) .Returns(new TextPlain()) .Verifiable(); ServiceLocator.SetLocatorProvider(() => locator.Object); obj = HttpRequest.FromString(value.ToString()); locator.VerifyAll(); } finally { ServiceLocator.SetLocatorProvider(null); } Assert.Equal <string>(requestLine, obj.RequestLine); Assert.True(obj.Headers.Contains(contentLength)); Assert.True(obj.Headers.Contains(contentType)); Assert.True(obj.Headers.Contains(host)); Assert.True(obj.Headers.Contains(connection)); }
public void op_FromString_string_whenGet() { RequestLine requestLine = "GET / HTTP/1.1"; HttpHeader host = "Host: www.example.com"; HttpHeader connection = "Connection: close"; var value = new StringBuilder(); value.AppendLine(requestLine); value.AppendLine(host); value.AppendLine(connection); var obj = HttpRequest.FromString(value.ToString()); Assert.Equal <string>(requestLine, obj.RequestLine); Assert.True(obj.Headers.Contains(host)); Assert.True(obj.Headers.Contains(connection)); }
public void op_FromString_string_whenHeaders() { const string line = "GET http://example.com/ HTTP/1.1"; HttpHeader host = "Host: example.com"; HttpHeader ua = "User-Agent: Example"; var buffer = new StringBuilder(); buffer.AppendLine(line); buffer.AppendLine(host); buffer.AppendLine(ua); var obj = HttpRequest.FromString(buffer.ToString()); Assert.Equal(line, obj.Line); Assert.Equal(host, obj.Headers.First()); Assert.Equal(ua, obj.Headers.Last()); Assert.Null(obj.Body); }
public void op_FromString_string_whenHeadersAndBody() { const string line = "PUT http://example.com/ HTTP/1.1"; HttpHeader host = "Host: example.com"; HttpHeader type = "Content-Type: text/plain"; var buffer = new StringBuilder(); buffer.AppendLine(line); buffer.AppendLine(host); buffer.AppendLine(type); buffer.AppendLine(string.Empty); buffer.Append("example"); var obj = HttpRequest.FromString(buffer.ToString()); Assert.Equal(line, obj.Line); Assert.Equal(host, obj.Headers.First()); Assert.Equal(type, obj.Headers.Last()); Assert.Equal("example", ((TextBody)obj.Body).Text); }
public override IEnumerable <object[]> GetData(MethodInfo methodUnderTest, Type[] parameterTypes) { if (null == methodUnderTest) { throw new ArgumentNullException("methodUnderTest"); } if (null == parameterTypes) { throw new ArgumentNullException("parameterTypes"); } if (0 == parameterTypes.Length) { throw new InvalidOperationException("A parameter is required."); } if (1 != parameterTypes.Length) { throw new InvalidOperationException("Only one parameter is permitted."); } var list = new List <object>(); var info = new FileInfo(File); if (parameterTypes[0] == typeof(HttpRequest)) { list.Add(HttpRequest.FromString(info.ReadToEnd())); } else { throw new InvalidOperationException("Only Http Request is supported as a parameter type."); } yield return(list.ToArray()); }
public void op_FromString_stringNull() { Assert.Throws <ArgumentNullException>(() => HttpRequest.FromString(null)); }
public void op_FromString_stringEmpty() { Assert.Throws <ArgumentOutOfRangeException>(() => HttpRequest.FromString(string.Empty)); }