forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatchHttpRequestContextTests.cs
More file actions
343 lines (281 loc) · 11.2 KB
/
Copy pathBatchHttpRequestContextTests.cs
File metadata and controls
343 lines (281 loc) · 11.2 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// 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.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using System.Web.Http.Controllers;
using System.Web.Http.Routing;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.Batch
{
public class BatchHttpRequestContextTests
{
[Fact]
public void Constructor_Throws_WhenBatchContextIsNull()
{
// Arrange
HttpRequestContext batchContext = null;
// Act & Assert
Assert.ThrowsArgumentNull(() => CreateProductUnderTest(batchContext), "batchContext");
}
[Fact]
public void BatchContext_ReturnsProvidedInstance()
{
// Arrange
HttpRequestContext expectedBatchContext = CreateDummyContext();
BatchHttpRequestContext context = CreateProductUnderTest(expectedBatchContext);
// Act
HttpRequestContext batchContext = context.BatchContext;
// Assert
Assert.Same(expectedBatchContext, batchContext);
}
[Fact]
public void ClientCertificateGet_ReturnsBatchContextClientCertificate()
{
// Arrange
X509Certificate2 expectedCertificate = CreateCertificate();
HttpRequestContext batchContext = CreateContext();
batchContext.ClientCertificate = expectedCertificate;
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
X509Certificate2 certificate = context.ClientCertificate;
// Assert
Assert.Same(expectedCertificate, certificate);
}
[Fact]
public void ClientCertificateSet_UpdatesBatchContextClientCertificate()
{
// Arrange
X509Certificate2 expectedCertificate = CreateCertificate();
HttpRequestContext batchContext = CreateContext();
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
context.ClientCertificate = expectedCertificate;
// Assert
X509Certificate2 certificate = batchContext.ClientCertificate;
Assert.Same(expectedCertificate, certificate);
}
[Fact]
public void ConfigurationGet_DoesNotReturnBatchContextConfiguration()
{
// Arrange
using (HttpConfiguration unexpectedConfiguration = CreateConfiguration())
{
HttpRequestContext batchContext = CreateContext();
batchContext.Configuration = unexpectedConfiguration;
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
HttpConfiguration configuration = context.Configuration;
// Assert
Assert.NotSame(unexpectedConfiguration, configuration);
}
}
[Fact]
public void ConfigurationSet_UpdatesConfiguration()
{
// Arrange
using (HttpConfiguration expectedConfiguration = CreateConfiguration())
{
HttpRequestContext batchContext = CreateContext();
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
context.Configuration = expectedConfiguration;
// Assert
HttpConfiguration configuration = context.Configuration;
Assert.Same(expectedConfiguration, configuration);
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IncludeErrorDetailGet_ReturnsBatchContextIncludeErrorDetail(bool expectedIncludeErrorDetail)
{
// Arrange
HttpRequestContext batchContext = CreateContext();
batchContext.IncludeErrorDetail = expectedIncludeErrorDetail;
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
bool includeErrorDetail = context.IncludeErrorDetail;
// Assert
Assert.Equal(expectedIncludeErrorDetail, includeErrorDetail);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IncludeErrorDetailSet_UpdatesBatchContextIncludeErrorDetail(bool expectedIncludeErrorDetail)
{
// Arrange
HttpRequestContext batchContext = CreateContext();
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
context.IncludeErrorDetail = expectedIncludeErrorDetail;
// Assert
bool includeErrorDetail = batchContext.IncludeErrorDetail;
Assert.Equal(expectedIncludeErrorDetail, includeErrorDetail);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IsLocalGet_ReturnsBatchContextIsLocal(bool expectedIsLocal)
{
// Arrange
HttpRequestContext batchContext = CreateContext();
batchContext.IsLocal = expectedIsLocal;
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
bool isLocal = context.IsLocal;
// Assert
Assert.Equal(expectedIsLocal, isLocal);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IsLocalSet_UpdatesBatchContextIsLocal(bool expectedIsLocal)
{
// Arrange
HttpRequestContext batchContext = CreateContext();
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
context.IsLocal = expectedIsLocal;
// Assert
bool isLocal = batchContext.IsLocal;
Assert.Equal(expectedIsLocal, isLocal);
}
[Fact]
public void PrincipalGet_ReturnsBatchContextPrincipal()
{
// Arrange
IPrincipal expectedPrincipal = CreateDummyPrincipal();
HttpRequestContext batchContext = CreateContext();
batchContext.Principal = expectedPrincipal;
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
IPrincipal principal = context.Principal;
// Assert
Assert.Same(expectedPrincipal, principal);
}
[Fact]
public void PrincipalSet_UpdatesBatchContextPrincipal()
{
// Arrange
IPrincipal expectedPrincipal = CreateDummyPrincipal();
HttpRequestContext batchContext = CreateContext();
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
context.Principal = expectedPrincipal;
// Assert
IPrincipal principal = batchContext.Principal;
Assert.Same(expectedPrincipal, principal);
}
[Fact]
public void RouteDataGet_DoesNotReturnBatchContextRouteData()
{
// Arrange
IHttpRouteData unexpectedRouteData = CreateDummyRouteData();
HttpRequestContext batchContext = CreateContext();
batchContext.RouteData = unexpectedRouteData;
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
IHttpRouteData routeData = context.RouteData;
// Assert
Assert.NotSame(unexpectedRouteData, routeData);
}
[Fact]
public void RouteDataSet_UpdatesRouteData()
{
// Arrange
IHttpRouteData expectedRouteData = CreateDummyRouteData();
HttpRequestContext batchContext = CreateContext();
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
context.RouteData = expectedRouteData;
// Assert
IHttpRouteData routeData = context.RouteData;
Assert.Same(expectedRouteData, routeData);
}
[Fact]
public void UrlGet_DoesNotReturnBatchContextUrl()
{
// Arrange
UrlHelper unexpectedUrl = CreateDummyUrlHelper();
HttpRequestContext batchContext = CreateContext();
batchContext.Url = unexpectedUrl;
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
UrlHelper url = context.Url;
// Assert
Assert.NotSame(unexpectedUrl, url);
}
[Fact]
public void UrlSet_UpdatesUrl()
{
// Arrange
UrlHelper expectedUrl = CreateDummyUrlHelper();
HttpRequestContext batchContext = CreateContext();
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
context.Url = expectedUrl;
// Assert
UrlHelper url = context.Url;
Assert.Same(expectedUrl, url);
}
[Fact]
public void VirtualPathRootGet_ReturnsBatchContextVirtualPathRoot()
{
// Arrange
string expectedVirtualPathRoot = "foo";
HttpRequestContext batchContext = CreateContext();
batchContext.VirtualPathRoot = expectedVirtualPathRoot;
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
string virtualPathRoot = context.VirtualPathRoot;
// Assert
Assert.Same(expectedVirtualPathRoot, virtualPathRoot);
}
[Fact]
public void VirtualPathRootSet_UpdatesBatchContextVirtualPathRoot()
{
// Arrange
string expectedVirtualPathRoot = "foo";
HttpRequestContext batchContext = CreateContext();
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
context.VirtualPathRoot = expectedVirtualPathRoot;
// Assert
string virtualPathRoot = batchContext.VirtualPathRoot;
Assert.Same(expectedVirtualPathRoot, virtualPathRoot);
}
private static X509Certificate2 CreateCertificate()
{
return new X509Certificate2();
}
private static HttpConfiguration CreateConfiguration()
{
return new HttpConfiguration();
}
private static HttpRequestContext CreateContext()
{
return new HttpRequestContext();
}
private static HttpRequestContext CreateDummyContext()
{
return new Mock<HttpRequestContext>(MockBehavior.Strict).Object;
}
private static IPrincipal CreateDummyPrincipal()
{
return new Mock<IPrincipal>(MockBehavior.Strict).Object;
}
private static IHttpRouteData CreateDummyRouteData()
{
return new Mock<IHttpRouteData>(MockBehavior.Strict).Object;
}
private static UrlHelper CreateDummyUrlHelper()
{
return new Mock<UrlHelper>(MockBehavior.Strict).Object;
}
private static BatchHttpRequestContext CreateProductUnderTest(HttpRequestContext batchContext)
{
return new BatchHttpRequestContext(batchContext);
}
}
}