public void ConditionallySupportODataIfQueryStringPresent() { // Arrange #1, #2 and #3 using (HttpConfiguration configuration = CreateConfiguration()) { foreach (ODataMediaTypeFormatter odataFormatter in configuration.Formatters.OfType <ODataMediaTypeFormatter>()) { odataFormatter.SupportedMediaTypes.Clear(); odataFormatter.MediaTypeMappings.Add(new ODataMediaTypeMapping(ODataTestUtil.ApplicationAtomMediaTypeWithQuality)); odataFormatter.MediaTypeMappings.Add(new ODataMediaTypeMapping(ODataTestUtil.ApplicationJsonMediaTypeWithQuality)); } using (HttpServer host = new HttpServer(configuration)) using (HttpClient client = new HttpClient(host)) { // Arrange #1 this request should return response in OData atom format using (HttpRequestMessage request = ODataTestUtil.GenerateRequestMessage( CreateAbsoluteUri("People(10)?format=odata"), isAtom: true)) // Act #1 using (HttpResponseMessage response = client.SendAsync(request).Result) { // Assert #1 AssertODataVersion3AtomResponse(Resources.PersonEntryInAtom, response); } // Arrange #2: this request should return response in OData json format using (HttpRequestMessage requestWithJsonHeader = ODataTestUtil.GenerateRequestMessage( CreateAbsoluteUri("People(10)?format=odata"), isAtom: false)) // Act #2 using (HttpResponseMessage response = client.SendAsync(requestWithJsonHeader).Result) { // Assert #2 AssertODataVersion3JsonResponse(Resources.PersonEntryInJsonVerbose, response); } // Arrange #3: when the query string is not present, request should be handled by the regular Json // Formatter using (HttpRequestMessage requestWithNonODataJsonHeader = ODataTestUtil.GenerateRequestMessage( CreateAbsoluteUri("People(10)"), isAtom: false)) // Act #3 using (HttpResponseMessage response = client.SendAsync(requestWithNonODataJsonHeader).Result) { // Assert #3 Assert.NotNull(response); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(ODataTestUtil.ApplicationJsonMediaTypeWithQuality.MediaType, response.Content.Headers.ContentType.MediaType); Assert.Null(ODataTestUtil.GetDataServiceVersion(response.Content.Headers)); ODataTestUtil.VerifyJsonResponse(response.Content, Resources.PersonEntryInPlainOldJson); } } } }
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); } } }