1 // Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. 2 3 using System.Collections.Generic; 4 using System.Net.Http.Formatting; 5 using Moq; 6 using Xunit; 7 using Xunit.Extensions; 8 using Assert = Microsoft.TestCommon.AssertEx; 9 10 namespace System.Net.Http 11 { 12 public class HttpResponseMessageExtensionsTest 13 { 14 private readonly HttpResponseMessage _response = new HttpResponseMessage(); 15 private readonly Mock<MediaTypeFormatter> _formatterMock = new Mock<MediaTypeFormatter>(); 16 HttpResponseMessageExtensionsTest()17 public HttpResponseMessageExtensionsTest() 18 { 19 _formatterMock.Setup(f => f.CanWriteType(It.IsAny<Type>())).Returns(true); 20 } 21 22 [Fact] TryGetContentValue_WhenResponseParameterIsNull_Throws()23 public void TryGetContentValue_WhenResponseParameterIsNull_Throws() 24 { 25 object value; 26 Assert.ThrowsArgumentNull(() => HttpResponseMessageExtensions.TryGetContentValue<object>(null, out value), "response"); 27 } 28 29 [Theory] 30 [InlineData(default(bool))] 31 [InlineData(default(int))] 32 [InlineData(default(object))] TryGetContentValue_WhenResponseHasNoContent_ReturnsFalse(T expectedResult)33 public void TryGetContentValue_WhenResponseHasNoContent_ReturnsFalse<T>(T expectedResult) 34 { 35 T value; 36 37 Assert.False(_response.TryGetContentValue<T>(out value)); 38 39 Assert.Equal(expectedResult, value); 40 } 41 42 43 [Theory] 44 [InlineData(default(bool))] 45 [InlineData(default(int))] 46 [InlineData(default(object))] TryGetContentValue_WhenResponseHasNonObjectContent_ReturnsFalse(T expectedResult)47 public void TryGetContentValue_WhenResponseHasNonObjectContent_ReturnsFalse<T>(T expectedResult) 48 { 49 _response.Content = new StringContent("43"); 50 T value; 51 52 Assert.False(_response.TryGetContentValue<T>(out value)); 53 54 Assert.Equal(expectedResult, value); 55 } 56 57 [Theory] 58 [InlineData(default(bool))] 59 [InlineData(default(int))] 60 [InlineData(default(object))] TryGetContentValue_WhenResponseHasObjectContentWithNullValue_ReturnsFalse(T expectedResult)61 public void TryGetContentValue_WhenResponseHasObjectContentWithNullValue_ReturnsFalse<T>(T expectedResult) 62 { 63 _response.Content = new ObjectContent(typeof(object), null, _formatterMock.Object); 64 T value; 65 66 Assert.False(_response.TryGetContentValue<T>(out value)); 67 68 Assert.Equal(expectedResult, value); 69 } 70 71 [Theory] 72 [InlineData(default(bool))] 73 [InlineData(default(int))] TryGetContentValue_WhenResponseHasObjectContentWithIncompatibleValue_ReturnsFalse(T expectedResult)74 public void TryGetContentValue_WhenResponseHasObjectContentWithIncompatibleValue_ReturnsFalse<T>(T expectedResult) 75 { 76 _response.Content = new ObjectContent<string>("42", _formatterMock.Object); 77 T value; 78 79 Assert.False(_response.TryGetContentValue<T>(out value)); 80 81 Assert.Equal(expectedResult, value); 82 } 83 84 [Fact] TryGetContentValue_WhenResponseHasObjectContentWithCompatibleValue_ReturnsTrue()85 public void TryGetContentValue_WhenResponseHasObjectContentWithCompatibleValue_ReturnsTrue() 86 { 87 List<string> value = new List<string>(); 88 _response.Content = new ObjectContent<List<string>>(value, _formatterMock.Object); 89 IList<string> result; 90 91 Assert.True(_response.TryGetContentValue(out result)); 92 93 Assert.Same(value, result); 94 } 95 96 [Fact] TryGetContentValue_WhenResponseHasObjectContentValueTypeValue_RetrievingAsObjectReturnsTrue()97 public void TryGetContentValue_WhenResponseHasObjectContentValueTypeValue_RetrievingAsObjectReturnsTrue() 98 { 99 _response.Content = new ObjectContent<int>(32, _formatterMock.Object); 100 object result; 101 102 Assert.True(_response.TryGetContentValue(out result)); 103 104 Assert.Equal(32, result); 105 } 106 } 107 } 108