From 5f1b5415ec06bddddcd3460de5a1e084e423e61b Mon Sep 17 00:00:00 2001 From: Kaleb Sturgill Date: Tue, 11 Mar 2014 14:01:26 -0700 Subject: [PATCH 1/2] Adding an interface, IOverrideInputModelForWhenValidationFails, that can be applied to a validating input model to override the resulting input model. This helps when your input models are not the same --- ...buMVC.Validation.IntegrationTesting.csproj | 2 + .../IntegratedLoFi_diff_models_Endpoint.cs | 37 ++++++++++++++++ .../IntegratedLoFi_diff_models_Tester.cs | 44 +++++++++++++++++++ .../ValidationActionFilter.cs | 15 +++++-- 4 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 src/FubuMVC.Validation.IntegrationTesting/LoFi_diff_models/IntegratedLoFi_diff_models_Endpoint.cs create mode 100644 src/FubuMVC.Validation.IntegrationTesting/LoFi_diff_models/IntegratedLoFi_diff_models_Tester.cs diff --git a/src/FubuMVC.Validation.IntegrationTesting/FubuMVC.Validation.IntegrationTesting.csproj b/src/FubuMVC.Validation.IntegrationTesting/FubuMVC.Validation.IntegrationTesting.csproj index 415c7de..fc1d268 100644 --- a/src/FubuMVC.Validation.IntegrationTesting/FubuMVC.Validation.IntegrationTesting.csproj +++ b/src/FubuMVC.Validation.IntegrationTesting/FubuMVC.Validation.IntegrationTesting.csproj @@ -136,6 +136,8 @@ + + diff --git a/src/FubuMVC.Validation.IntegrationTesting/LoFi_diff_models/IntegratedLoFi_diff_models_Endpoint.cs b/src/FubuMVC.Validation.IntegrationTesting/LoFi_diff_models/IntegratedLoFi_diff_models_Endpoint.cs new file mode 100644 index 0000000..8b437cc --- /dev/null +++ b/src/FubuMVC.Validation.IntegrationTesting/LoFi_diff_models/IntegratedLoFi_diff_models_Endpoint.cs @@ -0,0 +1,37 @@ +using FubuValidation; + +namespace FubuMVC.Validation.IntegrationTesting.LoFi_diff_models +{ + public class IntegratedLoFi_diff_models_Endpoint + { + public const string GET = "Input data"; + public const string SUCCESS = "Success"; + + public string get_lofi(LoFiInput_get input) + { + return GET; + } + + public string post_lofi(LoFiInput_post input) + { + return SUCCESS; + } + } + + public class LoFiInput_get + { + public string Name { get; set; } + } + + + public class LoFiInput_post : IOverrideInputModelForWhenValidationFails + { + [Required] + public string Name { get; set; } + + public object GetInputModelForValidationFail(LoFiInput_post inputModel) + { + return new LoFiInput_get(){Name = inputModel.Name}; + } + } +} \ No newline at end of file diff --git a/src/FubuMVC.Validation.IntegrationTesting/LoFi_diff_models/IntegratedLoFi_diff_models_Tester.cs b/src/FubuMVC.Validation.IntegrationTesting/LoFi_diff_models/IntegratedLoFi_diff_models_Tester.cs new file mode 100644 index 0000000..eca30c8 --- /dev/null +++ b/src/FubuMVC.Validation.IntegrationTesting/LoFi_diff_models/IntegratedLoFi_diff_models_Tester.cs @@ -0,0 +1,44 @@ +using FubuMVC.Core.Endpoints; +using FubuMVC.Validation.IntegrationTesting.LoFi_diff_models; +using FubuTestingSupport; +using NUnit.Framework; + +namespace FubuMVC.Validation.IntegrationTesting.LoFi +{ + [TestFixture] + public class IntegratedLoFi_diff_models_Tester : ValidationHarness + { + private LoFiInput_post postInputModel; + + [SetUp] + public void SetUp() + { + postInputModel = new LoFiInput_post(); + } + + protected override void configure(Core.FubuRegistry registry) + { + registry.Actions.IncludeType(); + registry.Import(); + } + + private HttpResponse theResponse + { + get { return endpoints.PostAsForm(postInputModel); } + } + + [Test] + public void output_from_endpoint_if_validation_succeeds() + { + postInputModel.Name = "Josh"; + theResponse.ReadAsText().ShouldEqual(IntegratedLoFi_diff_models_Endpoint.SUCCESS); + } + + [Test] + public void redirects_to_get_if_validation_fails() + { + postInputModel.Name = null; + theResponse.ReadAsText().ShouldEqual(IntegratedLoFi_diff_models_Endpoint.GET); + } + } +} \ No newline at end of file diff --git a/src/FubuMVC.Validation/ValidationActionFilter.cs b/src/FubuMVC.Validation/ValidationActionFilter.cs index 009c3ff..b438c9e 100644 --- a/src/FubuMVC.Validation/ValidationActionFilter.cs +++ b/src/FubuMVC.Validation/ValidationActionFilter.cs @@ -23,11 +23,16 @@ public static ActionFilter ValidationFor(Expression> method) } } + public interface IOverrideInputModelForWhenValidationFails + { + object GetInputModelForValidationFail(T inputModel); + } + public class ValidationActionFilter { private readonly IValidationFilter _filter; private readonly IFubuRequest _request; - + public ValidationActionFilter(IValidationFilter filter, IFubuRequest request) { _filter = filter; @@ -41,9 +46,13 @@ public FubuContinuation Validate(T input) { return FubuContinuation.NextBehavior(); } - _request.Set(notification); - return FubuContinuation.TransferTo(input, categoryOrHttpMethod: "GET"); + + var inputAsOverride = input as IOverrideInputModelForWhenValidationFails; + if (inputAsOverride == null) return FubuContinuation.TransferTo(input, categoryOrHttpMethod: "GET"); + + var typeWithAttributeValue = inputAsOverride.GetInputModelForValidationFail(input); + return FubuContinuation.TransferTo(typeWithAttributeValue, categoryOrHttpMethod: "GET"); } } } \ No newline at end of file From 436c1db49a61838e42537070924a965fa953f20d Mon Sep 17 00:00:00 2001 From: Kaleb Sturgill Date: Thu, 13 Mar 2014 07:49:18 -0700 Subject: [PATCH 2/2] changed the return type of the override method to be a FubuContinuation and a constraint to make sure it is only a TransferTo Continuation. --- .../IntegratedLoFi_diff_models_Endpoint.cs | 7 ++++--- src/FubuMVC.Validation/ValidationActionFilter.cs | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/FubuMVC.Validation.IntegrationTesting/LoFi_diff_models/IntegratedLoFi_diff_models_Endpoint.cs b/src/FubuMVC.Validation.IntegrationTesting/LoFi_diff_models/IntegratedLoFi_diff_models_Endpoint.cs index 8b437cc..1b8a6e7 100644 --- a/src/FubuMVC.Validation.IntegrationTesting/LoFi_diff_models/IntegratedLoFi_diff_models_Endpoint.cs +++ b/src/FubuMVC.Validation.IntegrationTesting/LoFi_diff_models/IntegratedLoFi_diff_models_Endpoint.cs @@ -1,4 +1,5 @@ -using FubuValidation; +using FubuMVC.Core.Continuations; +using FubuValidation; namespace FubuMVC.Validation.IntegrationTesting.LoFi_diff_models { @@ -29,9 +30,9 @@ public class LoFiInput_post : IOverrideInputModelForWhenValidationFails(Expression> method) public interface IOverrideInputModelForWhenValidationFails { - object GetInputModelForValidationFail(T inputModel); + FubuContinuation GetFubuContinuationForWhenValidationFails(T inputModel); } public class ValidationActionFilter @@ -51,8 +51,9 @@ public FubuContinuation Validate(T input) var inputAsOverride = input as IOverrideInputModelForWhenValidationFails; if (inputAsOverride == null) return FubuContinuation.TransferTo(input, categoryOrHttpMethod: "GET"); - var typeWithAttributeValue = inputAsOverride.GetInputModelForValidationFail(input); - return FubuContinuation.TransferTo(typeWithAttributeValue, categoryOrHttpMethod: "GET"); + var continuation = inputAsOverride.GetFubuContinuationForWhenValidationFails(input); + if (continuation.Type != ContinuationType.Transfer) throw new Exception("The overriden FubuContinuation must be a TransferTo to give validation details."); + return continuation; } } } \ No newline at end of file