Created
January 14, 2012 04:04
-
-
Save agross/1610214 to your computer and use it in GitHub Desktop.
Specs for a Fubu MVC behavior that controls which HTTP methods are allowed for a controller action
This file contains hidden or 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
using System; | |
using System.Net; | |
using Crimson.Web.Behaviors; | |
using Crimson.Web.Services; | |
using FubuMVC.Core.Behaviors; | |
using Machine.Specifications; | |
using Rhino.Mocks; | |
namespace Crimson.Web.Tests.Behaviors | |
{ | |
public abstract class RequestTypeBehaviorSpecs | |
{ | |
static ICurrentHttpRequest Request; | |
static IControllerActionInspector ControllerActionInspector; | |
protected static object InModel; | |
protected static Func<object, object> ControllerAction; | |
protected static IResponseStatusService ResponseStatus; | |
protected static IControllerActionBehavior CreateBehavior(HttpVerbs requiredVerb, HttpVerbs requestVerb) | |
{ | |
ControllerAction = MockRepository.GenerateStub<Func<object, object>>(); | |
InModel = new object(); | |
ControllerActionInspector = MockRepository.GenerateStub<IControllerActionInspector>(); | |
ControllerActionInspector | |
.Stub(x => x.GetRequiredRequestVerb()) | |
.Return(requiredVerb); | |
Request = MockRepository.GenerateStub<ICurrentHttpRequest>(); | |
Request | |
.Stub(x => x.Verb) | |
.Return(requestVerb); | |
ResponseStatus = MockRepository.GenerateStub<IResponseStatusService>(); | |
return new Allow_only_valid_HTTP_verbs_for_controller_actions(ControllerActionInspector, Request, ResponseStatus) | |
{ InsideBehavior = MockRepository.GenerateStub<IControllerActionBehavior>() }; | |
} | |
} | |
[Subject(typeof(Allow_only_valid_HTTP_verbs_for_controller_actions))] | |
public class When_a_controller_action_that_requires_HTTP_POST_is_requested_via_HTTP_GET : RequestTypeBehaviorSpecs | |
{ | |
static IControllerActionBehavior Behavior; | |
Establish context = () => { Behavior = CreateBehavior(HttpVerbs.Post, HttpVerbs.Get); }; | |
Because of = () => Behavior.Invoke(InModel, ControllerAction); | |
It should_refuse_the_request = | |
() => ResponseStatus.Status.ShouldEqual((int) HttpStatusCode.MethodNotAllowed); | |
It should_not_proceed_with_the_request = | |
() => Behavior.InsideBehavior.AssertWasNotCalled(x => x.Invoke(InModel, ControllerAction)); | |
} | |
[Subject(typeof(Allow_only_valid_HTTP_verbs_for_controller_actions))] | |
public class When_a_controller_action_that_requires_HTTP_GET_is_requested_via_HTTP_POST : RequestTypeBehaviorSpecs | |
{ | |
static IControllerActionBehavior Behavior; | |
Establish context = () => { Behavior = CreateBehavior(HttpVerbs.Get, HttpVerbs.Post); }; | |
Because of = () => Behavior.Invoke(InModel, ControllerAction); | |
It should_refuse_the_request = | |
() => ResponseStatus.Status.ShouldEqual((int) HttpStatusCode.MethodNotAllowed); | |
It should_not_proceed_with_the_request = | |
() => Behavior.InsideBehavior.AssertWasNotCalled(x => x.Invoke(InModel, ControllerAction)); | |
} | |
[Subject(typeof(Allow_only_valid_HTTP_verbs_for_controller_actions))] | |
public class When_a_controller_action_that_requires_HTTP_POST_is_requested_via_HTTP_POST : RequestTypeBehaviorSpecs | |
{ | |
static IControllerActionBehavior Behavior; | |
Establish context = () => { Behavior = CreateBehavior(HttpVerbs.Post, HttpVerbs.Post); }; | |
Because of = () => Behavior.Invoke(InModel, ControllerAction); | |
It should_proceed_with_the_request = | |
() => Behavior.InsideBehavior.AssertWasCalled(x => x.Invoke(InModel, ControllerAction)); | |
} | |
[Subject(typeof(Allow_only_valid_HTTP_verbs_for_controller_actions))] | |
public class When_a_controller_action_that_requires_HTTP_GET_is_requested_via_HTTP_GET : RequestTypeBehaviorSpecs | |
{ | |
static IControllerActionBehavior Behavior; | |
Establish context = () => { Behavior = CreateBehavior(HttpVerbs.Get, HttpVerbs.Get); }; | |
Because of = () => Behavior.Invoke(InModel, ControllerAction); | |
It should_proceed_with_the_request = | |
() => Behavior.InsideBehavior.AssertWasCalled(x => x.Invoke(InModel, ControllerAction)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment