sargant
sargant

Reputation: 356

How to test that an MVC action is only accessible via HTTP POST?

I am using MvcContrib-TestHelper to test the routing on my app. I have an action which is restricted to HTTP POST only:

public TestController
{
    [HttpPost]
    public ActionResult Example()
    {
        return View();
    }
}

And here is an example of a test that should fail:

[TestFixture]
public class RoutingTests
{
    [TestFixtureSetUp]
    public void TestFixtureSetUp()
    {
        RouteTable.Routes.Clear();
        Application.RegisterRoutes(RouteTable.Routes);
    }

    [Test]
    public void TestWithGet()
    {
        var route = "~/Test/Example".WithMethod(HttpVerbs.Get);
        route.ShouldMapTo(r => r.Example());
    }
}

However, the test passes! I've seen one other unanswered question (sorry, wrong link) where this was also raised, and it seems like the functionality is broken. What's a better way to test that this route is accessible via POST only?

Upvotes: 1

Views: 1056

Answers (2)

Mediator
Mediator

Reputation: 15378

use this code:

var controller = new HomeController();
var methodInfo = controller.GetType().GetMethod("MrthodName");
var attributes = methodInfo.GetCustomAttributes(typeof(ActionMethodSelectorAttribute), true).Cast<ActionMethodSelectorAttribute>().ToList();

attributes - this is list accept verbs

Upvotes: 1

CoffeeCode
CoffeeCode

Reputation: 4314

It looks like you are just trying to test the ASP.NET MVC framework there. I dont think that such test will bring value...

Upvotes: 2

Related Questions