Using the WebAPI.
One of our tests that had been created was to ensure that for a specific controller, only GET verbs where permitted.
A test had been written that used the MVC HelpPages
HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(baseAddress);
config.Routes.MapHttpRoute(
"SearchAPI",
"api/{controller}/{id}");
HttpSelfHostServer server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
IApiExplorer apiExplorer = config.Services.GetApiExplorer();
var apiDescriptions = apiExplorer.ApiDescriptions;
var data = from description in apiDescriptions
where description.ActionDescriptor.ControllerDescriptor.ControllerType.FullName.StartsWith("MySite.Presentation.Pages.SearchAPI")
orderby description.RelativePath
select description
;
foreach (var apiDescription in data)
{
Assert.That(apiDescription.HttpMethod, Is.EqualTo(HttpMethod.Get), string.Format("Method not Allowed: {0} {1}", apiDescription.RelativePath, apiDescription.HttpMethod));
}
Now this test while it may not have been the best way of ensuring that for our controller, only GET HTTP VERB methods where applicable, it worked.
We have now upgraded to MVC5, and this test now fails. As HttpSelfHostServer is no longer available
Looking at Microsoft's msdn library, you are not recommended to use HttpSelfHostServer, but instead are encouraged to use Owin.
I have started off with a new Owin class
public class OwinStartUp
{
public void Configuration(IAppBuilder appBuilder)
{
var config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "SearchAPI",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
AreaRegistration.RegisterAllAreas();
appBuilder.UseWebApi(config);
}
}
But when it comes to the test, this is as far as I have been able to get
string baseAddress = "http://localhost/bar";
using (var server = WebApp.Start<OwinStartUp>(url: baseAddress))
{
}
I dont know how to access Services from the configuration, to then be able to call the GetApiExplorer method as there are no public methods on the server variable being suggested by Intellisense..
I have been looking at some sites that show how to use Owin, but they have not helped me solve this issue: http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api
There is also this existing questions Can't get ASP.NET Web API 2 Help pages working when using Owin But that has not helped me solve the problem.
What do I need to do, to be able to write a unit test to ensure that only specific HTTP VERBS are permitted for a controller/method, or how to configure Owin to use the API HelpPages