forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpActionBindingTracerTest.cs
More file actions
153 lines (125 loc) · 6.89 KB
/
Copy pathHttpActionBindingTracerTest.cs
File metadata and controls
153 lines (125 loc) · 6.89 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.ObjectModel;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Services;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.Tracing.Tracers
{
public class HttpActionBindingTracerTest
{
private Mock<HttpActionDescriptor> _mockActionDescriptor;
private Mock<HttpParameterDescriptor> _mockParameterDescriptor;
private Mock<HttpParameterBinding> _mockParameterBinding;
private HttpActionBinding _actionBinding;
private HttpActionContext _actionContext;
private HttpControllerContext _controllerContext;
private HttpControllerDescriptor _controllerDescriptor;
public HttpActionBindingTracerTest()
{
_mockActionDescriptor = new Mock<HttpActionDescriptor>() { CallBase = true };
_mockActionDescriptor.Setup(a => a.ActionName).Returns("test");
_mockActionDescriptor.Setup(a => a.GetParameters()).Returns(new Collection<HttpParameterDescriptor>(new HttpParameterDescriptor[0]));
_mockParameterDescriptor = new Mock<HttpParameterDescriptor>() { CallBase = true };
_mockParameterBinding = new Mock<HttpParameterBinding>(_mockParameterDescriptor.Object) { CallBase = true };
_actionBinding = new HttpActionBinding(_mockActionDescriptor.Object, new HttpParameterBinding[] { _mockParameterBinding.Object });
_controllerDescriptor = new HttpControllerDescriptor(new HttpConfiguration(), "controller", typeof(ApiController));
_controllerContext = ContextUtil.CreateControllerContext(request: new HttpRequestMessage());
_controllerContext.ControllerDescriptor = _controllerDescriptor;
_actionContext = ContextUtil.CreateActionContext(_controllerContext, actionDescriptor: _mockActionDescriptor.Object);
}
[Fact]
public void ActionDescriptor_Uses_Inners()
{
// Arrange
HttpActionBinding binding = new Mock<HttpActionBinding>() { CallBase = true }.Object;
binding.ActionDescriptor = _mockActionDescriptor.Object;
HttpActionBindingTracer tracer = new HttpActionBindingTracer(binding, new TestTraceWriter());
// Assert
Assert.Same(binding.ActionDescriptor, tracer.ActionDescriptor);
}
[Fact]
public void ParameterBindings_Uses_Inners()
{
// Arrange
HttpActionBinding binding = new Mock<HttpActionBinding>() { CallBase = true }.Object;
HttpParameterBinding[] parameterBindings = new HttpParameterBinding[0];
binding.ParameterBindings = parameterBindings;
HttpActionBindingTracer tracer = new HttpActionBindingTracer(binding, new TestTraceWriter());
// Assert
Assert.Same(parameterBindings, tracer.ParameterBindings);
}
[Fact]
public async Task BindValuesAsync_Invokes_Inner_And_Traces()
{
// Arrange
bool wasInvoked = false;
Mock<HttpActionBinding> mockBinder = new Mock<HttpActionBinding>() { CallBase = true };
mockBinder.Setup(b => b.ExecuteBindingAsync(It.IsAny<HttpActionContext>(), It.IsAny<CancellationToken>()))
.Callback(() => wasInvoked = true).Returns(TaskHelpers.Completed());
TestTraceWriter traceWriter = new TestTraceWriter();
HttpActionBindingTracer tracer = new HttpActionBindingTracer(mockBinder.Object, traceWriter);
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info) { Kind = TraceKind.Begin },
new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info) { Kind = TraceKind.End }
};
// Act
await tracer.ExecuteBindingAsync(_actionContext, CancellationToken.None);
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
Assert.True(wasInvoked);
}
[Fact]
public async Task ExecuteBindingAsync_Faults_And_Traces_When_Inner_Faults()
{
// Arrange
InvalidOperationException exception = new InvalidOperationException();
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
tcs.TrySetException(exception);
Mock<HttpActionBinding> mockBinder = new Mock<HttpActionBinding>() { CallBase = true };
mockBinder.Setup(b => b.ExecuteBindingAsync(It.IsAny<HttpActionContext>(), It.IsAny<CancellationToken>()))
.Returns(tcs.Task);
TestTraceWriter traceWriter = new TestTraceWriter();
HttpActionBindingTracer tracer = new HttpActionBindingTracer(mockBinder.Object, traceWriter);
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info) { Kind = TraceKind.Begin },
new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Error) { Kind = TraceKind.End }
};
// Act
Task task = tracer.ExecuteBindingAsync(_actionContext, CancellationToken.None);
// Assert
Exception thrown = await Assert.ThrowsAsync<InvalidOperationException>(() => task);
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
Assert.Same(exception, thrown);
Assert.Same(exception, traceWriter.Traces[1].Exception);
}
[Fact]
public void Inner_Property_On_HttpActionBindingTracer_Returns_HttpActionBinding()
{
// Arrange
HttpActionBinding expectedInner = new HttpActionBinding();
HttpActionBindingTracer productUnderTest = new HttpActionBindingTracer(expectedInner, new TestTraceWriter());
// Act
HttpActionBinding actualInner = productUnderTest.Inner;
// Assert
Assert.Same(expectedInner, actualInner);
}
[Fact]
public void Decorator_GetInner_On_HttpActionBindingTracer_Returns_HttpActionBinding()
{
// Arrange
HttpActionBinding expectedInner = new HttpActionBinding();
HttpActionBindingTracer productUnderTest = new HttpActionBindingTracer(expectedInner, new TestTraceWriter());
// Act
HttpActionBinding actualInner = Decorator.GetInner(productUnderTest as HttpActionBinding);
// Assert
Assert.Same(expectedInner, actualInner);
}
}
}