private static HttpConfiguration CreateConfiguration(bool tracingEnabled = false)
        {
            IEdmModel         model         = ODataTestUtil.GetEdmModel();
            HttpConfiguration configuration = CreateConfiguration(model);

            if (tracingEnabled)
            {
                configuration.Services.Replace(typeof(ITraceWriter), new Mock <ITraceWriter>().Object);
            }

            return(configuration);
        }
        public void Conditionally_Support_OData_If_Query_String_Present()
        {
            ODataMediaTypeFormatter odataFormatter = _serverFormatter;

            odataFormatter.SupportedMediaTypes.Clear();
            odataFormatter.MediaTypeMappings.Add(new ODataMediaTypeMapping(ODataTestUtil.ApplicationAtomMediaTypeWithQuality));
            odataFormatter.MediaTypeMappings.Add(new ODataMediaTypeMapping(ODataTestUtil.ApplicationJsonMediaTypeWithQuality));
            _config.Formatters.Insert(0, odataFormatter);

            using (HttpServer host = new HttpServer(_config))
            {
                _client = new HttpClient(host);
                // this request should return response in OData atom format
                HttpRequestMessage requestMessage = ODataTestUtil.GenerateRequestMessage(new Uri(baseAddress + "People(10)?format=odata"), isAtom: true);
                using (HttpResponseMessage response = _client.SendAsync(requestMessage).Result)
                {
                    Assert.NotNull(response);
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                    Assert.Equal(ODataTestUtil.ApplicationAtomMediaTypeWithQuality.MediaType, response.Content.Headers.ContentType.MediaType);
                    Assert.Equal(ODataTestUtil.Version3NumberString, ODataTestUtil.GetDataServiceVersion(response.Content.Headers));
                    ODataTestUtil.VerifyResponse(response.Content, BaselineResource.EntryTypePersonAtom);
                }

                // this request should return response in OData json format
                HttpRequestMessage messageWithJsonHeader = ODataTestUtil.GenerateRequestMessage(new Uri(baseAddress + "People(10)?format=odata"), isAtom: false);
                using (HttpResponseMessage response = _client.SendAsync(messageWithJsonHeader).Result)
                {
                    Assert.NotNull(response);
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                    Assert.Equal(ODataTestUtil.ApplicationJsonMediaType.MediaType, response.Content.Headers.ContentType.MediaType);
                    Assert.Equal(ODataTestUtil.Version3NumberString, ODataTestUtil.GetDataServiceVersion(response.Content.Headers));

                    // this request should be handled by OData Json
                    ODataTestUtil.VerifyJsonResponse(response.Content, BaselineResource.EntryTypePersonODataJson);
                }

                // when the query string is not present, request should be handled by the regular Json Formatter
                messageWithJsonHeader = ODataTestUtil.GenerateRequestMessage(new Uri(baseAddress + "People(10)"), isAtom: false);

                using (HttpResponseMessage response = _client.SendAsync(messageWithJsonHeader).Result)
                {
                    Assert.NotNull(response);
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                    Assert.Equal(response.Content.Headers.ContentType.MediaType, ODataTestUtil.ApplicationJsonMediaTypeWithQuality.MediaType);
                    Assert.Null(ODataTestUtil.GetDataServiceVersion(response.Content.Headers));

                    ODataTestUtil.VerifyJsonResponse(response.Content, BaselineResource.EntryTypePersonRegularJson);
                }
            }
        }
        public void TryMatchMediaTypeWithNonRawvalueRequestDoesntMatchRequest()
        {
            IEdmModel model = ODataTestUtil.GetEdmModel();
            PropertyAccessPathSegment propertySegment = new PropertyAccessPathSegment((model.GetEdmType(typeof(FormatterPerson)) as IEdmEntityType).FindProperty("Age"));
            ODataPath path = new ODataPath(new EntitySetPathSegment("People"), new KeyValuePathSegment("1"), propertySegment);
            ODataPrimitiveValueMediaTypeMapping mapping = new ODataPrimitiveValueMediaTypeMapping();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/People(1)/Age/");

            request.ODataProperties().Model = model;
            request.ODataProperties().Path  = path;

            double mapResult = mapping.TryMatchMediaType(request);

            Assert.Equal(0, mapResult);
        }
        public void Get_Entry_In_OData_Atom_Format()
        {
            using (HttpServer host = new HttpServer(_config))
            {
                _client = new HttpClient(host);
                HttpRequestMessage requestMessage = new HttpRequestMessage(System.Net.Http.HttpMethod.Get, new Uri(baseAddress + "People(10)"));
                requestMessage.Headers.Accept.Add(_atomMediaType);
                requestMessage.Headers.Add("DataServiceVersion", "2.0");
                requestMessage.Headers.Add("MaxDataServiceVersion", "3.0");
                using (HttpResponseMessage response = _client.SendAsync(requestMessage).Result)
                {
                    Assert.NotNull(response);
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                    Assert.Equal(response.Content.Headers.ContentType.MediaType, _atomMediaType.MediaType);
                    Assert.Equal(ODataTestUtil.GetDataServiceVersion(response.Content.Headers), ODataTestUtil.Version3NumberString);

                    ODataTestUtil.VerifyResponse(response.Content, BaselineResource.EntryTypePersonAtom);
                }
            }
        }
        public void Support_Only_OData_Atom_Format()
        {
            ODataMediaTypeFormatter odataFormatter = _serverFormatter;

            odataFormatter.SupportedMediaTypes.Remove(ODataTestUtil.ApplicationJsonMediaType);
            _config.Formatters.Insert(0, odataFormatter);

            using (HttpServer host = new HttpServer(_config))
            {
                _client = new HttpClient(host);
                HttpRequestMessage requestMessage = new HttpRequestMessage(System.Net.Http.HttpMethod.Get, new Uri(baseAddress + "People(10)"));
                requestMessage.Headers.Accept.Add(_atomMediaType);
                requestMessage.Headers.Add("DataServiceVersion", "2.0");
                requestMessage.Headers.Add("MaxDataServiceVersion", "3.0");

                using (HttpResponseMessage response = _client.SendAsync(requestMessage).Result)
                {
                    Assert.NotNull(response);
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                    Assert.Equal(response.Content.Headers.ContentType.MediaType, _atomMediaType.MediaType);
                    Assert.Equal(ODataTestUtil.GetDataServiceVersion(response.Content.Headers), ODataTestUtil.Version3NumberString);

                    ODataTestUtil.VerifyResponse(response.Content, BaselineResource.EntryTypePersonAtom);
                }

                HttpRequestMessage messageWithJsonHeader = new HttpRequestMessage(System.Net.Http.HttpMethod.Get, new Uri(baseAddress + "People(10)"));
                messageWithJsonHeader.Headers.Accept.Add(ODataTestUtil.ApplicationJsonMediaTypeWithQuality);
                messageWithJsonHeader.Headers.Add("DataServiceVersion", "2.0");
                messageWithJsonHeader.Headers.Add("MaxDataServiceVersion", "3.0");

                using (HttpResponseMessage response = _client.SendAsync(messageWithJsonHeader).Result)
                {
                    Assert.NotNull(response);
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                    Assert.Equal(response.Content.Headers.ContentType.MediaType, ODataTestUtil.ApplicationJsonMediaTypeWithQuality.MediaType);
                    Assert.Null(ODataTestUtil.GetDataServiceVersion(response.Content.Headers));

                    ODataTestUtil.VerifyJsonResponse(response.Content, BaselineResource.EntryTypePersonRegularJson);
                }
            }
        }
        public void SupportOnlyODataAtomFormat()
        {
            // Arrange #1 and #2
            using (HttpConfiguration configuration = CreateConfiguration())
            {
                foreach (ODataMediaTypeFormatter odataFormatter in
                         configuration.Formatters.OfType <ODataMediaTypeFormatter>())
                {
                    odataFormatter.SupportedMediaTypes.Remove(ODataMediaTypes.ApplicationJsonODataVerbose);
                    odataFormatter.SupportedMediaTypes.Remove(ODataMediaTypes.ApplicationJson);
                }

                using (HttpServer host = new HttpServer(configuration))
                    using (HttpClient client = new HttpClient(host))
                    {
                        // Arrange #1
                        using (HttpRequestMessage request = CreateRequestWithDataServiceVersionHeaders("People(10)",
                                                                                                       ODataTestUtil.ApplicationAtomMediaTypeWithQuality))
                            // Act #1
                            using (HttpResponseMessage response = client.SendAsync(request).Result)
                            {
                                // Assert #1
                                AssertODataVersion3AtomResponse(Resources.PersonEntryInAtom, response);
                            }

                        // Arrange #2
                        using (HttpRequestMessage request = CreateRequestWithDataServiceVersionHeaders("People(10)",
                                                                                                       ODataTestUtil.ApplicationJsonMediaTypeWithQuality))
                            // Act #2
                            using (HttpResponseMessage response = client.SendAsync(request).Result)
                            {
                                // Assert #2
                                Assert.NotNull(response);
                                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                                Assert.Equal(ODataTestUtil.ApplicationJsonMediaTypeWithQuality.MediaType,
                                             response.Content.Headers.ContentType.MediaType);
                                ODataTestUtil.VerifyJsonResponse(response.Content, Resources.PersonEntryInPlainOldJson);
                            }
                    }
            }
        }
Exemplo n.º 7
0
        public void PostEntry_InODataAtomFormat()
        {
            var _config = new HttpConfiguration();

            _config.EnableOData(ODataTestUtil.GetEdmModel());

            using (HttpServer host = new HttpServer(_config))
            {
                var _client = new HttpClient(host);
                HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri(baseAddress + "People"));
                requestMessage.Content = new StringContent(BaselineResource.EntryTypePersonAtom, Encoding.UTF8, "application/atom+xml");
                using (HttpResponseMessage response = _client.SendAsync(requestMessage).Result)
                {
                    Assert.NotNull(response);
                    Assert.Equal(HttpStatusCode.Created, response.StatusCode);
                    Assert.Equal("application/atom+xml", response.Content.Headers.ContentType.MediaType);

                    ODataTestUtil.VerifyResponse(response.Content, BaselineResource.EntryTypePersonAtom);
                }
            }
        }
Exemplo n.º 8
0
        public void PostEntry_InODataJsonVerboseFormat()
        {
            var config = new HttpConfiguration();

            config.Routes.MapODataRoute(ODataTestUtil.GetEdmModel());

            using (HttpServer host = new HttpServer(config))
            {
                var client = new HttpClient(host);
                HttpRequestMessage requestMessage = new HttpRequestMessage(System.Net.Http.HttpMethod.Post, new Uri(baseAddress + "People"));
                requestMessage.Content = new StringContent(Resources.PersonRequestEntryInPlainOldJson);
                requestMessage.Content.Headers.ContentType = ODataTestUtil.ApplicationJsonMediaType;
                using (HttpResponseMessage response = client.SendAsync(requestMessage).Result)
                {
                    Assert.NotNull(response);
                    Assert.Equal(HttpStatusCode.Created, response.StatusCode);
                    Assert.Equal("application/json", response.Content.Headers.ContentType.MediaType);

                    ODataTestUtil.VerifyJsonResponse(response.Content, Resources.PersonEntryInJsonVerbose);
                }
            }
        }
        public void Get_Entry_In_OData_Json_Format()
        {
            using (HttpServer host = new HttpServer(_config))
            {
                _client = new HttpClient(host);
                HttpRequestMessage requestMessage = new HttpRequestMessage(System.Net.Http.HttpMethod.Get, new Uri(baseAddress + "People(10)"));
                requestMessage.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata=verbose"));
                requestMessage.Headers.Add("DataServiceVersion", "2.0");
                requestMessage.Headers.Add("MaxDataServiceVersion", "3.0");

                using (HttpResponseMessage response = _client.SendAsync(requestMessage).Result)
                {
                    Assert.NotNull(response);
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                    Assert.Equal(response.Content.Headers.ContentType.MediaType, _jsonMediaType.MediaType);
                    Assert.Equal(ODataTestUtil.GetDataServiceVersion(response.Content.Headers), ODataTestUtil.Version3NumberString);

                    // this request should be handled by OData Json
                    ODataTestUtil.VerifyJsonResponse(response.Content, BaselineResource.EntryTypePersonODataJson);
                }
            }
        }
Exemplo n.º 10
0
        public void PostEntry_InODataJsonFormat()
        {
            var _config = new HttpConfiguration();

            _config.Routes.MapHttpRoute(ODataRouteNames.GetById, "{controller}({id})");
            _config.Routes.MapHttpRoute(ODataRouteNames.Default, "{controller}");
            _config.Formatters.Insert(0, new ODataMediaTypeFormatter(ODataTestUtil.GetEdmModel()));

            using (HttpServer host = new HttpServer(_config))
            {
                var _client = new HttpClient(host);
                HttpRequestMessage requestMessage = new HttpRequestMessage(System.Net.Http.HttpMethod.Post, new Uri(baseAddress + "People"));
                requestMessage.Content = new StringContent(BaselineResource.ODataJsonPersonRequest);
                requestMessage.Content.Headers.ContentType = ODataTestUtil.ApplicationJsonMediaType;
                using (HttpResponseMessage response = _client.SendAsync(requestMessage).Result)
                {
                    Assert.NotNull(response);
                    Assert.Equal(HttpStatusCode.Created, response.StatusCode);
                    Assert.Equal("application/json", response.Content.Headers.ContentType.MediaType);

                    ODataTestUtil.VerifyJsonResponse(response.Content, BaselineResource.EntryTypePersonODataJson);
                }
            }
        }
Exemplo n.º 11
0
 public ODataFormatterTests()
 {
     _config = new HttpConfiguration();
     _config.EnableOData(ODataTestUtil.GetEdmModel());
     _serverFormatters = _config.Formatters.OfType <ODataMediaTypeFormatter>();
 }