1 // Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. 2 3 using System.Net.Http; 4 using Xunit; 5 using Assert = Microsoft.TestCommon.AssertEx; 6 7 namespace System.Web.Http.Controllers 8 { 9 public class ResponseMessageResultConverterTest 10 { 11 private readonly ResponseMessageResultConverter _converter = new ResponseMessageResultConverter(); 12 private readonly HttpControllerContext _context = new HttpControllerContext(); 13 private readonly HttpRequestMessage _request = new HttpRequestMessage(); 14 ResponseMessageResultConverterTest()15 public ResponseMessageResultConverterTest() 16 { 17 _context.Request = _request; 18 _context.Configuration = new HttpConfiguration(); 19 } 20 21 [Fact] Convert_WhenValueIsResponseMessage_ReturnsResponseMessageWithRequestAssigned()22 public void Convert_WhenValueIsResponseMessage_ReturnsResponseMessageWithRequestAssigned() 23 { 24 HttpResponseMessage response = new HttpResponseMessage(); 25 26 var result = _converter.Convert(_context, response); 27 28 Assert.Same(response, result); 29 Assert.Same(_request, result.RequestMessage); 30 } 31 32 [Fact] Convert_WhenContextIsNull_Throws()33 public void Convert_WhenContextIsNull_Throws() 34 { 35 Assert.ThrowsArgumentNull(() => _converter.Convert(controllerContext: null, actionResult: new HttpResponseMessage()), "controllerContext"); 36 } 37 38 [Fact] Convert_WhenValueIsNull_Throws()39 public void Convert_WhenValueIsNull_Throws() 40 { 41 Assert.Throws<InvalidOperationException>(() => _converter.Convert(_context, null), 42 "A null value was returned where an instance of HttpResponseMessage was expected."); 43 } 44 45 [Fact] Convert_WhenValueIsIncompatibleType_Throws()46 public void Convert_WhenValueIsIncompatibleType_Throws() 47 { 48 Assert.Throws<InvalidCastException>(() => _converter.Convert(_context, "42"), 49 "Unable to cast object of type 'System.String' to type 'System.Net.Http.HttpResponseMessage'."); 50 } 51 } 52 } 53