public void Primitives_Can_Be_Used_For_Top_And_Skip(string filter) { // Arrange QueryableAttribute attribute = new QueryableAttribute(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Primitive/?" + filter); HttpConfiguration config = new HttpConfiguration(); request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; HttpControllerContext controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute()), request); HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(new HttpConfiguration(), "Primitive", typeof(PrimitiveController)); HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(PrimitiveController).GetMethod("GetIEnumerableOfInt")); HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor); HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(HttpStatusCode.OK); HttpContent expectedResponse = new ObjectContent(typeof(object), new List <int>(), new JsonMediaTypeFormatter()); context.Response.Content = expectedResponse; // Act and Assert attribute.OnActionExecuted(context); HttpResponseMessage response = context.Response; Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(expectedResponse, response.Content); }
public void InvalidActionReturnType_Throws(string actionName) { // Arrange QueryableAttribute attribute = new QueryableAttribute(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customer?$skip=1"); HttpConfiguration config = new HttpConfiguration(); request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; HttpControllerContext controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute()), request); HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(new HttpConfiguration(), "CustomerHighLevel", typeof(CustomerHighLevelController)); HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(CustomerHighLevelController).GetMethod(actionName)); HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor); HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(HttpStatusCode.OK); Type returnType = actionDescriptor.ReturnType; object instance = returnType.IsArray ? Array.CreateInstance(returnType.GetElementType(), 5) : Activator.CreateInstance(returnType); context.Response.Content = new ObjectContent(returnType, instance, new JsonMediaTypeFormatter()); // Act & Assert Assert.Throws <InvalidOperationException>( () => attribute.OnActionExecuted(context), String.Format( "The action '{0}' on controller '{1}' with return type '{2}' cannot support querying. Ensure the type of the returned content is IEnumerable, IQueryable, or a generic form of either interface.", actionName, controllerDescriptor.ControllerName, actionDescriptor.ReturnType.FullName)); }
public virtual void QueryableUsesConfiguredAssembliesResolver_For_MappingDerivedTypes() { // Arrange QueryableAttribute attribute = new QueryableAttribute(); HttpActionExecutedContext actionExecutedContext = GetActionExecutedContext( "http://localhost:8080/QueryCompositionCustomer/?$filter=Id eq 2", QueryCompositionCustomerController.CustomerList.AsQueryable()); ODataModelBuilder modelBuilder = new ODataConventionModelBuilder(); modelBuilder.EntitySet <QueryCompositionCustomer>(typeof(QueryCompositionCustomer).Name); IEdmModel model = modelBuilder.GetEdmModel(); model.SetAnnotationValue <ClrTypeAnnotation>(model.FindType("System.Web.Http.OData.Query.QueryCompositionCustomer"), null); bool called = false; Mock <IAssembliesResolver> assembliesResolver = new Mock <IAssembliesResolver>(); assembliesResolver .Setup(r => r.GetAssemblies()) .Returns(new DefaultAssembliesResolver().GetAssemblies()) .Callback(() => { called = true; }) .Verifiable(); actionExecutedContext.Request.GetConfiguration().Services.Replace(typeof(IAssembliesResolver), assembliesResolver.Object); // Act attribute.OnActionExecuted(actionExecutedContext); // Assert Assert.True(called); }
public void Primitives_Cannot_Be_Used_By_Filter_And_OrderBy(string filter) { // Arrange QueryableAttribute attribute = new QueryableAttribute(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Primitive/?" + filter); HttpConfiguration config = new HttpConfiguration(); request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; HttpControllerContext controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute()), request); HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(new HttpConfiguration(), "Primitive", typeof(PrimitiveController)); HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(PrimitiveController).GetMethod("GetIEnumerableOfInt")); HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor); HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(HttpStatusCode.OK); context.Response.Content = new ObjectContent(typeof(object), new List <int>(), new JsonMediaTypeFormatter()); // Act and Assert attribute.OnActionExecuted(context); HttpResponseMessage response = context.Response; Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); Assert.IsAssignableFrom(typeof(ObjectContent), response.Content); Assert.IsType(typeof(HttpError), ((ObjectContent)response.Content).Value); Assert.Equal("Only $skip and $top OData query options are supported for this type.", ((HttpError)((ObjectContent)response.Content).Value).Message); }
public void OnActionExecuted_SingleResult_Returns400_IfQueryContainsNonSelectExpand() { HttpActionExecutedContext actionExecutedContext = GetActionExecutedContext("http://localhost/?$top=10", new Customer()); QueryableAttribute attribute = new QueryableAttribute(); attribute.OnActionExecuted(actionExecutedContext); Assert.Equal(HttpStatusCode.BadRequest, actionExecutedContext.Response.StatusCode); }
public void OnActionExecuted_SingleResult_WithMoreThanASingleQueryResult_ThrowsInvalidOperationException() { // Arrange var customers = CustomerList.AsQueryable(); SingleResult result = SingleResult.Create(customers); HttpActionExecutedContext actionExecutedContext = GetActionExecutedContext("http://localhost/", result); QueryableAttribute attribute = new QueryableAttribute(); // Act and Assert Assert.Throws <InvalidOperationException>(() => attribute.OnActionExecuted(actionExecutedContext)); }
public void OnActionExecuted_SingleResult_ReturnsSingleItemEvenIfThereIsNoSelectExpand() { Customer customer = new Customer(); SingleResult singleResult = new SingleResult <Customer>(new Customer[] { customer }.AsQueryable()); HttpActionExecutedContext actionExecutedContext = GetActionExecutedContext("http://localhost/", singleResult); QueryableAttribute attribute = new QueryableAttribute(); attribute.OnActionExecuted(actionExecutedContext); Assert.Equal(HttpStatusCode.OK, actionExecutedContext.Response.StatusCode); Assert.Equal(customer, (actionExecutedContext.Response.Content as ObjectContent).Value); }
public void QueryableOnActionUnknownOperatorStartingDollarSignThrows() { QueryableAttribute attribute = new QueryableAttribute(); HttpActionExecutedContext actionExecutedContext = GetActionExecutedContext( "http://localhost:8080/QueryCompositionCustomer?$orderby=Name desc&$unknown=12", QueryCompositionCustomerController.CustomerList.AsQueryable()); var exception = Assert.Throws <HttpResponseException>(() => attribute.OnActionExecuted(actionExecutedContext)); // QueryableAttribute will validate and throws Assert.Equal(HttpStatusCode.BadRequest, exception.Response.StatusCode); }
public void OnActionExecuted_SingleResult_WithEmptyQueryResult_SetsNotFoundResponse() { // Arrange var customers = Enumerable.Empty <Customer>().AsQueryable(); SingleResult result = SingleResult.Create(customers); HttpActionExecutedContext actionExecutedContext = GetActionExecutedContext("http://localhost/", result); QueryableAttribute attribute = new QueryableAttribute(); // Act attribute.OnActionExecuted(actionExecutedContext); // Assert Assert.Equal(HttpStatusCode.NotFound, actionExecutedContext.Response.StatusCode); }
public void QueryableOnActionUnknownOperatorIsAllowed() { QueryableAttribute attribute = new QueryableAttribute(); HttpActionExecutedContext actionExecutedContext = GetActionExecutedContext( "http://localhost:8080/?$orderby=$it desc&unknown=12", Enumerable.Range(0, 5).AsQueryable()); // unsupported operator - ignored attribute.OnActionExecuted(actionExecutedContext); List <int> result = actionExecutedContext.Response.Content.ReadAsAsync <List <int> >().Result; Assert.Equal(new[] { 4, 3, 2, 1, 0 }, result); }
public async Task <HttpResponseMessage> ExecuteActionFilterAsync( HttpActionContext actionContext, CancellationToken cancellationToken, Func <Task <HttpResponseMessage> > continuation) { cancellationToken.ThrowIfCancellationRequested(); HttpResponseMessage response = null; Exception exception = null; try { response = await continuation(); } catch (Exception e) { exception = e; } try { HttpActionExecutedContext executedContext = new HttpActionExecutedContext(actionContext, exception) { Response = response }; _innerFilter.OnActionExecuted(executedContext); if (executedContext.Response != null) { return(await BufferAsyncQueryable(executedContext.Response)); } if (executedContext.Exception != null) { throw executedContext.Exception; } } catch { // Catch is running because OnActionExecuted threw an exception, so we just want to re-throw the exception. // We also need to reset the response to forget about it since a filter threw an exception. actionContext.Response = null; throw; } throw new NotSupportedException("InnerFilter must set either Response or Exception. Both are null."); }
public void DifferentReturnTypeWorks(string methodName, object responseObject, bool isNoOp) { // Arrange QueryableAttribute attribute = new QueryableAttribute(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customer/?$orderby=Name"); HttpConfiguration config = new HttpConfiguration(); request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; HttpControllerContext controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute()), request); HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(new HttpConfiguration(), "CustomerHighLevel", typeof(CustomerHighLevelController)); HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(CustomerHighLevelController).GetMethod(methodName)); HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor); HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(HttpStatusCode.OK); context.Response.Content = new ObjectContent(typeof(object), responseObject, new JsonMediaTypeFormatter()); // Act and Assert attribute.OnActionExecuted(context); Assert.Equal(HttpStatusCode.OK, context.Response.StatusCode); Assert.True(context.Response.Content is ObjectContent); Assert.Equal(isNoOp, ((ObjectContent)context.Response.Content).Value == responseObject); }
public void NonObjectContentResponse_ThrowsInvalidOperationException() { // Arrange QueryableAttribute attribute = new QueryableAttribute(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customer?$skip=1"); HttpConfiguration config = new HttpConfiguration(); request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; HttpControllerContext controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute()), request); HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(new HttpConfiguration(), "CustomerHighLevel", typeof(CustomerHighLevelController)); HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(CustomerHighLevelController).GetMethod("GetIEnumerableOfCustomer")); HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor); HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(HttpStatusCode.OK); context.Response.Content = new StreamContent(new MemoryStream()); // Act & Assert Assert.Throws <InvalidOperationException>( () => attribute.OnActionExecuted(context), "Queries can not be applied to a response content of type 'System.Net.Http.StreamContent'. The response content must be an ObjectContent."); }
public void UnknownQueryNotStartingWithDollarSignWorks() { // Arrange QueryableAttribute attribute = new QueryableAttribute(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customer/?select"); HttpConfiguration config = new HttpConfiguration(); request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; HttpControllerContext controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute()), request); HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(new HttpConfiguration(), "CustomerHighLevel", typeof(CustomerHighLevelController)); HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(CustomerHighLevelController).GetMethod("Get")); HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor); HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(HttpStatusCode.OK); context.Response.Content = new ObjectContent(typeof(object), new List <Customer>(), new JsonMediaTypeFormatter()); // Act and Assert attribute.OnActionExecuted(context); Assert.Equal(HttpStatusCode.OK, context.Response.StatusCode); }
public void NonGenericEnumerableReturnTypeThrows() { // Arrange QueryableAttribute attribute = new QueryableAttribute(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customer/?$skip=1"); HttpConfiguration config = new HttpConfiguration(); config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; HttpControllerContext controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute()), request); HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(new HttpConfiguration(), "CustomerHighLevel", typeof(CustomerHighLevelController)); HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(CustomerHighLevelController).GetMethod("GetNonGenericEnumerable")); HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor); HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(HttpStatusCode.OK); context.Response.Content = new ObjectContent(typeof(object), new NonGenericEnumerable(), new JsonMediaTypeFormatter()); // Act & Assert Assert.Throws <InvalidOperationException>( () => attribute.OnActionExecuted(context), "The 'QueryableAttribute' type cannot be used with action 'GetNonGenericEnumerable' on controller 'CustomerHighLevel' because the return type 'System.Web.Http.OData.TestCommon.Models.NonGenericEnumerable' does not specify the type of the collection."); }
public void NonGenericEnumerableReturnTypeThrows() { // Arrange QueryableAttribute attribute = new QueryableAttribute(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customer/?$skip=1"); HttpConfiguration config = new HttpConfiguration(); config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; HttpControllerContext controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute()), request); HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(new HttpConfiguration(), "CustomerHighLevel", typeof(CustomerHighLevelController)); HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(CustomerHighLevelController).GetMethod("GetNonGenericEnumerable")); HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor); HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(HttpStatusCode.OK); context.Response.Content = new ObjectContent(typeof(IEnumerable), new NonGenericEnumerable(), new JsonMediaTypeFormatter()); // Act & Assert Assert.Throws <InvalidOperationException>( () => attribute.OnActionExecuted(context), "Cannot create an EDM model as the action 'QueryableAttribute' on controller 'GetNonGenericEnumerable' has a return type 'CustomerHighLevel' that does not implement IEnumerable<T>."); }
public void UnknownQueryStartingWithDollarSignThrows() { // Arrange QueryableAttribute attribute = new QueryableAttribute(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customer/?$select"); HttpConfiguration config = new HttpConfiguration(); request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; HttpControllerContext controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute()), request); HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(new HttpConfiguration(), "CustomerHighLevel", typeof(CustomerHighLevelController)); HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(CustomerHighLevelController).GetMethod("Get")); HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor); HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(HttpStatusCode.OK); context.Response.Content = new ObjectContent(typeof(object), new List<Customer>(), new JsonMediaTypeFormatter()); // Act and Assert HttpResponseException errorResponse = Assert.Throws<HttpResponseException>(() => attribute.OnActionExecuted(context)); Assert.Equal(HttpStatusCode.BadRequest, errorResponse.Response.StatusCode); }
public void NonGenericEnumerableReturnTypeThrows() { // Arrange QueryableAttribute attribute = new QueryableAttribute(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customer/?$skip=1"); HttpConfiguration config = new HttpConfiguration(); config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; HttpControllerContext controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute()), request); HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(new HttpConfiguration(), "CustomerHighLevel", typeof(CustomerHighLevelController)); HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(CustomerHighLevelController).GetMethod("GetNonGenericEnumerable")); HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor); HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(HttpStatusCode.OK); context.Response.Content = new ObjectContent(typeof(object), new NonGenericEnumerable(), new JsonMediaTypeFormatter()); // Act & Assert Assert.Throws<InvalidOperationException>( () => attribute.OnActionExecuted(context), "The 'QueryableAttribute' type cannot be used with action 'GetNonGenericEnumerable' on controller 'CustomerHighLevel' because the return type 'System.Web.Http.OData.TestCommon.Models.NonGenericEnumerable' does not specify the type of the collection."); }
public void Primitives_Can_Be_Used_For_Top_And_Skip(string filter) { // Arrange QueryableAttribute attribute = new QueryableAttribute(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Primitive/?" + filter); HttpConfiguration config = new HttpConfiguration(); request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; HttpControllerContext controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute()), request); HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(new HttpConfiguration(), "Primitive", typeof(PrimitiveController)); HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(PrimitiveController).GetMethod("GetIEnumerableOfInt")); HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor); HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(HttpStatusCode.OK); HttpContent expectedResponse = new ObjectContent(typeof(object), new List<int>(), new JsonMediaTypeFormatter()); context.Response.Content = expectedResponse; // Act and Assert attribute.OnActionExecuted(context); HttpResponseMessage response = context.Response; Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(expectedResponse, response.Content); }
public void InvalidActionReturnType_Throws(string actionName) { // Arrange QueryableAttribute attribute = new QueryableAttribute(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customer?$skip=1"); HttpConfiguration config = new HttpConfiguration(); request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; HttpControllerContext controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute()), request); HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(new HttpConfiguration(), "CustomerHighLevel", typeof(CustomerHighLevelController)); HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(CustomerHighLevelController).GetMethod(actionName)); HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor); HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(HttpStatusCode.OK); Type returnType = actionDescriptor.ReturnType; object instance = returnType.IsArray ? Array.CreateInstance(returnType.GetElementType(), 5) : Activator.CreateInstance(returnType); context.Response.Content = new ObjectContent(returnType, instance, new JsonMediaTypeFormatter()); // Act & Assert Assert.Throws<InvalidOperationException>( () => attribute.OnActionExecuted(context), String.Format( "The action '{0}' on controller '{1}' with return type '{2}' cannot support querying. Ensure the type of the returned content is IEnumerable, IQueryable, or a generic form of either interface.", actionName, controllerDescriptor.ControllerName, actionDescriptor.ReturnType.FullName)); }
public void NonObjectContentResponse_ThrowsInvalidOperationException() { // Arrange QueryableAttribute attribute = new QueryableAttribute(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customer?$skip=1"); HttpConfiguration config = new HttpConfiguration(); request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; HttpControllerContext controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute()), request); HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(new HttpConfiguration(), "CustomerHighLevel", typeof(CustomerHighLevelController)); HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(CustomerHighLevelController).GetMethod("GetIEnumerableOfCustomer")); HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor); HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(HttpStatusCode.OK); context.Response.Content = new StreamContent(new MemoryStream()); // Act & Assert Assert.Throws<InvalidOperationException>( () => attribute.OnActionExecuted(context), "Queries can not be applied to a response content of type 'System.Net.Http.StreamContent'. The response content must be an ObjectContent."); }
public void NonGenericEnumerableReturnTypeThrows() { // Arrange QueryableAttribute attribute = new QueryableAttribute(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customer/?$skip=1"); HttpConfiguration config = new HttpConfiguration(); config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; HttpControllerContext controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute()), request); HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(new HttpConfiguration(), "CustomerHighLevel", typeof(CustomerHighLevelController)); HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(CustomerHighLevelController).GetMethod("GetNonGenericEnumerable")); HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor); HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(HttpStatusCode.OK); context.Response.Content = new ObjectContent(typeof(IEnumerable), new NonGenericEnumerable(), new JsonMediaTypeFormatter()); // Act & Assert Assert.Throws<InvalidOperationException>( () => attribute.OnActionExecuted(context), "Cannot create an EDM model as the action 'QueryableAttribute' on controller 'GetNonGenericEnumerable' has a return type 'CustomerHighLevel' that does not implement IEnumerable<T>."); }
public void UnknownQueryStartingWithDollarSignThrows() { // Arrange QueryableAttribute attribute = new QueryableAttribute(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customer/?$custom"); HttpConfiguration config = new HttpConfiguration(); request.SetConfiguration(config); HttpControllerContext controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute()), request); HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(new HttpConfiguration(), "CustomerHighLevel", typeof(CustomerHighLevelController)); HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(CustomerHighLevelController).GetMethod("Get")); HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor); HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(HttpStatusCode.OK); context.Response.Content = new ObjectContent(typeof(IEnumerable <Customer>), new List <Customer>(), new JsonMediaTypeFormatter()); // Act and Assert HttpResponseException errorResponse = Assert.Throws <HttpResponseException>(() => attribute.OnActionExecuted(context)); Assert.Equal(HttpStatusCode.BadRequest, errorResponse.Response.StatusCode); }
public void Primitives_Cannot_Be_Used_By_Filter_And_OrderBy(string filter) { // Arrange QueryableAttribute attribute = new QueryableAttribute(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Primitive/?" + filter); HttpConfiguration config = new HttpConfiguration(); request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; HttpControllerContext controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute()), request); HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(new HttpConfiguration(), "Primitive", typeof(PrimitiveController)); HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(PrimitiveController).GetMethod("GetIEnumerableOfInt")); HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor); HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(HttpStatusCode.OK); context.Response.Content = new ObjectContent(typeof(object), new List<int>(), new JsonMediaTypeFormatter()); // Act and Assert attribute.OnActionExecuted(context); HttpResponseMessage response = context.Response; Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); Assert.IsAssignableFrom(typeof(ObjectContent), response.Content); Assert.IsType(typeof(HttpError), ((ObjectContent)response.Content).Value); Assert.Equal("Only $skip and $top OData query options are supported for this type.", ((HttpError)((ObjectContent)response.Content).Value).Message); }
public void UnknownQueryNotStartingWithDollarSignWorks() { // Arrange QueryableAttribute attribute = new QueryableAttribute(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customer/?select"); HttpConfiguration config = new HttpConfiguration(); request.SetConfiguration(config); HttpControllerContext controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute()), request); HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(new HttpConfiguration(), "CustomerHighLevel", typeof(CustomerHighLevelController)); HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(CustomerHighLevelController).GetMethod("Get")); HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor); HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(HttpStatusCode.OK); context.Response.Content = new ObjectContent(typeof(IEnumerable<Customer>), new List<Customer>(), new JsonMediaTypeFormatter()); // Act and Assert attribute.OnActionExecuted(context); Assert.Equal(HttpStatusCode.OK, context.Response.StatusCode); }