public void NullCollectionProperties_NormalPass_FromEntity(string propertyName, NullCollectionsTestMode testMode) { // Arrange NullCollectionsTestsController.TestObject = new NullCollectionsTestsModel(); // Act HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/NullCollectionsTests/"); HttpResponseMessage response = _client.SendAsync(request).Result; // Assert response.EnsureSuccessStatusCode(); string responseJson = response.Content.ReadAsStringAsync().Result; dynamic result = JToken.Parse(responseJson); if (propertyName == "NullableColors") { Assert.Equal(new[] { Color.Red.ToString() }, (IEnumerable <string>)result[propertyName] .Values <string>()); } else if (propertyName == "ComplexCollection") { Assert.Equal(new[] { 42 }, ((IEnumerable <JObject>)result[propertyName].Values <JObject>()) .Select(v => (int)v.Property("A"))); } else { Assert.Equal(new[] { 42 }, (IEnumerable <int>)result[propertyName].Values <int>()); } }
public void NullCollectionProperties_NormalFail_FromParentComplex(string propertyName, NullCollectionsTestMode testMode) { // Arrange NullCollectionsTestsModel testObject = new NullCollectionsTestsModel(); testObject.ParentComplex.GetType().GetProperty(propertyName).SetValue(testObject.ParentComplex, null); NullCollectionsTestsController.TestObject = testObject; // Act HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/NullCollectionsTests/"); try { HttpResponseMessage response = _client.SendAsync(request).Result; response.EnsureSuccessStatusCode(); } catch (Exception e) { // Assert Assert.Equal("Null collections cannot be serialized.", e.InnerException.Message); } }
public void NullCollectionProperties_SerializeAsEmpty_FromParentComplex(string propertyName, NullCollectionsTestMode testMode) { // Arrange NullCollectionsTestsModel testObject = new NullCollectionsTestsModel(); testObject.ParentComplex.GetType().GetProperty(propertyName).SetValue(testObject.ParentComplex, null); _config.SetSerializeNullCollectionsAsEmpty(true); NullCollectionsTestsController.TestObject = testObject; // Act HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/NullCollectionsTests/"); HttpResponseMessage response = _client.SendAsync(request).Result; // Assert response.EnsureSuccessStatusCode(); string responseJson = response.Content.ReadAsStringAsync().Result; dynamic result = JToken.Parse(responseJson); var parent3 = result["ParentComplex"]; Assert.NotNull(parent3[propertyName]); IEnumerable <JObject> collection2 = parent3[propertyName].Values <JObject>(); Assert.Empty(collection2); }
public void NullCollectionProperties_DoNotSerialize_FromEntity(string propertyName, NullCollectionsTestMode testMode) { // Arrange NullCollectionsTestsModel testObject = new NullCollectionsTestsModel(); testObject.GetType().GetProperty(propertyName).SetValue(testObject, null); _config.SetDoNotSerializeNullCollections(true); NullCollectionsTestsController.TestObject = testObject; // Act HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/NullCollectionsTests/"); HttpResponseMessage response = _client.SendAsync(request).Result; // Assert response.EnsureSuccessStatusCode(); string responseJson = response.Content.ReadAsStringAsync().Result; dynamic result = JToken.Parse(responseJson); Assert.Null(result[propertyName]); }