Ejemplo n.º 1
0
        public void ODataCollectionSerializer_SerializeIQueryableOfIEdmEntityObject()
        {
            // Arrange
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<CollectionSerializerCustomer>("CollectionSerializerCustomers");
            IEdmModel model = builder.GetEdmModel();
            var controllers = new[] { typeof(CollectionSerializerCustomersController) };

            using (HttpConfiguration configuration = controllers.GetHttpConfiguration())
            {
                configuration.MapODataServiceRoute("odata", routePrefix: null, model: model);
                using (HttpServer host = new HttpServer(configuration))
                using (HttpClient client = new HttpClient(host))
                using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/CollectionSerializerCustomers?$select=ID"))
                {
                    // Act
                    using (HttpResponseMessage response = client.SendAsync(request).Result)
                    {
                        // Assert
                        response.EnsureSuccessStatusCode();
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void EnumSerializer_HasMetadataType()
        {
            // Arrange
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<EnumCustomer>("EnumCustomers");
            IEdmModel model = builder.GetEdmModel();
            var controllers = new[] { typeof(EnumCustomersController) };

            using (HttpConfiguration configuration = controllers.GetHttpConfiguration())
            {
                configuration.MapODataServiceRoute("odata", routePrefix: null, model: model);
                using (HttpServer host = new HttpServer(configuration))
                using (HttpClient client = new HttpClient(host))
                using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/EnumCustomers"))
                {
                    request.Content = new StringContent(
                        string.Format(@"{{'@odata.type':'#System.Web.OData.Formatter.EnumCustomer',
                            'ID':0,'Color':'Green, Blue','Colors':['Red','Red, Blue']}}"));
                    request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                    request.Headers.Accept.ParseAdd("application/json;odata.metadata=full");

                    // Act
                    using (HttpResponseMessage response = client.SendAsync(request).Result)
                    {
                        // Assert
                        response.EnsureSuccessStatusCode();
                        dynamic payload = JToken.Parse(response.Content.ReadAsStringAsync().Result);
                        Assert.Equal("#System.Web.OData.Formatter.EnumCustomer", payload["@odata.type"].Value);
                        Assert.Equal("#System.Web.OData.Builder.TestModels.Color", payload["*****@*****.**"].Value);
                        Assert.Equal("#Collection(System.Web.OData.Builder.TestModels.Color)", payload["*****@*****.**"].Value);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public void RequestProperty_HasCorrectContextUrl()
        {
            // Arrange
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<EnumCustomer>("EnumCustomers");
            IEdmModel model = builder.GetEdmModel();
            var controllers = new[] { typeof(EnumCustomersController) };

            using (HttpConfiguration configuration = controllers.GetHttpConfiguration())
            {
                configuration.MapODataServiceRoute("odata", routePrefix: null, model: model);
                using (HttpServer host = new HttpServer(configuration))
                using (HttpClient client = new HttpClient(host))

                // Act
                using (HttpResponseMessage response = client.GetAsync("http://localhost/EnumCustomers(5)/Color").Result)
                {
                    // Assert
                    response.EnsureSuccessStatusCode();
                    JObject payload = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                    Assert.Equal("http://localhost/$metadata#EnumCustomers(5)/Color", payload.GetValue("@odata.context"));
                }
            }
        }
Ejemplo n.º 4
0
        public void EnumTypeRoundTripTest()
        {
            // Arrange
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<EnumCustomer>("EnumCustomers");
            IEdmModel model = builder.GetEdmModel();
            var controllers = new[] { typeof(EnumCustomersController) };

            using (HttpConfiguration configuration = controllers.GetHttpConfiguration())
            {
                configuration.MapODataServiceRoute("odata", routePrefix: null, model: model);
                using (HttpServer host = new HttpServer(configuration))
                using (HttpClient client = new HttpClient(host))
                using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/EnumCustomers"))
                {
                    request.Content = new StringContent(
                        string.Format(@"{{'@odata.type':'#System.Web.OData.Formatter.EnumCustomer',
                            'ID':0,'Color':'Green, Blue','Colors':['Red','Red, Blue']}}"));
                    request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                    request.Headers.Accept.ParseAdd("application/json");

                    // Act
                    using (HttpResponseMessage response = client.SendAsync(request).Result)
                    {
                        // Assert
                        response.EnsureSuccessStatusCode();
                        var customer = response.Content.ReadAsAsync<JObject>().Result;
                        Assert.Equal(0, customer["ID"]);
                        Assert.Equal(Color.Green | Color.Blue, Enum.Parse(typeof(Color), customer["Color"].ToString()));
                        var colors = customer["Colors"].Select(c => Enum.Parse(typeof(Color), c.ToString()));
                        Assert.Equal(2, colors.Count());
                        Assert.Contains(Color.Red, colors);
                        Assert.Contains(Color.Red | Color.Blue, colors);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public void RequestCollectionProperty_HasNextPageLine_Count()
        {
            // Arrange
            const string expect = @"{
              ""@odata.context"": ""http://localhost/$metadata#Collection(System.Web.OData.Builder.TestModels.Color)"",
              ""@odata.count"": 3,
              ""@odata.nextLink"": ""http://localhost/EnumCustomers(5)/Colors?$count=true&$skip=2"",
              ""value"": [
            ""Blue"",
            ""Green""
              ]
            }";
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<EnumCustomer>("EnumCustomers");
            IEdmModel model = builder.GetEdmModel();
            var controllers = new[] { typeof(EnumCustomersController) };

            using (HttpConfiguration configuration = controllers.GetHttpConfiguration())
            {
                configuration.MapODataServiceRoute("odata", routePrefix: null, model: model);
                using (HttpServer host = new HttpServer(configuration))
                using (HttpClient client = new HttpClient(host))

                // Act
                using (HttpResponseMessage response = client.GetAsync("http://localhost/EnumCustomers(5)/Colors?$count=true").Result)
                {
                    // Assert
                    response.EnsureSuccessStatusCode();
                    JObject payload = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                    Assert.Equal(expect, payload.ToString());
                }
            }
        }
Ejemplo n.º 6
0
        public void EnumKeySimpleSerializerTest()
        {
            // Arrange
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<EnumCustomer>("EnumKeyCustomers");
            builder.EntityType<EnumCustomer>().HasKey(c => c.Color);
            IEdmModel model = builder.GetEdmModel();
            var controllers = new[] { typeof(EnumKeyCustomersController) };

            HttpConfiguration configuration = controllers.GetHttpConfiguration();
            configuration.MapODataServiceRoute("odata", routePrefix: null, model: model);
            HttpServer host = new HttpServer(configuration);
            HttpClient client = new HttpClient(host);

            // Act
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get,
                "http://localhost/EnumKeyCustomers(System.Web.OData.Builder.TestModels.Color'Red')");
            HttpResponseMessage response = client.SendAsync(request).Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            var customer = response.Content.ReadAsAsync<JObject>().Result;
            Assert.Equal(9, customer["ID"]);
            Assert.Equal(Color.Red, Enum.Parse(typeof(Color), customer["Color"].ToString()));
            var colors = customer["Colors"].Select(c => Enum.Parse(typeof(Color), c.ToString()));
            Assert.Equal(2, colors.Count());
            Assert.Contains(Color.Blue, colors);
            Assert.Contains(Color.Red, colors);
        }
Ejemplo n.º 7
0
        [InlineData("KeyCustomers4")] // with [FromODataUriAttribute] int attribute routing
        public void RelatedKeySimpleSerializerTest(string entitySet)
        {
            // Arrange
            IEdmModel model = GetKeyCustomerOrderModel();
            var controllers = new[]
            {
                typeof(KeyCustomers1Controller), typeof(KeyCustomers2Controller),
                typeof(KeyCustomerOrderController)
            };

            HttpConfiguration configuration = controllers.GetHttpConfiguration();
            configuration.MapODataServiceRoute("odata", routePrefix: null, model: model);
            HttpServer host = new HttpServer(configuration);
            HttpClient client = new HttpClient(host);

            // Act
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Delete,
                "http://localhost/" + entitySet + "(6)/Orders(StringKey='my',DateKey=2016-05-11,GuidKey=46538EC2-E497-4DFE-A039-1C22F0999D6C)/$ref");
            HttpResponseMessage response = client.SendAsync(request).Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            var customer = response.Content.ReadAsAsync<JObject>().Result;
            Assert.Equal("6+my", customer["value"]);
        }
Ejemplo n.º 8
0
        [InlineData("KeyCustomers4")] // with [FromODataUriAttribute] int attribute routing  
        public void SingleKeySimpleSerializerTest(string entitySet)
        {
            // Arrange
            IEdmModel model = GetKeyCustomerOrderModel();
            var controllers = new[] { typeof(KeyCustomers1Controller), typeof(KeyCustomers2Controller), typeof(KeyCustomerOrderController) };

            HttpConfiguration configuration = controllers.GetHttpConfiguration();
            configuration.MapODataServiceRoute("odata", routePrefix: null, model: model);
            HttpServer host = new HttpServer(configuration);
            HttpClient client = new HttpClient(host);

            // Act
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get,
                "http://localhost/" + entitySet + "(5)");
            HttpResponseMessage response = client.SendAsync(request).Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            var customer = response.Content.ReadAsAsync<JObject>().Result;
            Assert.Equal(5, customer["value"]);
        }