forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpPageConfigurationExtensionsTest.cs
More file actions
423 lines (392 loc) · 21.2 KB
/
Copy pathHelpPageConfigurationExtensionsTest.cs
File metadata and controls
423 lines (392 loc) · 21.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// 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;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Web.Http;
using System.Web.Http.Description;
using System.Web.Http.ModelBinding;
using Microsoft.TestCommon;
using Moq;
using ROOT_PROJECT_NAMESPACE.Areas.HelpPage;
using ROOT_PROJECT_NAMESPACE.Areas.HelpPage.ModelDescriptions;
using ROOT_PROJECT_NAMESPACE.Areas.HelpPage.Models;
namespace WebApiHelpPageWebHost.UnitTest
{
public class HelpPageConfigurationExtensionsTest
{
[Theory]
[InlineData("Get-Values")]
[InlineData("get-values")]
[InlineData("Get-Values_Name")]
[InlineData("get-values_NAME")]
[InlineData("Get-Values-id")]
[InlineData("Get-Values-ID")]
[InlineData("Post-Values")]
[InlineData("POST-VALUES")]
[InlineData("Put-Values-id")]
[InlineData("Put-VALUES-ID")]
[InlineData("Put-Values")]
[InlineData("Put-VALUES")]
[InlineData("Delete-Values-id")]
[InlineData("Delete-VALUES-id")]
[InlineData("Patch-Values")]
[InlineData("Patch-VALUES")]
[InlineData("Options-Values")]
[InlineData("OpTions-VALUES")]
[InlineData("Head-Values-id")]
[InlineData("HEAD-VALUES-id")]
public void GetHelpPageApiModel_ReturnsTheModel_WhenIdIsValid(string apiId)
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional });
HelpPageApiModel model = config.GetHelpPageApiModel(apiId);
Assert.NotNull(model);
Assert.Same(model, config.GetHelpPageApiModel(apiId));
Assert.Equal(apiId, model.ApiDescription.GetFriendlyId(), StringComparer.OrdinalIgnoreCase);
}
[Fact]
public void GetHelpPageApiModel_TypeConverterModel_GeneratesUriParameter()
{
// If class Point with properties X and Y is defined with a TypeConverter,
// the UriParameters should be a single parameter name "point" instead of two member properties X and Y,
// because X and Y do not appear in the Relative Path and it is the user that should specify how the query
// string is parsed.
// Arrange
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional });
// Act
HelpPageApiModel model = config.GetHelpPageApiModel("GET-Values_point");
// Assert
Assert.NotNull(model);
string expectedParameter = Assert.Single(model.ApiDescription.ParameterDescriptions).Name;
ParameterDescription parameterDescription = Assert.Single(model.UriParameters);
Assert.Equal(expectedParameter, parameterDescription.Name);
}
[Theory]
[InlineData("Get-Values-Unused")]
[InlineData("get-values-unused")]
[InlineData("Get-Values-Unused_Name")]
[InlineData("get-values-unused_NAME")]
[InlineData("Get-Values-Unused-id")]
[InlineData("Get-Values-unused-ID")]
[InlineData("Post-Values-Unused")]
[InlineData("POST-VALUES-UNUSED")]
[InlineData("Put-Values-Unused-id")]
[InlineData("Put-VALUES-UNUSED-ID")]
[InlineData("Put-Values-Unused")]
[InlineData("Put-VALUES-UNUSED")]
[InlineData("Delete-Values-Unused-id")]
[InlineData("Delete-VALUES-UNUSED-id")]
[InlineData("Patch-Values-Unused")]
[InlineData("Patch-VALUES-UNUSED")]
[InlineData("Options-Values-Unused")]
[InlineData("OpTions-VALUES-UNUSED")]
[InlineData("Head-Values-Unused-id")]
[InlineData("HEAD-VALUES-UNUSED-id")]
public void GetHelpPageApiModel_ReturnsTheModel_WhenIdIsValid_UnusedParameters(string apiId)
{
// Arrange
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute("Default", "{controller}/{unused}/{id}", new { id = RouteParameter.Optional });
// Act
HelpPageApiModel model = config.GetHelpPageApiModel(apiId);
// Assert
Assert.NotNull(model);
Assert.Same(model, config.GetHelpPageApiModel(apiId));
Assert.Equal(apiId, model.ApiDescription.GetFriendlyId(), StringComparer.OrdinalIgnoreCase);
}
[Theory]
[InlineData("foo")]
[InlineData("bar")]
[InlineData("@alpha")]
[InlineData("GetValues/{id}/{name}")]
public void GetHelpPageApiModel_ReturnsNull_WhenIdIsInvalid(string apiId)
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional });
HelpPageApiModel model = config.GetHelpPageApiModel(apiId);
Assert.Null(model);
}
[Theory]
[InlineData("Get-Values")]
[InlineData("get-values")]
[InlineData("Get-Values-id")]
[InlineData("Get-Values-ID")]
public void GetHelpPageApiModel_ReturnsNull_IfNoRoute(string apiId)
{
HttpConfiguration config = new HttpConfiguration();
HelpPageApiModel model = config.GetHelpPageApiModel(apiId);
Assert.Null(model);
}
[Fact]
public void GetHelpPageApiModel_HandlesException_ThrownDuringSampleGeneration()
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional });
Mock<HelpPageSampleGenerator> faultyGenerator = new Mock<HelpPageSampleGenerator>();
faultyGenerator.Setup(g => g.GetSample(It.IsAny<ApiDescription>(), It.IsAny<SampleDirection>())).Returns(() => { throw new InvalidOperationException("This is a faulty sample generator."); });
config.SetHelpPageSampleGenerator(faultyGenerator.Object);
HelpPageApiModel model = config.GetHelpPageApiModel("Get-Values");
Assert.NotNull(model);
Assert.NotEmpty(model.ErrorMessages);
Assert.Equal("An exception has occurred while generating the sample. Exception message: This is a faulty sample generator.", model.ErrorMessages[0]);
}
[Fact]
public void GetHelpPageApiModel_HandlesException_ThrownDuringSampleObjectSerialization()
{
HttpConfiguration config = new HttpConfiguration();
config.Formatters.Clear();
config.Formatters.Add(new JQueryMvcFormUrlEncodedFormatter());
config.SetSampleObjects(new Dictionary<Type, object> { { typeof(string), "sample" } });
config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional });
HelpPageApiModel model = config.GetHelpPageApiModel("Post-Values");
Assert.NotNull(model);
Assert.NotEmpty(model.ErrorMessages);
Assert.Equal("Failed to generate the sample for media type 'application/x-www-form-urlencoded'. Cannot use formatter 'JQueryMvcFormUrlEncodedFormatter' to write type 'String'.", model.ErrorMessages[0]);
}
[Fact]
public void GetHelpPageApiModel_UnwrapsAggregateException()
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional });
Mock<HelpPageSampleGenerator> faultyGenerator = new Mock<HelpPageSampleGenerator>();
faultyGenerator.Setup(g => g.GetSample(It.IsAny<ApiDescription>(), It.IsAny<SampleDirection>())).Returns(() => { throw new AggregateException(new InvalidOperationException("Sample generator failed.")); });
config.SetHelpPageSampleGenerator(faultyGenerator.Object);
HelpPageApiModel model = config.GetHelpPageApiModel("Get-Values");
Assert.NotNull(model);
Assert.NotEmpty(model.ErrorMessages);
Assert.Equal("An exception has occurred while generating the sample. Exception message: Sample generator failed.", model.ErrorMessages[0]);
}
[Theory]
[InlineData("Post-Users", true)]
[InlineData("Post-Values", true)]
[InlineData("Put-Values", true)]
[InlineData("Get-Values", false)]
[InlineData("Get-Users", false)]
[InlineData("Get-Values-id", false)]
[InlineData("Head-Values-id", false)]
public void GetHelpPageApiModel_ReturnsExpectedRequestModelDescription(string apiId, bool hasRequestModelDescription)
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional });
HelpPageApiModel model = config.GetHelpPageApiModel(apiId);
Assert.NotNull(model);
if (hasRequestModelDescription)
{
Assert.NotNull(model.RequestModelDescription);
}
else
{
Assert.Null(model.RequestModelDescription);
}
}
[Fact]
public void SetDocumentationProvider()
{
HttpConfiguration config = new HttpConfiguration();
Mock<IDocumentationProvider> docProviderMock = new Mock<IDocumentationProvider>();
IDocumentationProvider docProvider = docProviderMock.Object;
config.SetDocumentationProvider(docProvider);
Assert.Same(docProvider, config.Services.GetDocumentationProvider());
}
[Fact]
public void SetSampleObjects()
{
HttpConfiguration config = new HttpConfiguration();
Dictionary<Type, object> sampleObjects = new Dictionary<Type, object>
{
{typeof(int), 21},
{typeof(string), "sample"}
};
config.SetSampleObjects(sampleObjects);
object sampleGeneratorObj;
config.Properties.TryGetValue(typeof(HelpPageSampleGenerator), out sampleGeneratorObj);
HelpPageSampleGenerator sampleGenerator = Assert.IsType<HelpPageSampleGenerator>(sampleGeneratorObj);
Assert.Same(sampleObjects, sampleGenerator.SampleObjects);
}
[Fact]
public void SetSampleRequest()
{
HttpConfiguration config = new HttpConfiguration();
TextSample sample = new TextSample("test");
config.SetSampleRequest(sample, new MediaTypeHeaderValue("application/xml"), "values", "get");
object sampleGeneratorObj;
config.Properties.TryGetValue(typeof(HelpPageSampleGenerator), out sampleGeneratorObj);
HelpPageSampleGenerator sampleGenerator = Assert.IsType<HelpPageSampleGenerator>(sampleGeneratorObj);
Assert.NotEmpty(sampleGenerator.ActionSamples);
var actionSample = sampleGenerator.ActionSamples.First();
Assert.Equal("values", actionSample.Key.ControllerName);
Assert.Equal("get", actionSample.Key.ActionName);
Assert.Equal(new MediaTypeHeaderValue("application/xml"), actionSample.Key.MediaType);
Assert.Equal(SampleDirection.Request, actionSample.Key.SampleDirection);
Assert.NotEmpty(actionSample.Key.ParameterNames);
Assert.Equal("*", actionSample.Key.ParameterNames.First());
Assert.Same(sample, actionSample.Value);
}
[Fact]
public void SetSampleRequest_WithParameters()
{
HttpConfiguration config = new HttpConfiguration();
TextSample sample = new TextSample("test");
config.SetSampleRequest(sample, new MediaTypeHeaderValue("application/json"), "values", "post", "id", "name");
object sampleGeneratorObj;
config.Properties.TryGetValue(typeof(HelpPageSampleGenerator), out sampleGeneratorObj);
HelpPageSampleGenerator sampleGenerator = Assert.IsType<HelpPageSampleGenerator>(sampleGeneratorObj);
Assert.NotEmpty(sampleGenerator.ActionSamples);
var actionSample = sampleGenerator.ActionSamples.First();
Assert.Equal("values", actionSample.Key.ControllerName);
Assert.Equal("post", actionSample.Key.ActionName);
Assert.Equal(new MediaTypeHeaderValue("application/json"), actionSample.Key.MediaType);
Assert.Equal(SampleDirection.Request, actionSample.Key.SampleDirection);
Assert.Equal(2, actionSample.Key.ParameterNames.Count);
Assert.True(actionSample.Key.ParameterNames.SetEquals(new[] { "id", "name" }));
Assert.Same(sample, actionSample.Value);
}
[Fact]
public void SetSampleResponse()
{
HttpConfiguration config = new HttpConfiguration();
TextSample sample = new TextSample("test");
config.SetSampleResponse(sample, new MediaTypeHeaderValue("application/xml"), "values", "get");
object sampleGeneratorObj;
config.Properties.TryGetValue(typeof(HelpPageSampleGenerator), out sampleGeneratorObj);
HelpPageSampleGenerator sampleGenerator = Assert.IsType<HelpPageSampleGenerator>(sampleGeneratorObj);
Assert.NotEmpty(sampleGenerator.ActionSamples);
var actionSample = sampleGenerator.ActionSamples.First();
Assert.Equal("values", actionSample.Key.ControllerName);
Assert.Equal("get", actionSample.Key.ActionName);
Assert.Equal(new MediaTypeHeaderValue("application/xml"), actionSample.Key.MediaType);
Assert.Equal(SampleDirection.Response, actionSample.Key.SampleDirection);
Assert.NotEmpty(actionSample.Key.ParameterNames);
Assert.Equal("*", actionSample.Key.ParameterNames.First());
Assert.Same(sample, actionSample.Value);
}
[Fact]
public void SetSampleResponse_WithParameters()
{
HttpConfiguration config = new HttpConfiguration();
TextSample sample = new TextSample("test");
config.SetSampleResponse(sample, new MediaTypeHeaderValue("application/json"), "values", "post", "id", "name");
object sampleGeneratorObj;
config.Properties.TryGetValue(typeof(HelpPageSampleGenerator), out sampleGeneratorObj);
HelpPageSampleGenerator sampleGenerator = Assert.IsType<HelpPageSampleGenerator>(sampleGeneratorObj);
Assert.NotEmpty(sampleGenerator.ActionSamples);
var actionSample = sampleGenerator.ActionSamples.First();
Assert.Equal("values", actionSample.Key.ControllerName);
Assert.Equal("post", actionSample.Key.ActionName);
Assert.Equal(new MediaTypeHeaderValue("application/json"), actionSample.Key.MediaType);
Assert.Equal(SampleDirection.Response, actionSample.Key.SampleDirection);
Assert.Equal(2, actionSample.Key.ParameterNames.Count);
Assert.True(actionSample.Key.ParameterNames.SetEquals(new[] { "id", "name" }));
Assert.Same(sample, actionSample.Value);
}
[Fact]
public void SetSampleForType()
{
HttpConfiguration config = new HttpConfiguration();
ImageSample sample = new ImageSample("http://localhost/test.png");
config.SetSampleForType(sample, new MediaTypeHeaderValue("image/png"), typeof(string));
object sampleGeneratorObj;
config.Properties.TryGetValue(typeof(HelpPageSampleGenerator), out sampleGeneratorObj);
HelpPageSampleGenerator sampleGenerator = Assert.IsType<HelpPageSampleGenerator>(sampleGeneratorObj);
Assert.NotEmpty(sampleGenerator.ActionSamples);
var actionSample = sampleGenerator.ActionSamples.First();
Assert.Equal(String.Empty, actionSample.Key.ControllerName);
Assert.Equal(String.Empty, actionSample.Key.ActionName);
Assert.Equal(new MediaTypeHeaderValue("image/png"), actionSample.Key.MediaType);
Assert.Null(actionSample.Key.SampleDirection);
Assert.Empty(actionSample.Key.ParameterNames);
Assert.Same(sample, actionSample.Value);
}
[Fact]
public void SetActualRequestType()
{
HttpConfiguration config = new HttpConfiguration();
config.SetActualRequestType(typeof(string), "c", "a");
object sampleGeneratorObj;
config.Properties.TryGetValue(typeof(HelpPageSampleGenerator), out sampleGeneratorObj);
HelpPageSampleGenerator sampleGenerator = Assert.IsType<HelpPageSampleGenerator>(sampleGeneratorObj);
Assert.NotEmpty(sampleGenerator.ActualHttpMessageTypes);
var actualType = sampleGenerator.ActualHttpMessageTypes.First();
Assert.Equal("c", actualType.Key.ControllerName);
Assert.Equal("a", actualType.Key.ActionName);
Assert.Null(actualType.Key.MediaType);
Assert.Equal(SampleDirection.Request, actualType.Key.SampleDirection);
Assert.NotEmpty(actualType.Key.ParameterNames);
Assert.Equal("*", actualType.Key.ParameterNames.First());
Assert.Equal(typeof(string), actualType.Value);
}
[Fact]
public void SetActualRequestType_WithParameters()
{
HttpConfiguration config = new HttpConfiguration();
config.SetActualRequestType(typeof(string), "c", "a", "id");
object sampleGeneratorObj;
config.Properties.TryGetValue(typeof(HelpPageSampleGenerator), out sampleGeneratorObj);
HelpPageSampleGenerator sampleGenerator = Assert.IsType<HelpPageSampleGenerator>(sampleGeneratorObj);
Assert.NotEmpty(sampleGenerator.ActualHttpMessageTypes);
var actualType = sampleGenerator.ActualHttpMessageTypes.First();
Assert.Equal("c", actualType.Key.ControllerName);
Assert.Equal("a", actualType.Key.ActionName);
Assert.Null(actualType.Key.MediaType);
Assert.Equal(SampleDirection.Request, actualType.Key.SampleDirection);
Assert.NotEmpty(actualType.Key.ParameterNames);
Assert.Equal("id", actualType.Key.ParameterNames.First());
Assert.Equal(typeof(string), actualType.Value);
}
[Fact]
public void SetActualResponseType()
{
HttpConfiguration config = new HttpConfiguration();
config.SetActualResponseType(typeof(int), "c", "a");
object sampleGeneratorObj;
config.Properties.TryGetValue(typeof(HelpPageSampleGenerator), out sampleGeneratorObj);
HelpPageSampleGenerator sampleGenerator = Assert.IsType<HelpPageSampleGenerator>(sampleGeneratorObj);
Assert.NotEmpty(sampleGenerator.ActualHttpMessageTypes);
var actualType = sampleGenerator.ActualHttpMessageTypes.First();
Assert.Equal("c", actualType.Key.ControllerName);
Assert.Equal("a", actualType.Key.ActionName);
Assert.Null(actualType.Key.MediaType);
Assert.Equal(SampleDirection.Response, actualType.Key.SampleDirection);
Assert.NotEmpty(actualType.Key.ParameterNames);
Assert.Equal("*", actualType.Key.ParameterNames.First());
Assert.Equal(typeof(int), actualType.Value);
}
[Fact]
public void SetActualResponseType_WithParameters()
{
HttpConfiguration config = new HttpConfiguration();
config.SetActualResponseType(typeof(int), "c", "a", "id");
object sampleGeneratorObj;
config.Properties.TryGetValue(typeof(HelpPageSampleGenerator), out sampleGeneratorObj);
HelpPageSampleGenerator sampleGenerator = Assert.IsType<HelpPageSampleGenerator>(sampleGeneratorObj);
Assert.NotEmpty(sampleGenerator.ActualHttpMessageTypes);
var actualType = sampleGenerator.ActualHttpMessageTypes.First();
Assert.Equal("c", actualType.Key.ControllerName);
Assert.Equal("a", actualType.Key.ActionName);
Assert.Null(actualType.Key.MediaType);
Assert.Equal(SampleDirection.Response, actualType.Key.SampleDirection);
Assert.NotEmpty(actualType.Key.ParameterNames);
Assert.Equal("id", actualType.Key.ParameterNames.First());
Assert.Equal(typeof(int), actualType.Value);
}
[Fact]
public void GetHelpPageSampleGenerator_ReturnsDefaultValue()
{
HttpConfiguration config = new HttpConfiguration();
HelpPageSampleGenerator helpPageSampleGenerator = config.GetHelpPageSampleGenerator();
Assert.NotNull(helpPageSampleGenerator);
Assert.Same(helpPageSampleGenerator, config.GetHelpPageSampleGenerator());
}
[Fact]
public void SetHelpPageSampleGenerator_ChangesTheDefault()
{
HttpConfiguration config = new HttpConfiguration();
Mock<HelpPageSampleGenerator> helpPageSampleGenerator = new Mock<HelpPageSampleGenerator>();
config.SetHelpPageSampleGenerator(helpPageSampleGenerator.Object);
Assert.Same(helpPageSampleGenerator.Object, config.GetHelpPageSampleGenerator());
}
}
}