forked from tomkuijsten/restup
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request tomkuijsten#98 from Jark/feature-add-constructor-a…
…rguments-validation Add validation around constructor args.
- Loading branch information
Showing
14 changed files
with
220 additions
and
55 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
src/WebServer.UnitTests/Rest/RestRouteHandlerTests.ControllerConstructor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Restup.Webserver.UnitTests.TestHelpers; | ||
|
||
namespace Restup.Webserver.UnitTests.Rest | ||
{ | ||
[TestClass] | ||
public class RestRouteHandlerTests_ControllerConstructor : RestRouteHandlerTests | ||
{ | ||
private class ControllerWithOneStringParameter | ||
{ | ||
public ControllerWithOneStringParameter(string param) | ||
{ | ||
} | ||
} | ||
|
||
private class ControllerWithOneStringParameterAndOneIntegerParameter | ||
{ | ||
public ControllerWithOneStringParameterAndOneIntegerParameter(string param, int param2) | ||
{ | ||
} | ||
} | ||
|
||
private class ControllerWithPrivateConstructor | ||
{ | ||
private ControllerWithPrivateConstructor() | ||
{ | ||
} | ||
} | ||
|
||
private class ControllerWithTwoConstructors | ||
{ | ||
public ControllerWithTwoConstructors() | ||
{ | ||
} | ||
|
||
public ControllerWithTwoConstructors(string param) | ||
{ | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void RegisterController_WithConstructerWithAParameterAndNoParamIsPassedIn_ThenExceptionIsThrown() | ||
{ | ||
AssertRegisterControllerThrows<ControllerWithOneStringParameter>(); | ||
} | ||
|
||
[TestMethod] | ||
public void RegisterController_WithConstructerWithAStringParameterAndAnIntegerIsPassedIn_ThenExceptionIsThrown() | ||
{ | ||
AssertRegisterControllerThrows<ControllerWithOneStringParameter>(1); | ||
} | ||
|
||
[TestMethod] | ||
public void RegisterController_WithConstructerWithAParameterAndMoreParamsArePassedIn_ThenExceptionIsThrown() | ||
{ | ||
AssertRegisterControllerThrows<ControllerWithOneStringParameter>("param1", "param2"); | ||
} | ||
|
||
[TestMethod] | ||
public void RegisterController_WithConstructerWithAParameterAndTheCorrectParamsArePassedIn_ThenNoExceptionIsThrown() | ||
{ | ||
_restRouteHandler.RegisterController<ControllerWithOneStringParameter>(() => new object[] { "param1" }); | ||
Assert.IsTrue(true); | ||
} | ||
|
||
[TestMethod] | ||
public void RegisterController_WithPrivateConstructer_ThenExceptionIsThrown() | ||
{ | ||
AssertRegisterControllerThrows<ControllerWithPrivateConstructor>(() => new object[] { "param1" }); | ||
} | ||
|
||
[TestMethod] | ||
public void RegisterController_WithTwoConstructersAndFuncIsUsed_ThenExceptionIsThrown() | ||
{ | ||
AssertRegisterControllerThrows<ControllerWithTwoConstructors>(() => new object[] { "param1" }); | ||
} | ||
|
||
[TestMethod] | ||
public void RegisterController_WithTwoConstructersAndInstantiatedObjectIsUsed_ThenNoExceptionIsThrown() | ||
{ | ||
_restRouteHandler.RegisterController<ControllerWithTwoConstructors>("param1"); | ||
Assert.IsTrue(true); | ||
} | ||
|
||
[TestMethod] | ||
public void RegisterController_WithConstructerWithTwoParametersAndLessParamsArePassedIn_ThenExceptionIsThrown() | ||
{ | ||
AssertRegisterControllerThrows<ControllerWithOneStringParameterAndOneIntegerParameter>("param1"); | ||
} | ||
|
||
[TestMethod] | ||
public void RegisterController_WithConstructerWithTwoParametersAndTheCorrectParamsArePassedIn_ThenNoExceptionIsThrown() | ||
{ | ||
_restRouteHandler.RegisterController<ControllerWithOneStringParameterAndOneIntegerParameter>("param1", 1); | ||
Assert.IsTrue(true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/WebServer.UnitTests/TestHelpers/RestRouteHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Restup.Webserver.Rest; | ||
|
||
namespace Restup.Webserver.UnitTests.TestHelpers | ||
{ | ||
public abstract class RestRouteHandlerTests | ||
{ | ||
protected RestRouteHandler _restRouteHandler; | ||
|
||
[TestInitialize] | ||
public void Initialize() | ||
{ | ||
_restRouteHandler = new RestRouteHandler(); | ||
} | ||
|
||
protected void AssertRegisterControllerThrows<T>(params object[] args) where T : class | ||
{ | ||
Assert.ThrowsException<Exception>(() => | ||
_restRouteHandler.RegisterController<T>(args) | ||
); | ||
} | ||
|
||
protected void AssertRegisterControllerThrows<T>(Func<object[]> args) where T : class | ||
{ | ||
Assert.ThrowsException<Exception>(() => | ||
_restRouteHandler.RegisterController<T>(args) | ||
); | ||
} | ||
|
||
protected void AssertRegisterControllerThrows<T>() where T : class | ||
{ | ||
Assert.ThrowsException<Exception>(() => | ||
_restRouteHandler.RegisterController<T>() | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
using Restup.Webserver.Models.Contracts; | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace Restup.Webserver.InstanceCreators | ||
{ | ||
internal class PerCallInstanceCreator : IInstanceCreator | ||
{ | ||
public object Create(Type instanceType, params object[] args) | ||
public object Create(ConstructorInfo instanceType, object[] args) | ||
{ | ||
return Activator.CreateInstance(instanceType, args); | ||
return instanceType.Invoke(args); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace Restup.Webserver.Models.Contracts | ||
{ | ||
interface IInstanceCreator | ||
{ | ||
object Create(Type instanceType, object[] args); | ||
object Create(ConstructorInfo instanceType, object[] args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Restup.Webserver.Rest | ||
{ | ||
internal static class ReflectionHelper | ||
{ | ||
internal static bool TryFindMatchingConstructor<T>(object[] args, out ConstructorInfo foundConstructor) | ||
{ | ||
foreach (var constructorInfo in typeof(T).GetConstructors()) | ||
{ | ||
var parameters = constructorInfo.GetParameters(); | ||
if (args.Length != parameters.Length) | ||
continue; | ||
|
||
var argsTypes = args.Select(x => x.GetType()); | ||
var parameterTypes = parameters.Select(x => x.ParameterType); | ||
if ( !argsTypes.SequenceEqual(parameterTypes)) | ||
continue; | ||
|
||
foundConstructor = constructorInfo; | ||
return true; | ||
} | ||
foundConstructor = null; | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.