public void SelectAction_With_DifferentExecutionContexts() { ApiControllerActionSelector actionSelector = new ApiControllerActionSelector(); HttpControllerContext GetContext = ContextUtil.CreateControllerContext(); HttpControllerDescriptor usersControllerDescriptor = new HttpControllerDescriptor(GetContext.Configuration, "Users", typeof(UsersController)); usersControllerDescriptor.Configuration.Services.Replace(typeof(IHttpActionSelector), actionSelector); GetContext.ControllerDescriptor = usersControllerDescriptor; GetContext.Request = new HttpRequestMessage { Method = HttpMethod.Get }; HttpControllerContext PostContext = ContextUtil.CreateControllerContext(); usersControllerDescriptor.Configuration.Services.Replace(typeof(IHttpActionSelector), actionSelector); PostContext.ControllerDescriptor = usersControllerDescriptor; PostContext.Request = new HttpRequestMessage { Method = HttpMethod.Post }; HttpActionDescriptor getActionDescriptor = actionSelector.SelectAction(GetContext); HttpActionDescriptor postActionDescriptor = actionSelector.SelectAction(PostContext); Assert.Equal("Get", getActionDescriptor.ActionName); Assert.Equal("Post", postActionDescriptor.ActionName); }
public void SelectAction_WithDirectRoutes_RespectsPrecedence() { // Arrange var actionSelector = new ApiControllerActionSelector(); HttpControllerContext context = ContextUtil.CreateControllerContext(); context.Request = new HttpRequestMessage { Method = HttpMethod.Get }; var controllerDescriptor = new HttpControllerDescriptor(context.Configuration, "MultipleGet", typeof(MultipleGetController)); context.ControllerDescriptor = controllerDescriptor; ReflectedHttpActionDescriptor firstDirectRouteAction = (ReflectedHttpActionDescriptor)actionSelector.GetActionMapping(controllerDescriptor)["GetA"].Single(); HttpRouteData[] subRouteData = new HttpRouteData[2]; subRouteData[0] = new HttpRouteData(new HttpRoute()); subRouteData[1] = new HttpRouteData(new HttpRoute()); context.RouteData.Values.Add(RouteCollectionRoute.SubRouteDataKey, subRouteData); subRouteData[0].Route.DataTokens.Add("actions", new ReflectedHttpActionDescriptor[] { firstDirectRouteAction }); subRouteData[0].Route.DataTokens.Add("precedence", 2M); ReflectedHttpActionDescriptor secondDirectRouteAction = (ReflectedHttpActionDescriptor)actionSelector.GetActionMapping(controllerDescriptor)["GetB"].Single(); subRouteData[1].Route.DataTokens.Add("actions", new ReflectedHttpActionDescriptor[] { secondDirectRouteAction }); subRouteData[1].Route.DataTokens.Add("precedence", 1M); // Act HttpActionDescriptor actionDescriptor = actionSelector.SelectAction(context); // Assert Assert.Same(secondDirectRouteAction, actionDescriptor); }
public static HttpActionContext CreateActionContext(HttpControllerContext controllerContext = null, HttpActionDescriptor actionDescriptor = null) { HttpControllerContext context = controllerContext ?? ContextUtil.CreateControllerContext(); HttpActionDescriptor descriptor = actionDescriptor ?? CreateActionDescriptor(); descriptor.ControllerDescriptor = context.ControllerDescriptor; return(new HttpActionContext(context, descriptor)); }
public void ControllerContext_Property() { Assert.Reflection.Property <HttpActionContext, HttpControllerContext>( instance: new HttpActionContext(), propertyGetter: ac => ac.ControllerContext, expectedDefaultValue: null, allowNull: false, roundTripTestValue: ContextUtil.CreateControllerContext()); }
public static HttpActionContext CreateActionContext(HttpControllerContext controllerContext = null, HttpActionDescriptor actionDescriptor = null) { HttpControllerContext context = controllerContext ?? ContextUtil.CreateControllerContext(); HttpActionDescriptor descriptor = actionDescriptor ?? new Mock <HttpActionDescriptor>() { CallBase = true }.Object; return(new HttpActionContext(context, descriptor)); }
public void Parameter_Constructor() { HttpControllerContext controllerContext = ContextUtil.CreateControllerContext(); HttpActionDescriptor actionDescriptor = new Mock <HttpActionDescriptor>().Object; HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor); Assert.Same(controllerContext, actionContext.ControllerContext); Assert.Same(actionDescriptor, actionContext.ActionDescriptor); Assert.Same(controllerContext.Request, actionContext.Request); Assert.Null(actionContext.Response); Assert.NotNull(actionContext.ActionArguments); Assert.NotNull(actionContext.ModelState); }
public QueryableAttributeTest() { _response.RequestMessage = _request; _actionContext = ContextUtil.CreateActionContext(ContextUtil.CreateControllerContext(request: _request)); _actionContext.ControllerContext.ControllerDescriptor = new HttpControllerDescriptor { ControllerName = "TestControllerName" }; Mock.Get(_actionContext.ActionDescriptor).Setup(ad => ad.ActionName).Returns("testActionName"); _actionContext.Response = _response; _actionExecutedContext = ContextUtil.GetActionExecutedContext(_request, _response); _actionExecutedContext.ActionContext = _actionContext; }
public void Route_Post_Action_With_Content_Parameter() { // Arrange ApiController api = new UsersRpcController(); HttpRouteData route = new HttpRouteData(new HttpRoute()); route.Values.Add("action", "EchoUserObject"); User postedUser = new User() { FirstName = "SampleFirstName", LastName = "SampleLastName" }; HttpRequestMessage request = new HttpRequestMessage() { Method = HttpMethod.Post }; // Create a serialized request because this test directly calls the controller // which would have normally been working with a serialized request content. string serializedUserAsString = null; using (HttpRequestMessage tempRequest = new HttpRequestMessage() { Content = new ObjectContent <User>(postedUser, new XmlMediaTypeFormatter()) }) { serializedUserAsString = tempRequest.Content.ReadAsStringAsync().Result; } StringContent stringContent = new StringContent(serializedUserAsString); stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/xml"); request.Content = stringContent; HttpControllerContext controllerContext = ContextUtil.CreateControllerContext(instance: api, routeData: route, request: request); controllerContext.ControllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, "test", typeof(UsersRpcController)); // Act HttpResponseMessage message = api.ExecuteAsync( controllerContext, CancellationToken.None).Result; User user = message.Content.ReadAsAsync <User>().Result; // Assert Assert.Equal(postedUser.FirstName, user.FirstName); Assert.Equal(postedUser.LastName, user.LastName); }
public void GetActionMapping_IgnoresNonAction() { var actionSelector = new ApiControllerActionSelector(); HttpControllerContext context = ContextUtil.CreateControllerContext(); context.Request = new HttpRequestMessage { Method = HttpMethod.Get }; var controllerDescriptor = new HttpControllerDescriptor(context.Configuration, "NonAction", typeof(NonActionController)); context.ControllerDescriptor = controllerDescriptor; var mapping = actionSelector.GetActionMapping(controllerDescriptor); Assert.False(mapping.Contains("GetA")); Assert.True(mapping.Contains("GetB")); }
public void Default_Delete() { // Arrange ApiController api = new UsersController(); HttpControllerContext controllerContext = ContextUtil.CreateControllerContext(instance: api, request: new HttpRequestMessage() { Method = HttpMethod.Delete }); controllerContext.ControllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, "test", typeof(UsersController)); // Act HttpResponseMessage message = api.ExecuteAsync( controllerContext, CancellationToken.None).Result; // Assert Assert.Equal("User Deleted", message.Content.ReadAsStringAsync().Result); }
public void SelectAction_RespectsDirectRoutes() { var actionSelector = new ApiControllerActionSelector(); HttpControllerContext context = ContextUtil.CreateControllerContext(); context.Request = new HttpRequestMessage { Method = HttpMethod.Get }; var controllerDescriptor = new HttpControllerDescriptor(context.Configuration, "Users", typeof(UsersController)); context.ControllerDescriptor = controllerDescriptor; ReflectedHttpActionDescriptor directRouteAction = (ReflectedHttpActionDescriptor)actionSelector.GetActionMapping(controllerDescriptor)["Get"].First(); context.RouteData.Route.DataTokens.Add("actions", new ReflectedHttpActionDescriptor[] { directRouteAction }); HttpActionDescriptor actionDescriptor = actionSelector.SelectAction(context); Assert.Same(directRouteAction, actionDescriptor); }
public static HttpControllerContext CreateControllerContext( string httpMethod, string requestUrl, string routeUrl, object routeDefault = null ) { string baseAddress = "http://localhost/"; HttpConfiguration config = new HttpConfiguration(); HttpRoute route = routeDefault != null ? new HttpRoute(routeUrl, new HttpRouteValueDictionary(routeDefault)) : new HttpRoute(routeUrl); config.Routes.Add("test", route); HttpRequestMessage request = new HttpRequestMessage( HttpMethodHelper.GetHttpMethod(httpMethod), baseAddress + requestUrl ); IHttpRouteData routeData = config.Routes.GetRouteData(request); if (routeData == null) { throw new InvalidOperationException( "Could not dispatch to controller based on the route." ); } RemoveOptionalRoutingParameters(routeData.Values); HttpControllerContext controllerContext = ContextUtil.CreateControllerContext( config, routeData, request ); return(controllerContext); }
public void Route_ActionName() { // Arrange ApiController api = new UsersRpcController(); HttpRouteData route = new HttpRouteData(new HttpRoute()); route.Values.Add("action", "Admin"); HttpControllerContext controllerContext = ContextUtil.CreateControllerContext(instance: api, routeData: route, request: new HttpRequestMessage() { Method = HttpMethod.Post }); controllerContext.ControllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, "test", typeof(UsersRpcController)); // Act HttpResponseMessage message = api.ExecuteAsync(controllerContext, CancellationToken.None).Result; User user = message.Content.ReadAsAsync <User>().Result; // Assert Assert.Equal("Yao", user.FirstName); Assert.Equal("Huang", user.LastName); }
public void Setting_CustomActionSelector() { // Arrange ApiController api = new UsersController(); HttpControllerContext controllerContext = ContextUtil.CreateControllerContext(); HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, "test", typeof(UsersController)); controllerContext.ControllerDescriptor = controllerDescriptor; Mock <IHttpActionSelector> mockSelector = new Mock <IHttpActionSelector>(); mockSelector .Setup(invoker => invoker.SelectAction(It.IsAny <HttpControllerContext>())) .Returns(() => { Func <HttpResponseMessage> testDelegate = () => new HttpResponseMessage { Content = new StringContent("This is a test") }; return(new ReflectedHttpActionDescriptor { Configuration = controllerContext.Configuration, ControllerDescriptor = controllerDescriptor, MethodInfo = testDelegate.Method }); }); controllerDescriptor.Configuration.Services.Replace(typeof(IHttpActionSelector), mockSelector.Object); // Act HttpResponseMessage message = api.ExecuteAsync( controllerContext, CancellationToken.None).Result; // Assert Assert.Equal("This is a test", message.Content.ReadAsStringAsync().Result); }
public ReflectedHttpActionDescriptorTest() { _context = ContextUtil.CreateControllerContext(instance: _controller); }
public ReflectedHttpActionDescriptorTest() { _context = ContextUtil.CreateControllerContext(instance: _controller); _context.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; }
public void Constructor_Throws_IfActionDescriptorIsNull() { Assert.ThrowsArgumentNull( () => new HttpActionContext(ContextUtil.CreateControllerContext(), null), "actionDescriptor"); }