Permalink
Browse files

Added unit tests for hosting namespace and configuration manager.

  • Loading branch information...
1 parent 5ad61b3 commit 4b5ff239da78dc726349c28846c0a1ca5b4f6cfa @davidfowl davidfowl committed Oct 30, 2012
@@ -0,0 +1,21 @@
+using Xunit;
+
+namespace Microsoft.AspNet.SignalR.Tests
+{
+ public class ConfigurationManagerFacts
+ {
+ [Fact]
+ public void DefaultValues()
+ {
+ // Arrange
+ var config = new DefaultConfigurationManager();
+
+ // Assert
+ Assert.Equal(config.ConnectionTimeout.TotalSeconds, 110);
+ Assert.Equal(config.DisconnectTimeout.TotalSeconds, 40);
+ Assert.Equal(config.HeartBeatInterval.TotalSeconds, 10);
+ Assert.NotNull(config.KeepAlive);
+ Assert.Equal(config.KeepAlive.Value.TotalSeconds, 30);
+ }
+ }
+}
@@ -0,0 +1,20 @@
+using Xunit;
+
+namespace Microsoft.AspNet.SignalR.Tests.Hosting
+{
+ public class CookieFacts
+ {
+ [Fact]
+ public void CookiePropertiesAreSetWhenInitializedThroughCtor()
+ {
+ // Arrange
+ var cookie = new Cookie("name", "value", "www.foo.com", "/");
+
+ // Assert
+ Assert.Equal("name", cookie.Name);
+ Assert.Equal("value", cookie.Value);
+ Assert.Equal("www.foo.com", cookie.Domain);
+ Assert.Equal("/", cookie.Path);
+ }
+ }
+}
@@ -0,0 +1,32 @@
+using System.Threading;
+using Moq;
+using Xunit;
+
+namespace Microsoft.AspNet.SignalR.Tests.Hosting
+{
+ public class HostContextExtensionsFacts
+ {
+ [Fact]
+ public void ExtensionMethodsMapToDictionaryEntries()
+ {
+ // Arrange
+ var request = new Mock<IRequest>();
+ var response = new Mock<IResponse>();
+ var context = new HostContext(request.Object, response.Object);
+
+ // Act
+ context.Items[HostConstants.DebugMode] = true;
+ context.Items[HostConstants.InstanceName] = "Instance name";
+ context.Items[HostConstants.ShutdownToken] = CancellationToken.None;
+ context.Items[HostConstants.SupportsWebSockets] = false;
+ context.Items[HostConstants.WebSocketServerUrl] = "ws://123";
+
+ // Assert
+ Assert.True(context.IsDebuggingEnabled());
+ Assert.Equal("Instance name", context.InstanceName());
+ Assert.Equal(CancellationToken.None, context.HostShutdownToken());
+ Assert.False(context.SupportsWebSockets());
+ Assert.Equal("ws://123", context.WebSocketServerUrl());
+ }
+ }
+}
@@ -0,0 +1,65 @@
+using System.Collections.Specialized;
+using Moq;
+using Xunit;
+
+namespace Microsoft.AspNet.SignalR.Tests.Hosting
+{
+ public class RequestExtensionsFacts
+ {
+ public class QueryStringOrForm
+ {
+ [Fact]
+ public void GetsValueFromFormIfQueryStringIsNull()
+ {
+ // Arrange
+ var request = new Mock<IRequest>();
+ request.Setup(m => m.QueryString).Returns(new NameValueCollection());
+ var form = new NameValueCollection();
+ form["value"] = "1";
+ request.Setup(m => m.Form).Returns(form);
+
+ // Act
+ string value = request.Object.QueryStringOrForm("value");
+
+ // Assert
+ Assert.Equal("1", value);
+ }
+
+ [Fact]
+ public void GetsValueQueryStringIfNotNull()
+ {
+ // Arrange
+ var request = new Mock<IRequest>();
+ var qs = new NameValueCollection();
+ qs["value"] = "1";
+ request.Setup(m => m.QueryString).Returns(qs);
+ request.Setup(m => m.Form).Returns(new NameValueCollection());
+
+ // Act
+ string value = request.Object.QueryStringOrForm("value");
+
+ // Assert
+ Assert.Equal("1", value);
+ }
+
+ [Fact]
+ public void GetsValueQueryStringIfInBoth()
+ {
+ // Arrange
+ var request = new Mock<IRequest>();
+ var qs = new NameValueCollection();
+ qs["value"] = "1";
+ request.Setup(m => m.QueryString).Returns(qs);
+ var form = new NameValueCollection();
+ form["value"] = "2";
+ request.Setup(m => m.Form).Returns(form);
+
+ // Act
+ string value = request.Object.QueryStringOrForm("value");
+
+ // Assert
+ Assert.Equal("1", value);
+ }
+ }
+ }
+}
@@ -0,0 +1,53 @@
+using System;
+using System.IO;
+using System.Text;
+using Moq;
+using Xunit;
+
+namespace Microsoft.AspNet.SignalR.Tests.Hosting
+{
+ public class ResponseExtensionsFacts
+ {
+ [Fact]
+ public void WrapperStreamOnlyImplementsWrite()
+ {
+ // Arrange
+ var response = new Mock<IResponse>();
+ response.Setup(m => m.Write(It.IsAny<ArraySegment<byte>>())).Verifiable();
+ Stream stream = response.Object.AsStream();
+ var buffer = Encoding.UTF8.GetBytes("Hello");
+
+ // Act
+ stream.Write(buffer, 0, buffer.Length);
+
+ // Assert
+ Assert.False(stream.CanRead);
+ Assert.False(stream.CanSeek);
+ Assert.False(stream.CanTimeout);
+ Assert.True(stream.CanWrite);
+ response.VerifyAll();
+ }
+
+ [Fact]
+ public void EndAsyncWritesUtf8BufferToResponse()
+ {
+ // Arrange
+ var response = new Mock<IResponse>();
+ string value = null;
+ response.Setup(m => m.Write(It.IsAny<ArraySegment<byte>>()))
+ .Callback<ArraySegment<byte>>(data =>
+ {
+ value = Encoding.UTF8.GetString(data.Array, data.Offset, data.Count);
+ });
+
+ response.Setup(m => m.EndAsync()).Verifiable();
+
+ // Act
+ response.Object.EndAsync("Hello World");
+
+ // Assert
+ Assert.Equal("Hello World", value);
+ response.VerifyAll();
+ }
+ }
+}
@@ -56,6 +56,11 @@
</Compile>
<Compile Include="ChunkBufferFacts.cs" />
<Compile Include="Client\ConnectionFacts.cs" />
+ <Compile Include="ConfigurationManagerFacts.cs" />
+ <Compile Include="Hosting\CookieFacts.cs" />
+ <Compile Include="Hosting\HostContextExtensionsFacts.cs" />
+ <Compile Include="Hosting\RequestExtensionsFacts.cs" />
+ <Compile Include="Hosting\ResponseExtensionsFacts.cs" />
<Compile Include="Server\AckHandlerFacts.cs" />
<Compile Include="Server\Hubs\HubDispatcherFacts.cs" />
<Compile Include="Server\MessageResultExtensions.cs" />

0 comments on commit 4b5ff23

Please sign in to comment.