1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;
using System.Web.Http.Tracing.Tracers;
using Moq;
using Xunit;
using Xunit.Extensions;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Http.Tracing
{
public class TraceManagerTest
{
[Fact]
public void TraceManager_Is_In_Default_ServiceResolver()
{
// Arrange
HttpConfiguration config = new HttpConfiguration();
// Act
ITraceManager traceManager = config.Services.GetService(typeof (ITraceManager)) as ITraceManager;
// Assert
Assert.IsType<TraceManager>(traceManager);
}
[Theory]
[InlineData(typeof(IHttpControllerSelector))]
[InlineData(typeof(IHttpControllerActivator))]
[InlineData(typeof(IHttpActionSelector))]
[InlineData(typeof(IHttpActionInvoker))]
[InlineData(typeof(IActionValueBinder))]
[InlineData(typeof(IContentNegotiator))]
public void Initialize_Does_Not_Alter_Configuration_When_No_TraceWriter_Present(Type serviceType)
{
// Arrange
HttpConfiguration config = new HttpConfiguration();
object defaultService = config.Services.GetService(serviceType);
// Act
new TraceManager().Initialize(config);
// Assert
Assert.Same(defaultService.GetType(), config.Services.GetService(serviceType).GetType());
}
[Theory]
[InlineData(typeof(IHttpControllerSelector))]
[InlineData(typeof(IHttpControllerActivator))]
[InlineData(typeof(IHttpActionSelector))]
[InlineData(typeof(IHttpActionInvoker))]
[InlineData(typeof(IActionValueBinder))]
[InlineData(typeof(IContentNegotiator))]
public void Initialize_Alters_Configuration_When_TraceWriter_Present(Type serviceType)
{
// Arrange
HttpConfiguration config = new HttpConfiguration();
Mock<ITraceWriter> traceWriter = new Mock<ITraceWriter>() { CallBase = true };
config.Services.Replace(typeof(ITraceWriter), traceWriter.Object);
object defaultService = config.Services.GetService(serviceType);
// Act
new TraceManager().Initialize(config);
// Assert
Assert.NotSame(defaultService.GetType(), config.Services.GetService(serviceType).GetType());
}
[Fact]
public void Initialize_Does_Not_Alter_MessageHandlers_When_No_TraceWriter_Present()
{
// Arrange
HttpConfiguration config = new HttpConfiguration();
Mock<DelegatingHandler> mockHandler = new Mock<DelegatingHandler>() { CallBase = true };
config.MessageHandlers.Add(mockHandler.Object);
// Act
new TraceManager().Initialize(config);
// Assert
Assert.Equal(config.MessageHandlers[config.MessageHandlers.Count - 1].GetType(), mockHandler.Object.GetType());
}
[Fact]
public void Initialize_Alters_MessageHandlers_WhenTraceWriter_Present()
{
// Arrange
HttpConfiguration config = new HttpConfiguration();
Mock<ITraceWriter> traceWriter = new Mock<ITraceWriter>() { CallBase = true };
config.Services.Replace(typeof(ITraceWriter), traceWriter.Object);
Mock<DelegatingHandler> mockHandler = new Mock<DelegatingHandler>() { CallBase = true };
config.MessageHandlers.Add(mockHandler.Object);
// Act
new TraceManager().Initialize(config);
// Assert
Assert.IsAssignableFrom<RequestMessageHandlerTracer>(config.MessageHandlers[config.MessageHandlers.Count - 1]);
Assert.IsAssignableFrom<MessageHandlerTracer>(config.MessageHandlers[config.MessageHandlers.Count - 2]);
}
[Fact]
public void Initialize_Does_Not_Alter_MediaTypeFormatters_When_No_TraceWriter_Present()
{
// Arrange
HttpConfiguration config = new HttpConfiguration();
// Act
new TraceManager().Initialize(config);
// Assert
foreach (var formatter in config.Formatters)
{
Assert.False(typeof(IFormatterTracer).IsAssignableFrom(formatter.GetType()));
}
}
[Fact]
public void Initialize_Alters_MediaTypeFormatters_WhenTraceWriter_Present()
{
// Arrange
HttpConfiguration config = new HttpConfiguration();
Mock<ITraceWriter> traceWriter = new Mock<ITraceWriter>() { CallBase = true };
config.Services.Replace(typeof(ITraceWriter), traceWriter.Object);
// Act
new TraceManager().Initialize(config);
// Assert
foreach (var formatter in config.Formatters)
{
Assert.IsAssignableFrom<IFormatterTracer>(formatter);
}
}
}
}
|