forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParameterSourceTest.cs
More file actions
186 lines (165 loc) · 9.58 KB
/
Copy pathParameterSourceTest.cs
File metadata and controls
186 lines (165 loc) · 9.58 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
// 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.ComponentModel;
using System.Linq;
using System.Web.Http.Description;
using System.Web.Http.Dispatcher;
using Microsoft.TestCommon;
namespace System.Web.Http.ApiExplorer
{
public class ParameterSourceTest
{
[Fact]
public void FromUriParameterSource_ShowUpCorrectlyOnDescription()
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute("Default", "{controller}/{action}/{id}", new { id = RouteParameter.Optional });
DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, typeof(ParameterSourceController));
config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector);
IApiExplorer explorer = config.Services.GetApiExplorer();
ApiDescription description = explorer.ApiDescriptions.FirstOrDefault(desc => desc.ActionDescriptor.ActionName == "GetCompleTypeFromUri");
Assert.NotNull(description);
Assert.True(description.ParameterDescriptions.All(param => param.Source == ApiParameterSource.FromUri), "All parameters should come from URI.");
description = explorer.ApiDescriptions.FirstOrDefault(desc => desc.ActionDescriptor.ActionName == "GetCustomFromUriAttribute");
Assert.NotNull(description);
Assert.True(description.ParameterDescriptions.Any(param => param.Source == ApiParameterSource.FromUri && param.Name == "value"), "The 'value' parameter should come from URI.");
Assert.True(description.ParameterDescriptions.Any(param => param.Source == ApiParameterSource.FromBody && param.Name == "bodyValue"), "The 'bodyValue' parameter should come from body.");
}
[Fact]
public void FromBodyParameterSource_ShowUpCorrectlyOnDescription()
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute("Default", "{controller}/{action}/{id}", new { id = RouteParameter.Optional });
DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, typeof(ParameterSourceController));
config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector);
IApiExplorer explorer = config.Services.GetApiExplorer();
ApiDescription description = explorer.ApiDescriptions.FirstOrDefault(desc => desc.ActionDescriptor.ActionName == "PostSimpleTypeFromBody");
Assert.NotNull(description);
Assert.True(description.ParameterDescriptions.All(param => param.Source == ApiParameterSource.FromBody), "The parameter should come from Body.");
}
[Fact]
public void UnknownParameterSource_ShowUpCorrectlyOnDescription()
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute("Default", "{controller}/{action}/{id}", new { id = RouteParameter.Optional });
DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, typeof(ParameterSourceController));
config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector);
IApiExplorer explorer = config.Services.GetApiExplorer();
ApiDescription description = explorer.ApiDescriptions.FirstOrDefault(desc => desc.ActionDescriptor.ActionName == "GetFromHeaderAttribute");
Assert.NotNull(description);
Assert.True(description.ParameterDescriptions.All(param => param.Source == ApiParameterSource.Unknown), "The parameter source should be Unknown.");
}
[Fact]
public void EnumParameters_ShowUpCorrectlyOnDescription()
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute("Default", "{controller}");
DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, typeof(EnumParameterOverloadsController));
config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector);
IApiExplorer explorer = config.Services.GetApiExplorer();
ApiDescription description = explorer.ApiDescriptions.FirstOrDefault(desc => desc.ActionDescriptor.ActionName == "GetWithEnumParameter");
Assert.NotNull(description);
ApiParameterDescription parameterDescription = Assert.Single(description.ParameterDescriptions);
Assert.Equal(ApiParameterSource.FromUri, parameterDescription.Source);
Assert.Equal("EnumParameterOverloads?scope={scope}", description.RelativePath);
description = explorer.ApiDescriptions.FirstOrDefault(desc => desc.ActionDescriptor.ActionName == "GetWithTwoEnumParameters");
Assert.NotNull(description);
Assert.Equal(2, description.ParameterDescriptions.Count);
Assert.Equal(ApiParameterSource.FromUri, description.ParameterDescriptions[0].Source);
Assert.Equal(ApiParameterSource.FromUri, description.ParameterDescriptions[1].Source);
Assert.Equal("EnumParameterOverloads?level={level}&kind={kind}", description.RelativePath);
description = explorer.ApiDescriptions.FirstOrDefault(desc => desc.ActionDescriptor.ActionName == "GetWithNullableEnumParameter");
Assert.NotNull(description);
parameterDescription = Assert.Single(description.ParameterDescriptions);
Assert.Equal(ApiParameterSource.FromUri, parameterDescription.Source);
Assert.Equal("EnumParameterOverloads?level={level}", description.RelativePath);
}
[Theory]
[InlineData("api/values/{id}", "", "Get")]
[InlineData("api/values", "?value={value}", "GetFromUri")]
[InlineData("api/values", "?X={X}&Y={Y}", "GetPoint")]
[InlineData("api/values", "?origin.X={origin.X}&origin.Y={origin.Y}&end.X={end.X}&end.Y={end.Y}", "GetDistance")]
[InlineData("api/values", "?Latitude={Latitude}&Longitude={Longitude}", "GetLocation")]
[InlineData("api/values", "?value={value}", "GetConvertible")]
[InlineData("api/values", "", "GetNoDescribable")]
[InlineData("api/values", "?X={X}", "GetParticle")]
public void RelativePath_IsCorrectForTypesFromUri(string routeTemplate, string expectedQueryString, string methodName)
{
// Arrange
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute("Default", routeTemplate, new { controller = "ApiExplorerActionsWithParameters", action = methodName });
DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, typeof(ApiExplorerActionsWithParametersController));
config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector);
IApiExplorer explorer = config.Services.GetApiExplorer();
// Act
ApiDescription description = explorer.ApiDescriptions.FirstOrDefault(desc => desc.ActionDescriptor.ActionName == methodName);
// Assert
Assert.NotNull(description);
Assert.Equal(routeTemplate + expectedQueryString, description.RelativePath);
}
public class ApiExplorerActionsWithParametersController : ApiController
{
public void Get(int id) { }
public void GetFromUri([FromUri]string value) { }
public void GetPoint([FromUri] Point complexParameterObject) { }
public void GetDistance([FromUri] Point origin, [FromUri] Point end) { }
public void GetLocation(Location location) { }
public void GetConvertible([FromUri] ConvertibleFromString value) { }
public void GetNoDescribable([FromUri] NonDescribable nonDescribable) { }
public void GetParticle([FromUri] Particle particle) { }
}
public class Point
{
public int X { get; set; }
public int Y { get; set; }
}
[FromUri]
public class Location
{
public int Latitude { get; set; }
public int Longitude { get; set; }
public static Location Greenwich { get; set; }
}
[TypeConverter(typeof(ConvertibleFromStringConverter))]
public class ConvertibleFromString
{
public class ConvertibleFromStringConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
}
}
public int Id { get; set; }
public string Value { get; set; }
}
// We only support complex types whose all of their individual
// properties can be converted from string.
public class NonDescribable
{
public Point Point { get; set; }
}
// Get only, Set only, and private properties are ignored.
public class Particle
{
public int X { get; set; }
public int Y { get; private set; }
public int Z
{
get
{
return 0;
}
}
private int _mass;
public int Mass
{
set
{
_mass = value;
}
}
private string Color { get; set; }
}
}
}