forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatterParameterBindingTracerTest.cs
More file actions
236 lines (203 loc) · 13.1 KB
/
Copy pathFormatterParameterBindingTracerTest.cs
File metadata and controls
236 lines (203 loc) · 13.1 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// 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.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Metadata;
using System.Web.Http.Metadata.Providers;
using System.Web.Http.ModelBinding;
using System.Web.Http.Services;
using System.Web.Http.Validation;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.Tracing.Tracers
{
public class FormatterParameterBindingTracerTest
{
[Fact]
public void ErrorMessage_Calls_Inner()
{
// Arrange
Mock<HttpParameterDescriptor> mockParamDescriptor = new Mock<HttpParameterDescriptor>() { CallBase = true };
mockParamDescriptor.Setup(d => d.ParameterName).Returns("paramName");
mockParamDescriptor.Setup(d => d.ParameterType).Returns(typeof(string));
Mock<FormatterParameterBinding> mockBinding = new Mock<FormatterParameterBinding>(mockParamDescriptor.Object, new MediaTypeFormatterCollection(), null);
mockBinding.Setup(b => b.ErrorMessage).Returns("errorMessage").Verifiable();
FormatterParameterBindingTracer tracer = new FormatterParameterBindingTracer(mockBinding.Object, new TestTraceWriter());
// Act & Assert
Assert.Equal("errorMessage", tracer.ErrorMessage);
mockBinding.Verify();
}
[Fact]
public void WillReadBody_Calls_Inner()
{
// Arrange
Mock<HttpParameterDescriptor> mockParamDescriptor = new Mock<HttpParameterDescriptor>() { CallBase = true };
mockParamDescriptor.Setup(d => d.ParameterName).Returns("paramName");
mockParamDescriptor.Setup(d => d.ParameterType).Returns(typeof(string));
Mock<FormatterParameterBinding> mockBinding = new Mock<FormatterParameterBinding>(mockParamDescriptor.Object, new MediaTypeFormatterCollection(), null);
mockBinding.Setup(b => b.WillReadBody).Returns(true).Verifiable();
FormatterParameterBindingTracer tracer = new FormatterParameterBindingTracer(mockBinding.Object, new TestTraceWriter());
// Act & Assert
Assert.True(tracer.WillReadBody);
mockBinding.Verify();
}
[Fact]
public void Descriptor_Uses_Inners()
{
// Arrange
Mock<HttpParameterDescriptor> mockParamDescriptor = new Mock<HttpParameterDescriptor>() { CallBase = true };
mockParamDescriptor.Setup(d => d.ParameterName).Returns("paramName");
mockParamDescriptor.Setup(d => d.ParameterType).Returns(typeof(string));
Mock<FormatterParameterBinding> mockBinding = new Mock<FormatterParameterBinding>(mockParamDescriptor.Object, new MediaTypeFormatterCollection(), null) { CallBase = true };
FormatterParameterBindingTracer tracer = new FormatterParameterBindingTracer(mockBinding.Object, new TestTraceWriter());
// Act & Assert
Assert.Same(mockBinding.Object.Descriptor, tracer.Descriptor);
}
[Fact]
public void Formatters_Uses_Inners()
{
// Arrange
Mock<HttpParameterDescriptor> mockParamDescriptor = new Mock<HttpParameterDescriptor>() { CallBase = true };
mockParamDescriptor.Setup(d => d.ParameterName).Returns("paramName");
mockParamDescriptor.Setup(d => d.ParameterType).Returns(typeof(string));
Mock<FormatterParameterBinding> mockBinding = new Mock<FormatterParameterBinding>(mockParamDescriptor.Object, new MediaTypeFormatterCollection(), null) { CallBase = true };
FormatterParameterBindingTracer tracer = new FormatterParameterBindingTracer(mockBinding.Object, new TestTraceWriter());
// Act & Assert
Assert.Equal(mockBinding.Object.Formatters, tracer.Formatters);
}
[Fact]
public void BodyModelValidator_Uses_Inners()
{
// Arrange
Mock<HttpParameterDescriptor> mockParamDescriptor = new Mock<HttpParameterDescriptor>() { CallBase = true };
mockParamDescriptor.Setup(d => d.ParameterName).Returns("paramName");
mockParamDescriptor.Setup(d => d.ParameterType).Returns(typeof(string));
Mock<IBodyModelValidator> mockValidator = new Mock<IBodyModelValidator>();
Mock<FormatterParameterBinding> mockBinding = new Mock<FormatterParameterBinding>(mockParamDescriptor.Object, new MediaTypeFormatterCollection(), mockValidator.Object) { CallBase = true };
FormatterParameterBindingTracer tracer = new FormatterParameterBindingTracer(mockBinding.Object, new TestTraceWriter());
// Act & Assert
Assert.Equal(mockBinding.Object.BodyModelValidator, tracer.BodyModelValidator);
}
/// <summary>
/// This test verifies that our <see cref="FormatterParameterBindingTracer"/>
/// intercepts the async bind request and redirects it to use tracing formatters
/// correlated to the request.
/// </summary>
[Fact]
public async Task ExecuteBindingAsync_Traces_And_Invokes_Inner_ReadAsync()
{
// Arrange
Mock<HttpParameterDescriptor> mockParamDescriptor = new Mock<HttpParameterDescriptor>() { CallBase = true };
mockParamDescriptor.Setup(d => d.ParameterName).Returns("paramName");
mockParamDescriptor.Setup(d => d.ParameterType).Returns(typeof(string));
FormatterParameterBinding binding = new FormatterParameterBinding(mockParamDescriptor.Object, new MediaTypeFormatterCollection(), null);
TestTraceWriter traceWriter = new TestTraceWriter();
FormatterParameterBindingTracer tracer = new FormatterParameterBindingTracer(binding, traceWriter);
HttpActionContext actionContext = ContextUtil.CreateActionContext();
actionContext.Request.Content = new StringContent("true");
actionContext.Request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
ModelMetadataProvider metadataProvider = new EmptyModelMetadataProvider();
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "ExecuteBindingAsync" },
new TraceRecord(actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info) { Kind = TraceKind.End, Operation = "ExecuteBindingAsync" }
};
// Act
await tracer.ExecuteBindingAsync(metadataProvider, actionContext, CancellationToken.None);
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
Assert.Equal("true", actionContext.ActionArguments["paramName"]);
}
[Fact]
public void ExecuteBindingAsync_Traces_And_Throws_When_Inner_Throws()
{
// Arrange
Mock<HttpParameterDescriptor> mockParamDescriptor = new Mock<HttpParameterDescriptor>() { CallBase = true };
mockParamDescriptor.Setup(d => d.ParameterName).Returns("paramName");
mockParamDescriptor.Setup(d => d.ParameterType).Returns(typeof(string));
Mock<FormatterParameterBinding> mockBinding = new Mock<FormatterParameterBinding>(mockParamDescriptor.Object, new MediaTypeFormatterCollection(), null) { CallBase = true };
InvalidOperationException exception = new InvalidOperationException("test");
mockBinding.Setup(
b =>
b.ExecuteBindingAsync(It.IsAny<ModelMetadataProvider>(), It.IsAny<HttpActionContext>(),
It.IsAny<CancellationToken>())).Throws(exception);
TestTraceWriter traceWriter = new TestTraceWriter();
FormatterParameterBindingTracer tracer = new FormatterParameterBindingTracer(mockBinding.Object, traceWriter);
HttpActionContext actionContext = ContextUtil.CreateActionContext();
ModelMetadataProvider metadataProvider = new EmptyModelMetadataProvider();
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "ExecuteBindingAsync" },
new TraceRecord(actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Error) { Kind = TraceKind.End, Operation = "ExecuteBindingAsync" }
};
// Act & Assert
Exception thrown = Assert.Throws<InvalidOperationException>(() => tracer.ExecuteBindingAsync(metadataProvider, actionContext, CancellationToken.None));
// Assert
Assert.Same(exception, thrown);
Assert.Same(exception, traceWriter.Traces[1].Exception);
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
}
[Fact]
public async Task ExecuteBindingAsync_Traces_And_Faults_When_Inner_Faults()
{
// Arrange
Mock<HttpParameterDescriptor> mockParamDescriptor = new Mock<HttpParameterDescriptor>() { CallBase = true };
mockParamDescriptor.Setup(d => d.ParameterName).Returns("paramName");
mockParamDescriptor.Setup(d => d.ParameterType).Returns(typeof(string));
Mock<FormatterParameterBinding> mockBinding = new Mock<FormatterParameterBinding>(mockParamDescriptor.Object, new MediaTypeFormatterCollection(), null) { CallBase = true };
InvalidOperationException exception = new InvalidOperationException("test");
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
tcs.TrySetException(exception);
mockBinding.Setup(
b =>
b.ExecuteBindingAsync(It.IsAny<ModelMetadataProvider>(), It.IsAny<HttpActionContext>(),
It.IsAny<CancellationToken>())).Returns(tcs.Task);
TestTraceWriter traceWriter = new TestTraceWriter();
FormatterParameterBindingTracer tracer = new FormatterParameterBindingTracer(mockBinding.Object, traceWriter);
HttpActionContext actionContext = ContextUtil.CreateActionContext();
ModelMetadataProvider metadataProvider = new EmptyModelMetadataProvider();
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "ExecuteBindingAsync" },
new TraceRecord(actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Error) { Kind = TraceKind.End, Operation = "ExecuteBindingAsync" }
};
// Act & Assert
Task task = tracer.ExecuteBindingAsync(metadataProvider, actionContext, CancellationToken.None);
// Assert
Exception thrown = await Assert.ThrowsAsync<InvalidOperationException>(() => task);
Assert.Same(exception, thrown);
Assert.Same(exception, traceWriter.Traces[1].Exception);
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
}
[Fact]
public void Inner_Property_On_FormatterParameterBindingTracer_Returns_FormatterParameterBinding()
{
// Arrange
HttpParameterDescriptor httpParameterDescriptor = new Mock<HttpParameterDescriptor>().Object;
MediaTypeFormatterCollection mediaTypeFormatterCollection = new MediaTypeFormatterCollection();
FormatterParameterBinding expectedInner = new FormatterParameterBinding(httpParameterDescriptor, mediaTypeFormatterCollection, null);
FormatterParameterBindingTracer productUnderTest = new FormatterParameterBindingTracer(expectedInner, new TestTraceWriter());
// Act
FormatterParameterBinding actualInner = productUnderTest.Inner;
// Assert
Assert.Same(expectedInner, actualInner);
}
[Fact]
public void Decorator_GetInner_On_FormatterParameterBindingTracer_Returns_FormatterParameterBinding()
{
// Arrange
HttpParameterDescriptor httpParameterDescriptor = new Mock<HttpParameterDescriptor>().Object;
MediaTypeFormatterCollection mediaTypeFormatterCollection = new MediaTypeFormatterCollection();
FormatterParameterBinding expectedInner = new FormatterParameterBinding(httpParameterDescriptor, mediaTypeFormatterCollection, null);
FormatterParameterBindingTracer productUnderTest = new FormatterParameterBindingTracer(expectedInner, new TestTraceWriter());
// Act
FormatterParameterBinding actualInner = Decorator.GetInner(productUnderTest as FormatterParameterBinding);
// Assert
Assert.Same(expectedInner, actualInner);
}
}
}