Skip to content

Commit

Permalink
Added unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dicky authored and dicky committed Dec 30, 2017
1 parent 7bc5c95 commit 9ab1db8
Show file tree
Hide file tree
Showing 5 changed files with 470 additions and 14 deletions.
59 changes: 59 additions & 0 deletions ASP.NET Core Demo/UnitTests/HubDisconnectionMiddlewareTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Microsoft.AspNetCore.SignalR;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Threading.Tasks;
using DotNetify;

namespace UnitTests
{
[TestClass]
public class HubDisonnectionMiddlewareTest
{
private class TestVM : BaseVM
{
public string Property { get; set; }
}

private class CustomMiddleware : IDisconnectionMiddleware
{
public event EventHandler<HubCallerContext> Invoked;

public Task OnDisconnected(HubCallerContext context)
{
Invoked?.Invoke(this, context);
return Task.CompletedTask;
}
}

private CustomMiddleware _middleware;

[TestInitialize]
public void Initialize()
{
_middleware = new CustomMiddleware();
VMController.CreateInstance = (type, args) => type == typeof(CustomMiddleware) ? _middleware : Activator.CreateInstance(type, args);
}

[TestCleanup]
public void Cleanup()
{
VMController.CreateInstance = (type, args) => Activator.CreateInstance(type, args);
}

[TestMethod]
public void DisconnectionMiddleware_DisconnectionIntercepted()
{
VMController.Register<TestVM>();
var hub = new MockDotNetifyHub()
.UseMiddleware<CustomMiddleware>()
.Create();

bool middlewareInvoked = false;
_middleware.Invoked += (sender, e) => middlewareInvoked = true;

hub.RequestVM(nameof(TestVM));
hub.OnDisconnected();
Assert.IsTrue(middlewareInvoked);
}
}
}
23 changes: 23 additions & 0 deletions ASP.NET Core Demo/UnitTests/HubExceptionMiddlewareTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ public Task<Exception> OnException(HubCallerContext context, Exception exception
}
}

private class OperationCancelledMiddleware : IMiddleware
{
public Task Invoke(DotNetifyHubContext context, NextDelegate next)
{
throw new OperationCanceledException();
}
}

[TestMethod]
public void ExceptionMiddleware_ExceptionOnRequest_NotIntercepted()
{
Expand Down Expand Up @@ -151,5 +159,20 @@ public void ExceptionMiddleware_ExceptionOnDispose_ExceptionIntercepted()

throw ExceptionOnDisposeMiddleware.Exception;
}

[TestMethod]
public void ExceptionMiddleware_OperationCancelled_NoResponse()
{
VMController.Register<ExceptionOnDisposeVM>();
var hub = new MockDotNetifyHub()
.UseMiddleware<OperationCancelledMiddleware>()
.Create();

bool responseSent = false;
hub.Response += (sender, e) => responseSent = true;

hub.RequestVM(nameof(ExceptionOnDisposeVM));
Assert.IsFalse(responseSent);
}
}
}
28 changes: 14 additions & 14 deletions ASP.NET Core Demo/UnitTests/HubMiddlewareTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace UnitTests
[TestClass]
public class HubMiddlewareTest
{
private class TestVM : BaseVM
private class MiddlewareTestVM : BaseVM
{
public string Property { get; set; } = "Hello";
}
Expand Down Expand Up @@ -82,7 +82,7 @@ public void Cleanup()
[TestMethod]
public void Middleware_RequestIntercepted()
{
VMController.Register<TestVM>();
VMController.Register<MiddlewareTestVM>();
var hub = new MockDotNetifyHub()
.UseMiddleware<ExtractHeadersMiddleware>()
.UseMiddleware<JwtBearerAuthenticationMiddleware>()
Expand All @@ -103,7 +103,7 @@ public void Middleware_RequestIntercepted()
{
var callType = context.CallType;
var connectionId = context.CallerContext.ConnectionId;
var testVMPropValue = (context.Data as JObject)[nameof(TestVM.Property)];
var testVMPropValue = (context.Data as JObject)[nameof(MiddlewareTestVM.Property)];
var authToken = (context.Headers as JObject)["Authorization"].ToString();
var principalName = context.Principal.Identity.Name;
var pipelineData = new Dictionary<string, object>(context.PipelineData);
Expand All @@ -124,7 +124,7 @@ public void Middleware_RequestIntercepted()
{
var callType = context.CallType;
var connectionId = context.CallerContext.ConnectionId;
var testVMPropValue = (context.Data as JObject)[nameof(TestVM.Property)];
var testVMPropValue = (context.Data as JObject)[nameof(MiddlewareTestVM.Property)];
var authToken = (context.Headers as JObject)["Authorization"].ToString();
var principalName = context.Principal.Identity.Name;
var pipelineData = new Dictionary<string, object>(context.PipelineData);
Expand All @@ -140,9 +140,9 @@ public void Middleware_RequestIntercepted()
};
};

hub.RequestVM(nameof(TestVM), _vmArg);
hub.RequestVM(nameof(MiddlewareTestVM), _vmArg);

Assert.AreEqual(nameof(TestVM), vmName);
Assert.AreEqual(nameof(MiddlewareTestVM), vmName);
Assert.AreEqual("World", vmData.Property.ToString());
middleware1Assertions();
middleware2Assertions();
Expand All @@ -151,7 +151,7 @@ public void Middleware_RequestIntercepted()
[TestMethod]
public void Middleware_UpdateIntercepted()
{
VMController.Register<TestVM>();
VMController.Register<MiddlewareTestVM>();
var hub = new MockDotNetifyHub()
.UseMiddleware<ExtractHeadersMiddleware>()
.UseMiddleware<JwtBearerAuthenticationMiddleware>()
Expand All @@ -166,7 +166,7 @@ public void Middleware_UpdateIntercepted()
return;

var connectionId = context.CallerContext.ConnectionId;
var testVMPropValue = (context.Data as Dictionary<string, object>)[nameof(TestVM.Property)];
var testVMPropValue = (context.Data as Dictionary<string, object>)[nameof(MiddlewareTestVM.Property)];
var authToken = (context.Headers as JObject)["Authorization"].ToString();
var principalName = context.Principal.Identity.Name;
var pipelineData = new Dictionary<string, object>(context.PipelineData);
Expand All @@ -188,7 +188,7 @@ public void Middleware_UpdateIntercepted()
return;

var connectionId = context.CallerContext.ConnectionId;
var testVMPropValue = (context.Data as Dictionary<string, object>)[nameof(TestVM.Property)];
var testVMPropValue = (context.Data as Dictionary<string, object>)[nameof(MiddlewareTestVM.Property)];
var authToken = (context.Headers as JObject)["Authorization"].ToString();
var principalName = context.Principal.Identity.Name;
var pipelineData = new Dictionary<string, object>(context.PipelineData);
Expand All @@ -203,8 +203,8 @@ public void Middleware_UpdateIntercepted()
};
};

hub.RequestVM(nameof(TestVM), _vmArg);
hub.UpdateVM(nameof(TestVM), new Dictionary<string, object> { { "Property", "Goodbye" } });
hub.RequestVM(nameof(MiddlewareTestVM), _vmArg);
hub.UpdateVM(nameof(MiddlewareTestVM), new Dictionary<string, object> { { "Property", "Goodbye" } });

middleware1Assertions();
middleware2Assertions();
Expand All @@ -213,7 +213,7 @@ public void Middleware_UpdateIntercepted()
[TestMethod]
public void Middleware_DisposeIntercepted()
{
VMController.Register<TestVM>();
VMController.Register<MiddlewareTestVM>();
var hub = new MockDotNetifyHub()
.UseMiddleware<ExtractHeadersMiddleware>()
.UseMiddleware<JwtBearerAuthenticationMiddleware>()
Expand Down Expand Up @@ -261,8 +261,8 @@ public void Middleware_DisposeIntercepted()
};
};

hub.RequestVM(nameof(TestVM), _vmArg);
hub.DisposeVM(nameof(TestVM));
hub.RequestVM(nameof(MiddlewareTestVM), _vmArg);
hub.DisposeVM(nameof(MiddlewareTestVM));

middleware1Assertions();
middleware2Assertions();
Expand Down
Loading

0 comments on commit 9ab1db8

Please sign in to comment.