/test/System.Web.Http.Integration.Test/ApiExplorer/RouteConstraintsTest.cs

https://bitbucket.org/mdavid/aspnetwebstack · C# · 116 lines · 103 code · 13 blank · 0 comment · 0 complexity · a399c9303ff0ce596dca461fcf9dd54c MD5 · raw file

  1. using System.Collections.Generic;
  2. using System.Net.Http;
  3. using System.Web.Http.Description;
  4. using System.Web.Http.Dispatcher;
  5. using System.Web.Http.Routing;
  6. using Xunit.Extensions;
  7. namespace System.Web.Http.ApiExplorer
  8. {
  9. public class RouteConstraintsTest
  10. {
  11. public static IEnumerable<object[]> HttpMethodConstraints_LimitsTheDescriptions_PropertyData
  12. {
  13. get
  14. {
  15. object controllerType = typeof(ItemController);
  16. object expectedApiDescriptions = new List<object>
  17. {
  18. new { HttpMethod = HttpMethod.Get, RelativePath = "Item?name={name}&series={series}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2},
  19. new { HttpMethod = HttpMethod.Put, RelativePath = "Item", HasRequestFormatters = true, HasResponseFormatters = true, NumberOfParameters = 1}
  20. };
  21. yield return new[] { controllerType, expectedApiDescriptions };
  22. controllerType = typeof(OverloadsController);
  23. expectedApiDescriptions = new List<object>
  24. {
  25. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/{id}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 1},
  26. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 0},
  27. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads?name={name}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 1},
  28. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/{id}?name={name}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2},
  29. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads?name={name}&age={age}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2},
  30. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads?name={name}&age={age}&ssn={ssn}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 3},
  31. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/{id}?name={name}&ssn={ssn}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 3},
  32. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads?name={name}&ssn={ssn}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2}
  33. };
  34. yield return new[] { controllerType, expectedApiDescriptions };
  35. }
  36. }
  37. [Theory]
  38. [PropertyData("HttpMethodConstraints_LimitsTheDescriptions_PropertyData")]
  39. public void HttpMethodConstraints_LimitsTheDescriptions(Type controllerType, List<object> expectedResults)
  40. {
  41. HttpConfiguration config = new HttpConfiguration();
  42. config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional }, new { routeConstraint = new HttpMethodConstraint(HttpMethod.Get, HttpMethod.Put) });
  43. DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerType);
  44. config.ServiceResolver.SetService(typeof(IHttpControllerSelector), controllerSelector);
  45. IApiExplorer explorer = config.ServiceResolver.GetApiExplorer();
  46. ApiExplorerHelper.VerifyApiDescriptions(explorer.ApiDescriptions, expectedResults);
  47. }
  48. public static IEnumerable<object[]> RegexConstraint_LimitsTheController_PropertyData
  49. {
  50. get
  51. {
  52. object[] controllerTypes = new Type[] { typeof(OverloadsController), typeof(ItemController) };
  53. object expectedApiDescriptions = new List<object>
  54. {
  55. new { HttpMethod = HttpMethod.Get, RelativePath = "Item?name={name}&series={series}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2},
  56. new { HttpMethod = HttpMethod.Post, RelativePath = "Item", HasRequestFormatters = true, HasResponseFormatters = true, NumberOfParameters = 1},
  57. new { HttpMethod = HttpMethod.Put, RelativePath = "Item", HasRequestFormatters = true, HasResponseFormatters = true, NumberOfParameters = 1},
  58. new { HttpMethod = HttpMethod.Delete, RelativePath = "Item/{id}", HasRequestFormatters = false, HasResponseFormatters = false, NumberOfParameters = 1}
  59. };
  60. yield return new[] { controllerTypes, expectedApiDescriptions };
  61. }
  62. }
  63. [Theory]
  64. [PropertyData("RegexConstraint_LimitsTheController_PropertyData")]
  65. public void RegexConstraint_LimitsTheController(Type[] controllerTypes, List<object> expectedResults)
  66. {
  67. HttpConfiguration config = new HttpConfiguration();
  68. config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional }, new { controller = "It.*" }); // controllers that start with "It"
  69. DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerTypes);
  70. config.ServiceResolver.SetService(typeof(IHttpControllerSelector), controllerSelector);
  71. IApiExplorer explorer = config.ServiceResolver.GetApiExplorer();
  72. ApiExplorerHelper.VerifyApiDescriptions(explorer.ApiDescriptions, expectedResults);
  73. }
  74. public static IEnumerable<object[]> RegexConstraint_LimitsTheAction_PropertyData
  75. {
  76. get
  77. {
  78. object[] controllerTypes = new Type[] { typeof(OverloadsController), typeof(ItemController) };
  79. object expectedApiDescriptions = new List<object>
  80. {
  81. new { HttpMethod = HttpMethod.Get, RelativePath = "Item/GetItem?name={name}&series={series}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2},
  82. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/GetPersonByNameAndId/{id}?name={name}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2},
  83. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/GetPersonByNameAndAge?name={name}&age={age}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2},
  84. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/GetPersonByNameAgeAndSsn?name={name}&age={age}&ssn={ssn}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 3},
  85. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/GetPersonByNameIdAndSsn/{id}?name={name}&ssn={ssn}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 3},
  86. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/GetPersonByNameAndSsn?name={name}&ssn={ssn}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2}
  87. };
  88. yield return new[] { controllerTypes, expectedApiDescriptions };
  89. }
  90. }
  91. [Theory]
  92. [PropertyData("RegexConstraint_LimitsTheAction_PropertyData")]
  93. public void RegexConstraint_LimitsTheAction(Type[] controllerTypes, List<object> expectedResults)
  94. {
  95. HttpConfiguration config = new HttpConfiguration();
  96. config.Routes.MapHttpRoute("Default", "{controller}/{action}/{id}", new { id = RouteParameter.Optional }, new { action = "Get.+" }); // actions that start with "Get" and at least one extra character
  97. DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerTypes);
  98. config.ServiceResolver.SetService(typeof(IHttpControllerSelector), controllerSelector);
  99. IApiExplorer explorer = config.ServiceResolver.GetApiExplorer();
  100. ApiExplorerHelper.VerifyApiDescriptions(explorer.ApiDescriptions, expectedResults);
  101. }
  102. }
  103. }