public
Created

Specs for a Fubu MVC behavior that controls which HTTP methods are allowed for a controller action

  • Download Gist
RequestTypeBehaviorSpecs.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
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));
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.