I am trying to do some unit testing of my WebApi route configuration. I want to test that the route "/api/super"
maps to the Get()
method of my SuperController
. I've setup the below test and am having a few issues.
public void GetTest()
{
var url = "~/api/super";
var routeCollection = new HttpRouteCollection();
routeCollection.MapHttpRoute("DefaultApi", "api/{controller}/");
var httpConfig = new HttpConfiguration(routeCollection);
var request = new HttpRequestMessage(HttpMethod.Get, url);
// exception when url = "/api/super"
// can get around w/ setting url = "http://localhost/api/super"
var routeData = httpConfig.Routes.GetRouteData(request);
request.Properties[HttpPropertyKeys.HttpRouteDataKey] = routeData;
var controllerSelector = new DefaultHttpControllerSelector(httpConfig);
var controlleDescriptor = controllerSelector.SelectController(request);
var controllerContext =
new HttpControllerContext(httpConfig, routeData, request);
controllerContext.ControllerDescriptor = controlleDescriptor;
var selector = new ApiControllerActionSelector();
var actionDescriptor = selector.SelectAction(controllerContext);
Assert.AreEqual(typeof(SuperController),
controlleDescriptor.ControllerType);
Assert.IsTrue(actionDescriptor.ActionName == "Get");
}
My first issue is that if I don't specify a fully qualified URL httpConfig.Routes.GetRouteData(request);
throws a InvalidOperationException
exception with a message of "This operation is not supported for a relative URI."
I'm obviously missing something with my stubbed configuration. I would prefer to use a relative URI as it does not seem reasonable to use a fully qualified URI for route testing.
My second issue with my configuration above is I am not testing my routes as configured in my RouteConfig but am instead using:
var routeCollection = new HttpRouteCollection();
routeCollection.MapHttpRoute("DefaultApi", "api/{controller}/");
How do I make use of the assigned RouteTable.Routes
as configured in a typical Global.asax:
public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
// other startup stuff
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
// route configuration
}
}
Further what I have stubbed out above may not be the best test configuration. If there is a more streamlined approach I am all ears.
question from:
https://stackoverflow.com/questions/11851558/testing-route-configuration-in-asp-net-webapi 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…