private static void AssertODataVersion4JsonResponse(string expectedContent, HttpResponseMessage actual) { Assert.NotNull(actual); Assert.Equal(HttpStatusCode.OK, actual.StatusCode); Assert.Equal(ODataTestUtil.ApplicationJsonMediaTypeWithQuality.MediaType, actual.Content.Headers.ContentType.MediaType); Assert.Equal(ODataTestUtil.Version4NumberString, ODataTestUtil.GetDataServiceVersion(actual.Content.Headers)); ODataTestUtil.VerifyResponse(actual.Content, expectedContent); }
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.ApplicationJsonMediaTypeWithQuality)); } using (HttpServer host = new HttpServer(configuration)) using (HttpClient client = new HttpClient(host)) { // Arrange #1: this request should return response in OData json format using (HttpRequestMessage requestWithJsonHeader = ODataTestUtil.GenerateRequestMessage( CreateAbsoluteUri("People(10)?$format=application/json"))) // Act #1 using (HttpResponseMessage response = client.SendAsync(requestWithJsonHeader).Result) { // Assert #1 AssertODataVersion4JsonResponse(Resources.PersonEntryInJsonLight, response); } // Arrange #2: when the query string is not present, request should be handled by the regular Json // Formatter using (HttpRequestMessage requestWithNonODataJsonHeader = ODataTestUtil.GenerateRequestMessage( CreateAbsoluteUri("People(10)"))) // Act #2 using (HttpResponseMessage response = client.SendAsync(requestWithNonODataJsonHeader).Result) { // Assert #2 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.VerifyResponse(response.Content, Resources.PersonEntryInPlainOldJson); } // Arrange #3: this request should return response in OData json format using (HttpRequestMessage requestWithJsonHeader = ODataTestUtil.GenerateRequestMessage( CreateAbsoluteUri("President?$format=application/json"))) // Act #3 using (HttpResponseMessage response = client.SendAsync(requestWithJsonHeader).Result) { // Assert #3 AssertODataVersion4JsonResponse(Resources.GetString("PresidentInJsonLightMinimalMetadata.json"), response); } } } }