예제 #1
0
        public ODataSingletonTest()
        {
            var controllers = new[] { typeof(OscorpController), typeof(OscorpSubsController) };
            var server      = TestServerFactory.Create(controllers, (config) =>
            {
                config.MapODataServiceRoute("odata", "odata", GetEdmModel());
            });

            _client = TestServerFactory.CreateClient(server);
        }
예제 #2
0
        private static HttpClient GetClient()
        {
            var controllers = new[] { typeof(MetadataController), typeof(DateAndTimeOfDayModelsController) };
            var server      = TestServerFactory.Create(controllers, (config) =>
            {
                config.Count().OrderBy().Filter().Expand().MaxTop(null).Select();
                config.MapODataServiceRoute("odata", "odata", GetEdmModel());
            });

            HttpClient client = TestServerFactory.CreateClient(server);

            return(client);
        }
예제 #3
0
        private static HttpClient GetClient(DependencyInjectionModel instance)
        {
            IEdmModel model       = GetEdmModel();
            var       controllers = new[] { typeof(DependencyInjectionModelsController) };
            var       server      = TestServerFactory.Create(controllers, config =>
            {
                config.MapODataServiceRoute("odata", "odata", builder =>
                                            builder.AddService(ServiceLifetime.Singleton, sp => instance)
                                            .AddService(ServiceLifetime.Singleton, sp => model)
                                            .AddService <IEnumerable <IODataRoutingConvention> >(ServiceLifetime.Singleton, sp =>
                                                                                                 ODataRoutingConventions.CreateDefaultWithAttributeRouting("odata", config)));
            });

            return(TestServerFactory.CreateClient(server));
        }
예제 #4
0
        private HttpClient GetClient(bool enableNullDynamicProperty = false)
        {
            var controllers = new[] { typeof(OpenCustomersController) };
            var server      = TestServerFactory.Create(controllers, (config) =>
            {
                if (enableNullDynamicProperty)
                {
                    config.SetSerializeNullDynamicProperty(enableNullDynamicProperty);
                }

                config.MapODataServiceRoute("odata", "odata", GetEdmModel());
            });

            return(TestServerFactory.CreateClient(server));
        }
예제 #5
0
        public ApplyTest()
        {
            Type[] controllers = new[] { typeof(ApplyTestCustomersController), typeof(NonODataApplyTestCustomersController), };
            var    server      = TestServerFactory.Create(controllers, (config) =>
            {
                var builder = ODataConventionModelBuilderFactory.Create(config);
                builder.EntitySet <ApplyTestCustomer>("ApplyTestCustomers");

                config.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
                config.Count().Filter().OrderBy().Expand().MaxTop(null).Select();

                config.MapNonODataRoute("api", "api/{controller}", new { controller = "NonODataApplyTestCustomers", action = "Get" });
                config.EnableDependencyInjection();
            });

            _client = TestServerFactory.CreateClient(server);
        }
예제 #6
0
        public async Task Get_UnTyped_Enum_Collection_Property(string requestUri, string expectedContainsResult)
        {
            // Arrange
            var controllers = new[] { typeof(UntypedSimpleOpenCustomersController) };
            var server      = TestServerFactory.Create(controllers, (config) =>
            {
                config.MapODataServiceRoute("odata", "odata", GetUntypedEdmModel());
            });
            var client = TestServerFactory.CreateClient(server);

            // Act
            HttpResponseMessage response = await client.GetAsync(_untypedCustomerRequestRooturl + requestUri);

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            Assert.Contains(expectedContainsResult, await response.Content.ReadAsStringAsync());
        }
예제 #7
0
        public QueryableLimitationTest()
        {
            _model = GetEdmModel();
            var controllers = new[]
            {
                typeof(QueryLimitCustomersController),
                typeof(OpenCustomersController),
                typeof(MetadataController)
            };
            var server = TestServerFactory.Create(controllers, config =>
            {
                config.Count().OrderBy().Filter().Expand().MaxTop(null);
                config.MapODataServiceRoute("odata", "odata", _model);
            });

            _client = TestServerFactory.CreateClient(server);
        }
        public async Task AGet_Minimial()
        {
            // Arrange
            var controllers = new[] { typeof(AccountsController) };
            var server      = TestServerFactory.Create(controllers, (config) =>
            {
                config.Count().OrderBy().Filter().Expand().MaxTop(null);
                config.MapODataServiceRoute("odata", "odata", GetEdmModel());
            });
            HttpClient client = TestServerFactory.CreateClient(server);

            // Act
            HttpResponseMessage response = await client.GetAsync(_requestRooturl + "Accounts?$expand=PayoutPI&$format=application/json;odata.metadata=minimal");

            // Assert
            Assert.True(response.IsSuccessStatusCode);
        }
예제 #9
0
        private Task <HttpResponseMessage> GetResponse(string uri, string acceptHeader)
        {
            var controllers = new[] {
                typeof(SelectExpandTestCustomersController),
                typeof(SelectExpandTestCustomersAliasController),
                typeof(PlayersController),
                typeof(NonODataSelectExpandTestCustomersController),
                typeof(AttributedSelectExpandCustomersController),
                typeof(SelectExpandTestCustomer),
                typeof(SelectExpandTestSpecialCustomer),
                typeof(SelectExpandTestCustomerWithAlias),
                typeof(SelectExpandTestOrder),
                typeof(SelectExpandTestSpecialOrder),
                typeof(SelectExpandTestSpecialOrderWithAlias),
                typeof(ReferenceNavigationPropertyExpandFilterController),
            };

            var server = TestServerFactory.Create(controllers, (config) =>
            {
#if NETFX
                config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
#endif
                config.Count().Filter().OrderBy().Expand().MaxTop(null).Select();
                config.MapODataServiceRoute("odata", "odata", GetModel());
                config.MapODataServiceRoute("odata-inheritance", "odata-inheritance", GetModelWithInheritance());
                config.MapODataServiceRoute("odata-alias", "odata-alias", GetModelWithCustomerAlias());
                config.MapODataServiceRoute("odata-alias2-inheritance", "odata-alias2-inheritance", GetModelWithCustomerAliasAndInheritance());
                config.MapODataServiceRoute("odata2", "odata2", GetModelWithOperations());
                config.MapODataServiceRoute("odata-expandfilter", "odata-expandfilter", GetModelWithReferenceNavigationPropertyFilter());
#if NETCORE
                config.MapRoute("api", "api/{controller}", new { controller = "NonODataSelectExpandTestCustomers", action = "Get" });
#else
                config.Routes.MapHttpRoute("api", "api/{controller}", new { controller = "NonODataSelectExpandTestCustomers" });
#endif
                config.EnableDependencyInjection();
            });

            HttpClient         client  = TestServerFactory.CreateClient(server);
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost" + uri);

            request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse(acceptHeader));
#if NETFX
            request.SetConfiguration(server.Configuration);
#endif
            return(client.SendAsync(request));
        }
예제 #10
0
        public async Task Patch_OpenEntityType()
        {
            // Arrange
            const string Payload = "{" +
                                   "\"CustomerId\":99,\"Name\":\"ChangedName\"," +
                                   "\"[email protected]\":\"#DateTimeOffset\",\"Token\":\"2014-01-01T00:00:00Z\"," +
                                   "\"[email protected]\":\"#Collection(Double)\"," +
                                   "\"DoubleList\":[5.5, 4.4, 3.3]" +
                                   "}";

            const string RequestUri = "http://localhost/odata/SimpleOpenCustomers(2)";

            var controllers = new[] { typeof(SimpleOpenCustomersController) };
            var server      = TestServerFactory.Create(controllers, (config) =>
            {
                config.MapODataServiceRoute("odata", "odata", GetEdmModel());
            });
            var client = TestServerFactory.CreateClient(server);

            // Act
            HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), RequestUri);

            request.Content = new StringContent(Payload);
            request.Content.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/json");
            request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=full"));
            HttpResponseMessage response = await client.SendAsync(request);

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            JObject result = JObject.Parse(await response.Content.ReadAsStringAsync());

            Assert.Equal(99, result["CustomerId"]);
            Assert.Equal("ChangedName", result["Name"]);

            // The type and the value of "Token" are changed.
            Assert.Equal("#DateTimeOffset", result["*****@*****.**"]);

            // The type and the value of "IntList" are un-changed.
            Assert.Equal(new JArray(new[] { 1, 2, 3, 4, 5, 6, 7 }), result["IntList"]);

            // New dynamic property "DoubleList" is added.
            Assert.Equal(new JArray(new[] { 5.5, 4.4, 3.3 }), result["DoubleList"]);
        }
예제 #11
0
        private Task <HttpResponseMessage> GetResponse(string uri, string acceptHeader)
        {
            var controllers = new[] { typeof(MsCustomersController), typeof(MetadataController) };
            var server      = TestServerFactory.Create(controllers, configuration =>
            {
#if NETFX
                configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
#endif
                configuration.Count().OrderBy().Filter().Expand().MaxTop(null);
                configuration.MapODataServiceRoute("odata", "odata", GetModel());
            });

            HttpClient client = TestServerFactory.CreateClient(server);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost" + uri);

            request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse(acceptHeader));
            return(client.SendAsync(request));
        }
예제 #12
0
        public async Task Get_OpenEntityType(bool enableNullDynamicProperty)
        {
            // Arrange
            const string RequestUri = "http://localhost/odata/SimpleOpenCustomers(9)";

            var controllers = new[] { typeof(SimpleOpenCustomersController) };

            var server = TestServerFactory.Create(controllers, (config) =>
            {
                config.SetSerializeNullDynamicProperty(enableNullDynamicProperty);
                config.MapODataServiceRoute("odata", "odata", GetEdmModel());
            });
            var client = TestServerFactory.CreateClient(server);

            // Act
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, RequestUri);

            request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=full"));
            HttpResponseMessage response = await client.SendAsync(request);

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            JObject result = JObject.Parse(await response.Content.ReadAsStringAsync());

            Assert.Equal("http://localhost/odata/$metadata#SimpleOpenCustomers/Microsoft.Test.AspNet.OData.Common.SimpleVipCustomer/$entity", result["@odata.context"]);
            Assert.Equal("#Microsoft.Test.AspNet.OData.Common.SimpleVipCustomer", result["@odata.type"]);
            Assert.Equal(9, result["CustomerId"]);
            Assert.Equal("VipCustomer", result["Name"]);
            Assert.Equal("#Collection(Int32)", result["*****@*****.**"]);
            Assert.Equal(new JArray(new[] { 200, 100, 300, 0, 400 }), result["ListProp"]);
            Assert.Equal("0001-01-01", result["DateList"][0]);
            Assert.Equal("9999-12-31", result["DateList"][1]);
            if (enableNullDynamicProperty)
            {
                Assert.NotNull(result["Receipt"]);
                Assert.Equal(JValue.CreateNull(), result["Receipt"]);
            }
            else
            {
                Assert.Null(result["Receipt"]);
            }
        }
예제 #13
0
        private static HttpClient GetClient(TimeZoneInfo timeZoneInfo)
        {
            var controllers = new[] { typeof(MetadataController), typeof(DateTimeModelsController) };

            var server = TestServerFactory.Create(controllers, (config) =>
            {
                config.Count().OrderBy().Filter().Expand().MaxTop(null).Select();
                if (timeZoneInfo != null)
                {
                    config.SetTimeZoneInfo(timeZoneInfo);
                }
                else
                {
                    config.SetTimeZoneInfo(TimeZoneInfo.Local);
                }
                config.MapODataServiceRoute("odata", "odata", GetEdmModel());
            });

            return(TestServerFactory.CreateClient(server));
        }
예제 #14
0
        private static System.Web.Http.HttpServer CreateServer(string customersEntitySet)
#endif
        {
            // We need to do this to avoid controllers with incorrect attribute
            // routing configuration in this assembly that cause an exception to
            // be thrown at runtime. With this, we restrict the test to the following
            // set of controllers.
            Type[] controllers = new Type[]
            {
                typeof(OnlyFilterAllowedCustomersController),
                typeof(OnlyFilterAndEqualsAllowedCustomersController),
                typeof(FilterDisabledCustomersController),
                typeof(EverythingAllowedCustomersController),
                typeof(OtherLimitationsCustomersController),
            };

            ODataModelBuilder builder = ODataConventionModelBuilderFactory.Create();

            builder.EntitySet <EnableQueryCustomer>(customersEntitySet);
            builder.EntityType <PremiumEnableQueryCustomer>();

            builder.EntitySet <EnableQueryCategory>("EnableQueryCategories");
            builder.EntityType <PremiumEnableQueryCategory>();

            builder.EntitySet <EnableQueryOrder>("EnableQueryOrders");
            builder.EntityType <DiscountedEnableQueryOrder>();

            builder.EntitySet <EnableQueryOrderLine>("EnableQueryOrderLines");

            builder.ComplexType <EnableQueryAddress>();

            IEdmModel model = builder.GetEdmModel();

            return(TestServerFactory.Create(controllers, (config) =>
            {
                config.MapODataServiceRoute("odata", "odata", model);
                config.Count().OrderBy().Filter().Expand().MaxTop(null).Select();
            }));
        }
예제 #15
0
        public async Task Post_UnTyped_OpenEntityType()
        {
            // Arrange
            const string Payload = "{" +
                                   "\"@odata.context\":\"http://localhost/odata/$metadata#UntypedSimpleOpenCustomers/$entity\"," +
                                   "\"CustomerId\":6,\"[email protected]\":\"#String\",\"Name\":\"FirstName 6\"," +
                                   "\"Address\":{" +
                                   "\"@odata.type\":\"#NS.Address\",\"Street\":\"Street 6\",\"City\":\"City 6\"" +
                                   "}," +
                                   "\"[email protected]\":\"#Collection(NS.Address)\"," +
                                   "\"Addresses\":[{" +
                                   "\"@odata.type\":\"#NS.Address\",\"Street\":\"Street 7\",\"City\":\"City 7\"" +
                                   "}]," +
                                   "\"[email protected]\":\"#Collection(Double)\"," +
                                   "\"DoubleList\":[5.5, 4.4, 3.3]," +
                                   "\"[email protected]\":\"#NS.Color\"," +
                                   "\"FavoriteColor\":\"Red\"," +
                                   "\"Color\":\"Red\"," +
                                   "\"[email protected]\":\"#Collection(NS.Color)\"," +
                                   "\"FavoriteColors\":[\"0\", \"1\"]" +
                                   "}";

            var controllers = new[] { typeof(UntypedSimpleOpenCustomersController) };
            var server      = TestServerFactory.Create(controllers, (config) =>
            {
                config.MapODataServiceRoute("odata", "odata", GetUntypedEdmModel());
            });
            var client = TestServerFactory.CreateClient(server);

            // Act
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, _untypedCustomerRequestRooturl);

            request.Content = new StringContent(Payload);
            request.Content.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/json");
            HttpResponseMessage response = await client.SendAsync(request);

            // Assert
            Assert.True(response.IsSuccessStatusCode);
        }
예제 #16
0
        public async Task Post_OpenEntityType()
        {
            // Arrange
            const string Payload = "{" +
                                   "\"@odata.context\":\"http://localhost/odata/$metadata#OpenCustomers/$entity\"," +
                                   "\"CustomerId\":6,\"Name\":\"FirstName 6\"," +
                                   "\"Address\":{" +
                                   "\"Street\":\"Street 6\",\"City\":\"City 6\",\"Place\":\"Earth\",\"[email protected]\":\"#Guid\"," +
                                   "\"Token\":\"4DB52263-4382-4BCB-A63E-3129C1B5FA0D\"," +
                                   "\"Number\":990" +
                                   "}," +
                                   "\"Website\": \"WebSite #6\"," +
                                   "\"[email protected]\":\"#String\",\"Place\":\"My Dynamic Place\"," + // odata.type is necessary, otherwise it will get an ODataUntypedValue
                                   "\"[email protected]\":\"#Guid\",\"Token\":\"2c1f450a-a2a7-4fe1-a25d-4d9332fc0694\"," +
                                   "\"[email protected]\":\"#Collection(Double)\"," +
                                   "\"DoubleList\":[5.5, 4.4, 3.3]" +
                                   "}";

            const string RequestUri = "http://localhost/odata/SimpleOpenCustomers";

            var controllers = new[] { typeof(SimpleOpenCustomersController) };
            var server      = TestServerFactory.Create(controllers, (config) =>
            {
                config.MapODataServiceRoute("odata", "odata", GetEdmModel());
            });
            var client = TestServerFactory.CreateClient(server);

            // Act
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, RequestUri);

            request.Content = new StringContent(Payload);
            request.Content.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/json");
            HttpResponseMessage response = await client.SendAsync(request);

            // Assert
            Assert.True(response.IsSuccessStatusCode);
        }
예제 #17
0
        public async Task CanDispatch_ActionPayload_With_EdmEnumObject()
        {
            const string RequestUri = "http://localhost/odata/UntypedSimpleOpenCustomers(1)/NS.AddColor";
            const string Payload    = @"{ 
                ""Color"": ""0""
            }";

            var controllers = new[] { typeof(UntypedSimpleOpenCustomersController) };
            var server      = TestServerFactory.Create(controllers, (config) =>
            {
                config.MapODataServiceRoute("odata", "odata", GetUntypedEdmModel());
            });
            var client = TestServerFactory.CreateClient(server);

            // Act
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, RequestUri);

            request.Content = new StringContent(Payload);
            request.Content.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/json");
            HttpResponseMessage response = await client.SendAsync(request);

            // Assert
            Assert.True(response.IsSuccessStatusCode);
        }
예제 #18
0
        public async Task NullableEnumParameter_Works_WithNullEnumValue()
        {
            // Arrange
            const string expect =
                "{" +
                "\"@odata.context\":\"http://localhost/odata/$metadata#Edm.Boolean\",\"value\":false" +
                "}";

            var controllers = new[] { typeof(NullableEnumValueController) };
            var server      = TestServerFactory.Create(controllers, (config) =>
            {
                config.MapODataServiceRoute("odata", "odata", GetSampleModel());
            });
            HttpClient client = TestServerFactory.CreateClient(server);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get,
                                                                "http://localhost/odata/NullableEnumFunction(ColorParameter=null)");

            // Act
            HttpResponseMessage respone = await client.SendAsync(request);

            // Assert
            Assert.Equal(expect, await respone.Content.ReadAsStringAsync());
        }