Example #1
0
        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]);
        }
Example #2
0
        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);
            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);
        }