Scenario:
A user has created a skeletal structure of ApiControllers and actions. Currently the user does not have any implementation yet in them. User creates some routes and likes to test if the expected action is hit for a given request message.
Currently for testing the above scenario, a user would need to know bunch of inner apis like ControllerDescriptor, ActionSelector etc.(Example below) This introduces complexity to the end user. Given that this is a very basic scenario that users could hit, we should try to improve the testability of it.
FYI...following is the sample test code that i had to write currently:
HttpConfiguration testGlobalConfiguration = new HttpConfiguration(new HttpRouteCollection("TestApp"));
WebApiConfig.Register(testGlobalConfiguration);
HttpRequestMessage request = new HttpRequestMessage(method, requestUrl);
IHttpRouteData routeData = testGlobalConfiguration.Routes.GetRouteData(request);
request.Properties[HttpPropertyKeys.HttpRouteDataKey] = routeData;
DefaultHttpControllerSelector controllerSelector = new DefaultHttpControllerSelector(testGlobalConfiguration);
HttpControllerDescriptor controlleDescriptor = controllerSelector.SelectController(request);
HttpControllerContext controllerContext = new HttpControllerContext(testGlobalConfiguration, routeData, request);
controllerContext.ControllerDescriptor = controlleDescriptor;
ApiControllerActionSelector selector = new ApiControllerActionSelector();
HttpActionDescriptor actionDescriptor = selector.SelectAction(controllerContext);
Assert.IsTrue(controlleDescriptor.ControllerType.Equals(typeof(IdeasController)));
Assert.IsTrue(actionDescriptor.ActionName == "GetIdeas");
Comments: The filter should probably also short circuit the request by setting actionContext.Response, but you get the idea.
A user has created a skeletal structure of ApiControllers and actions. Currently the user does not have any implementation yet in them. User creates some routes and likes to test if the expected action is hit for a given request message.
Currently for testing the above scenario, a user would need to know bunch of inner apis like ControllerDescriptor, ActionSelector etc.(Example below) This introduces complexity to the end user. Given that this is a very basic scenario that users could hit, we should try to improve the testability of it.
FYI...following is the sample test code that i had to write currently:
HttpConfiguration testGlobalConfiguration = new HttpConfiguration(new HttpRouteCollection("TestApp"));
WebApiConfig.Register(testGlobalConfiguration);
HttpRequestMessage request = new HttpRequestMessage(method, requestUrl);
IHttpRouteData routeData = testGlobalConfiguration.Routes.GetRouteData(request);
request.Properties[HttpPropertyKeys.HttpRouteDataKey] = routeData;
DefaultHttpControllerSelector controllerSelector = new DefaultHttpControllerSelector(testGlobalConfiguration);
HttpControllerDescriptor controlleDescriptor = controllerSelector.SelectController(request);
HttpControllerContext controllerContext = new HttpControllerContext(testGlobalConfiguration, routeData, request);
controllerContext.ControllerDescriptor = controlleDescriptor;
ApiControllerActionSelector selector = new ApiControllerActionSelector();
HttpActionDescriptor actionDescriptor = selector.SelectAction(controllerContext);
Assert.IsTrue(controlleDescriptor.ControllerType.Equals(typeof(IdeasController)));
Assert.IsTrue(actionDescriptor.ActionName == "GetIdeas");
Comments: The filter should probably also short circuit the request by setting actionContext.Response, but you get the idea.