PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/mdavid/aspnetwebstack
C# | 214 lines | 186 code | 28 blank | 0 comment | 0 complexity | a53b63c5b9f9595e7b499393f7c3227c 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 Xunit;
  6. using Xunit.Extensions;
  7. namespace System.Web.Http.ApiExplorer
  8. {
  9. public class RoutesTest
  10. {
  11. [Fact]
  12. public void VerifyDescription_OnEmptyRoute()
  13. {
  14. HttpConfiguration config = new HttpConfiguration();
  15. IApiExplorer explorer = config.ServiceResolver.GetApiExplorer();
  16. Assert.NotNull(explorer);
  17. Assert.Equal(0, explorer.ApiDescriptions.Count);
  18. }
  19. [Fact]
  20. public void VerifyDescription_OnInvalidRoute()
  21. {
  22. HttpConfiguration config = new HttpConfiguration();
  23. config.Routes.MapHttpRoute("Invalid", "badRouteWithNoControler");
  24. IApiExplorer explorer = config.ServiceResolver.GetApiExplorer();
  25. Assert.NotNull(explorer);
  26. Assert.Equal(0, explorer.ApiDescriptions.Count);
  27. }
  28. public static IEnumerable<object[]> VerifyDescription_OnDefaultRoute_PropertyData
  29. {
  30. get
  31. {
  32. object controllerType = typeof(ItemController);
  33. object expectedApiDescriptions = new List<object>
  34. {
  35. new { HttpMethod = HttpMethod.Get, RelativePath = "Item?name={name}&series={series}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2},
  36. new { HttpMethod = HttpMethod.Post, RelativePath = "Item", HasRequestFormatters = true, HasResponseFormatters = true, NumberOfParameters = 1},
  37. new { HttpMethod = HttpMethod.Put, RelativePath = "Item", HasRequestFormatters = true, HasResponseFormatters = true, NumberOfParameters = 1},
  38. new { HttpMethod = HttpMethod.Delete, RelativePath = "Item/{id}", HasRequestFormatters = false, HasResponseFormatters = false, NumberOfParameters = 1}
  39. };
  40. yield return new[] { controllerType, expectedApiDescriptions };
  41. controllerType = typeof(OverloadsController);
  42. expectedApiDescriptions = new List<object>
  43. {
  44. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/{id}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 1},
  45. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 0},
  46. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads?name={name}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 1},
  47. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/{id}?name={name}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2},
  48. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads?name={name}&age={age}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2},
  49. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads?name={name}&age={age}&ssn={ssn}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 3},
  50. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/{id}?name={name}&ssn={ssn}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 3},
  51. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads?name={name}&ssn={ssn}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2},
  52. new { HttpMethod = HttpMethod.Post, RelativePath = "Overloads", HasRequestFormatters = true, HasResponseFormatters = true, NumberOfParameters = 1},
  53. new { HttpMethod = HttpMethod.Post, RelativePath = "Overloads?name={name}&age={age}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2},
  54. new { HttpMethod = HttpMethod.Delete, RelativePath = "Overloads/{id}?name={name}", HasRequestFormatters = false, HasResponseFormatters = false, NumberOfParameters = 2},
  55. new { HttpMethod = HttpMethod.Delete, RelativePath = "Overloads/{id}?name={name}&age={age}", HasRequestFormatters = false, HasResponseFormatters = false, NumberOfParameters = 3}
  56. };
  57. yield return new[] { controllerType, expectedApiDescriptions };
  58. }
  59. }
  60. [Theory]
  61. [PropertyData("VerifyDescription_OnDefaultRoute_PropertyData")]
  62. public void VerifyDescription_OnDefaultRoute(Type controllerType, List<object> expectedResults)
  63. {
  64. HttpConfiguration config = new HttpConfiguration();
  65. config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional });
  66. DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerType);
  67. config.ServiceResolver.SetService(typeof(IHttpControllerSelector), controllerSelector);
  68. IApiExplorer explorer = config.ServiceResolver.GetApiExplorer();
  69. ApiExplorerHelper.VerifyApiDescriptions(explorer.ApiDescriptions, expectedResults);
  70. }
  71. public static IEnumerable<object[]> VerifyDescription_OnRouteWithControllerOnDefaults_PropertyData
  72. {
  73. get
  74. {
  75. object controllerType = typeof(ItemController);
  76. object expectedApiDescriptions = new List<object>
  77. {
  78. new { HttpMethod = HttpMethod.Get, RelativePath = "myitem?name={name}&series={series}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2},
  79. new { HttpMethod = HttpMethod.Post, RelativePath = "myitem", HasRequestFormatters = true, HasResponseFormatters = true, NumberOfParameters = 1},
  80. new { HttpMethod = HttpMethod.Put, RelativePath = "myitem", HasRequestFormatters = true, HasResponseFormatters = true, NumberOfParameters = 1},
  81. new { HttpMethod = HttpMethod.Delete, RelativePath = "myitem/{id}", HasRequestFormatters = false, HasResponseFormatters = false, NumberOfParameters = 1}
  82. };
  83. yield return new[] { controllerType, expectedApiDescriptions };
  84. }
  85. }
  86. [Theory]
  87. [PropertyData("VerifyDescription_OnRouteWithControllerOnDefaults_PropertyData")]
  88. public void VerifyDescription_OnRouteWithControllerOnDefaults(Type controllerType, List<object> expectedResults)
  89. {
  90. HttpConfiguration config = new HttpConfiguration();
  91. config.Routes.MapHttpRoute("Default", "myitem/{id}", new { controller = "Item", id = RouteParameter.Optional });
  92. DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerType);
  93. config.ServiceResolver.SetService(typeof(IHttpControllerSelector), controllerSelector);
  94. IApiExplorer explorer = config.ServiceResolver.GetApiExplorer();
  95. ApiExplorerHelper.VerifyApiDescriptions(explorer.ApiDescriptions, expectedResults);
  96. }
  97. public static IEnumerable<object[]> VerifyDescription_OnRouteWithActionVariable_PropertyData
  98. {
  99. get
  100. {
  101. object controllerType = typeof(ItemController);
  102. object expectedApiDescriptions = new List<object>
  103. {
  104. new { HttpMethod = HttpMethod.Get, RelativePath = "Item/GetItem?name={name}&series={series}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2},
  105. new { HttpMethod = HttpMethod.Post, RelativePath = "Item/PostItem", HasRequestFormatters = true, HasResponseFormatters = true, NumberOfParameters = 1},
  106. new { HttpMethod = HttpMethod.Put, RelativePath = "Item/PostItem", HasRequestFormatters = true, HasResponseFormatters = true, NumberOfParameters = 1},
  107. new { HttpMethod = HttpMethod.Delete, RelativePath = "Item/RemoveItem/{id}", HasRequestFormatters = false, HasResponseFormatters = false, NumberOfParameters = 1}
  108. };
  109. yield return new[] { controllerType, expectedApiDescriptions };
  110. controllerType = typeof(OverloadsController);
  111. expectedApiDescriptions = new List<object>
  112. {
  113. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/Get/{id}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 1},
  114. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/Get", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 0},
  115. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/Get?name={name}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 1},
  116. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/GetPersonByNameAndId/{id}?name={name}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2},
  117. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/GetPersonByNameAndAge?name={name}&age={age}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2},
  118. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/GetPersonByNameAgeAndSsn?name={name}&age={age}&ssn={ssn}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 3},
  119. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/GetPersonByNameIdAndSsn/{id}?name={name}&ssn={ssn}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 3},
  120. new { HttpMethod = HttpMethod.Get, RelativePath = "Overloads/GetPersonByNameAndSsn?name={name}&ssn={ssn}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2},
  121. new { HttpMethod = HttpMethod.Post, RelativePath = "Overloads/Post", HasRequestFormatters = true, HasResponseFormatters = true, NumberOfParameters = 1},
  122. new { HttpMethod = HttpMethod.Post, RelativePath = "Overloads/ActionDefaultedToPost?name={name}&age={age}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2},
  123. new { HttpMethod = HttpMethod.Delete, RelativePath = "Overloads/Delete/{id}?name={name}", HasRequestFormatters = false, HasResponseFormatters = false, NumberOfParameters = 2},
  124. new { HttpMethod = HttpMethod.Delete, RelativePath = "Overloads/Delete/{id}?name={name}&age={age}", HasRequestFormatters = false, HasResponseFormatters = false, NumberOfParameters = 3}
  125. };
  126. yield return new[] { controllerType, expectedApiDescriptions };
  127. }
  128. }
  129. [Theory]
  130. [PropertyData("VerifyDescription_OnRouteWithActionVariable_PropertyData")]
  131. public void VerifyDescription_OnRouteWithActionVariable(Type controllerType, List<object> expectedResults)
  132. {
  133. HttpConfiguration config = new HttpConfiguration();
  134. config.Routes.MapHttpRoute("Default", "{controller}/{action}/{id}", new { id = RouteParameter.Optional });
  135. DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerType);
  136. config.ServiceResolver.SetService(typeof(IHttpControllerSelector), controllerSelector);
  137. IApiExplorer explorer = config.ServiceResolver.GetApiExplorer();
  138. ApiExplorerHelper.VerifyApiDescriptions(explorer.ApiDescriptions, expectedResults);
  139. }
  140. public static IEnumerable<object[]> VerifyDescription_On_RouteWithActionOnDefaults_PropertyData
  141. {
  142. get
  143. {
  144. object controllerType = typeof(ItemController);
  145. object expectedApiDescriptions = new List<object>
  146. {
  147. new { HttpMethod = HttpMethod.Delete, RelativePath = "Item/{id}", HasRequestFormatters = false, HasResponseFormatters = false, NumberOfParameters = 1}
  148. };
  149. yield return new[] { controllerType, expectedApiDescriptions };
  150. }
  151. }
  152. [Theory]
  153. [PropertyData("VerifyDescription_On_RouteWithActionOnDefaults_PropertyData")]
  154. public void VerifyDescription_On_RouteWithActionOnDefaults(Type controllerType, List<object> expectedResults)
  155. {
  156. HttpConfiguration config = new HttpConfiguration();
  157. config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { action = "RemoveItem", id = RouteParameter.Optional });
  158. DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerType);
  159. config.ServiceResolver.SetService(typeof(IHttpControllerSelector), controllerSelector);
  160. IApiExplorer explorer = config.ServiceResolver.GetApiExplorer();
  161. ApiExplorerHelper.VerifyApiDescriptions(explorer.ApiDescriptions, expectedResults);
  162. }
  163. [Fact]
  164. public void InvalidActionNameOnRoute_DoesNotThrow()
  165. {
  166. Type controllerType = typeof(OverloadsController);
  167. HttpConfiguration config = new HttpConfiguration();
  168. config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { action = "ActionThatDoesNotExist", id = RouteParameter.Optional });
  169. DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerType);
  170. config.ServiceResolver.SetService(typeof(IHttpControllerSelector), controllerSelector);
  171. IApiExplorer explorer = config.ServiceResolver.GetApiExplorer();
  172. Assert.Empty(explorer.ApiDescriptions);
  173. }
  174. [Fact]
  175. public void InvalidControllerNameOnRoute_DoesNotThrow()
  176. {
  177. Type controllerType = typeof(OverloadsController);
  178. HttpConfiguration config = new HttpConfiguration();
  179. config.Routes.MapHttpRoute("Default", "mycontroller/{id}", new { controller = "ControllerThatDoesNotExist", id = RouteParameter.Optional });
  180. DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerType);
  181. config.ServiceResolver.SetService(typeof(IHttpControllerSelector), controllerSelector);
  182. IApiExplorer explorer = config.ServiceResolver.GetApiExplorer();
  183. Assert.Empty(explorer.ApiDescriptions);
  184. }
  185. }
  186. }