public void PrimitiveTypesSerializeAsOData(Type valueType, object value, MediaTypeHeaderValue mediaType, string resourceName) { string expectedEntity = Resources.GetString(resourceName); Assert.NotNull(expectedEntity); ODataConventionModelBuilder modelBuilder = new ODataConventionModelBuilder(); modelBuilder.EntitySet <WorkItem>("WorkItems"); IEdmModel model = modelBuilder.GetEdmModel(); string actualEntity; using (HttpConfiguration configuration = CreateConfiguration()) using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/WorkItems(10)/ID")) { request.SetConfiguration(configuration); IEdmProperty property = model.EntityContainers().Single().EntitySets().Single().ElementType.Properties().First(); request.SetEdmModel(model); request.SetODataPath(new ODataPath(new PropertyAccessPathSegment(property))); request.SetFakeODataRouteName(); ODataMediaTypeFormatter formatter = CreateFormatter(request); formatter.SupportedMediaTypes.Add(mediaType); Type type = (value != null) ? value.GetType() : typeof(Nullable <int>); using (ObjectContent content = new ObjectContent(type, value, formatter)) { actualEntity = content.ReadAsStringAsync().Result; } } bool isJson = resourceName.EndsWith(".json"); if (isJson) { JsonAssert.Equal(expectedEntity, actualEntity); } else { Assert.Xml.Equal(expectedEntity, actualEntity); } }
private static void WriteToStreamAsyncReturnsODataRepresentation(string expectedContent, bool json) { ODataConventionModelBuilder modelBuilder = new ODataConventionModelBuilder(); modelBuilder.EntitySet <WorkItem>("WorkItems"); IEdmModel model = modelBuilder.GetEdmModel(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/WorkItems(10)"); HttpConfiguration configuration = new HttpConfiguration(); string routeName = "Route"; configuration.Routes.MapODataRoute(routeName, null, model); request.SetConfiguration(configuration); request.SetEdmModel(model); IEdmEntitySet entitySet = model.EntityContainers().Single().EntitySets().Single(); request.SetODataPath(new ODataPath(new EntitySetPathSegment(entitySet), new KeyValuePathSegment("10"))); request.SetODataRouteName(routeName); ODataMediaTypeFormatter formatter; if (json) { formatter = CreateFormatterWithJson(model, request, ODataPayloadKind.Entry); } else { formatter = CreateFormatter(model, request, ODataPayloadKind.Entry); } ObjectContent <WorkItem> content = new ObjectContent <WorkItem>( (WorkItem)TypeInitializer.GetInstance(SupportedTypes.WorkItem), formatter); string actualContent = content.ReadAsStringAsync().Result; if (json) { JsonAssert.Equal(expectedContent, actualContent); } else { RegexReplacement replaceUpdateTime = new RegexReplacement( "<updated>*.*</updated>", "<updated>UpdatedTime</updated>"); Assert.Xml.Equal(expectedContent, actualContent, replaceUpdateTime); } }
public static void VerifyJsonResponse(HttpContent actualContent, string expected) { string actual = actualContent.ReadAsStringAsync().Result; JsonAssert.Equal(expected, actual); }