public void PrimitiveTypesDeserializeAsOData(string typeString, Type type, object value)
        {
            HttpConfiguration configuration = new HttpConfiguration();

            ODataConventionModelBuilder model = new ODataConventionModelBuilder();
            model.EntitySet<WorkItem>("WorkItems");

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/WorkItems(10)/ID");
            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;

            ODataMediaTypeFormatter formatter = new ODataMediaTypeFormatter(model.GetEdmModel()) { Request = request };
            ObjectContent content = new ObjectContent(type, value, formatter);

            var stream = content.ReadAsStreamAsync().Result;
            Assert.Equal(
                value,
                formatter.ReadFromStreamAsync(type, stream, content, new Mock<IFormatterLogger>().Object).Result);
        }
Example #2
0
        public void PrimitiveTypesDeserializeAsOData(Type valueType, object value, MediaTypeHeaderValue mediaType,
                                                     string resourceName)
        {
            string entity = Resources.GetString(resourceName);

            Assert.NotNull(entity);

            object expectedValue = value;

            ODataConventionModelBuilder modelBuilder = new ODataConventionModelBuilder();

            modelBuilder.EntitySet <WorkItem>("WorkItems");
            IEdmModel model = modelBuilder.GetEdmModel();

            object actualValue;

            using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/WorkItems(10)/ID"))
            {
                HttpConfiguration config = new HttpConfiguration();
                config.Routes.MapODataRoute("default", "", model);
                request.SetConfiguration(config);
                request.SetODataRouteName("default");
                request.SetEdmModel(model);

                ODataMediaTypeFormatter formatter = CreateFormatter(request);
                formatter.SupportedMediaTypes.Add(mediaType);

                using (StringContent content = new StringContent(entity))
                {
                    content.Headers.ContentType = mediaType;

                    using (Stream stream = content.ReadAsStreamAsync().Result)
                    {
                        actualValue = formatter.ReadFromStreamAsync(valueType, stream, content,
                                                                    new Mock <IFormatterLogger>().Object).Result;
                    }
                }
            }

            Assert.Equal(expectedValue, actualValue);
        }
Example #3
0
        public void PrimitiveTypesDeserializeAsOData(string typeString, Type type, object value)
        {
            HttpConfiguration configuration = new HttpConfiguration();

            ODataConventionModelBuilder model = new ODataConventionModelBuilder();

            model.EntitySet <WorkItem>("WorkItems");

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/WorkItems(10)/ID");

            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;

            ODataMediaTypeFormatter formatter = CreateFormatter(model.GetEdmModel(), request);
            ObjectContent           content   = new ObjectContent(type, value, formatter, "application/xml");

            var stream = content.ReadAsStreamAsync().Result;

            Assert.Equal(
                value,
                formatter.ReadFromStreamAsync(type, stream, content, new Mock <IFormatterLogger>().Object).Result);
        }