Guest User

Untitled

a guest
Dec 10th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. namespace ServiceStack.Service.Tests
  2. {
  3. using System;
  4. using System.IO;
  5.  
  6. using NUnit.Framework;
  7.  
  8. using ServiceStack.Service;
  9. using ServiceStack.ServiceClient.Web;
  10. using ServiceStack.ServiceHost;
  11. using ServiceStack.Text;
  12.  
  13. [RestService("/customers/{id}", "GET")]
  14. [RestService("/customers/by-code/{code}", "GET")]
  15. public class GetCustomer : IRequest<GetCustomerResponse>
  16. {
  17. public int? Id { get; set; }
  18. public string Code { get; set; }
  19. public string Name { get; set; }
  20. }
  21.  
  22. public class GetCustomerResponse
  23. {
  24. }
  25.  
  26. [RestService("/orders", Verbs = "POST")]
  27. [RestService("/orders/{id}", Verbs = "PUT")]
  28. public class SaveOrder : IRequest<SaveOrderResponse>
  29. {
  30. public int? Id { get; set; }
  31.  
  32. public string Description { get; set; }
  33. }
  34.  
  35. public class SaveOrderResponse
  36. {
  37. }
  38.  
  39. [RestService("/orders/{orderId}/order-lines/{lineNumber}", "GET")]
  40. [RestService("/order-lines/{orderId}/{lineNumber}", "GET")]
  41. public class GetOrderLine : IRequest<GetOrderLineResponse>
  42. {
  43. public int LineNumber { get; set; }
  44. public int OrderId { get; set; }
  45. }
  46.  
  47. public class GetOrderLineResponse
  48. {
  49. }
  50.  
  51. [TestFixture]
  52. public class RoutesResolvingTests
  53. {
  54. private RestClientMock mockClient;
  55.  
  56. [SetUp]
  57. public void SetUp()
  58. {
  59. this.mockClient = new RestClientMock();
  60. }
  61.  
  62. [Test]
  63. public void Should_resolve_different_urls_for_different_properties()
  64. {
  65. SpyUrl(new GetCustomer { Code = "CustomerCode" }).ShouldEqual("GET /customers/by-code/CustomerCode");
  66. SpyUrl(new GetCustomer { Id = 1 }).ShouldEqual("GET /customers/1");
  67. }
  68.  
  69. [Test]
  70. public void Should_throw_on_ambiguous_routes_match()
  71. {
  72. var ex = Catch.Exception(() => SpyUrl(new GetCustomer { Code = "CustomerCode", Id = 1 }));
  73. ex.ShouldBeOfType<InvalidOperationException>()
  74. .Message
  75. .ShouldContain("Ambiguous matching routes found for '{0}' request:".Fmt(typeof(GetCustomer).Name))
  76. .ShouldContain("/customers/{id}")
  77. .ShouldContain("/customers/by-code/{code}");
  78. }
  79.  
  80. [Test]
  81. public void Should_throw_when_none_of_path_matches()
  82. {
  83. Catch.Exception(() => SpyUrl(new GetCustomer())).ShouldBeOfType<InvalidOperationException>()
  84. .Message.ShouldEqual(@"None of the given rest routes matches 'GetCustomer' request:
  85. /customers/by-code/{code}: Could not match following variables: code
  86. /customers/{id}: Could not match following variables: id");
  87. }
  88.  
  89. [Test]
  90. public void Should_escape_matched_path_parts()
  91. {
  92. SpyUrl(new GetCustomer { Code = "* +" }).ShouldEqual("GET /customers/by-code/*%20%2B");
  93. }
  94.  
  95. [Test]
  96. public void Should_escape_non_matched_properties_and_append_them_as_url_parameters_for_GET_request()
  97. {
  98. SpyUrl(new GetCustomer { Code = "Code", Name = "? ?" }).ShouldEqual("GET /customers/by-code/Code?name=%3F%20%3F");
  99. }
  100.  
  101. [Test]
  102. public void Should_choose_most_specific_url_when_several_urls_matched()
  103. {
  104. SpyUrl(new SaveOrder { Id = 5 }).ShouldEqual("PUT /orders/5");
  105. SpyUrl(new SaveOrder()).ShouldEqual("POST /orders");
  106. }
  107.  
  108. [Test]
  109. public void Should_choose_shortest_path_for_routes_with_same_variables()
  110. {
  111. SpyUrl(new GetOrderLine { OrderId = 1, LineNumber = 2 }).ShouldEqual("GET /order-lines/1/2");
  112. }
  113.  
  114. [Test]
  115. public void Should_send_request_dto_for_POST_and_PUT_requests()
  116. {
  117. var request = new SaveOrder();
  118. mockClient.Send(request);
  119. mockClient.Request.ShouldBeTheSameAs(request);
  120. mockClient.HttpVerb.ShouldEqual("POST");
  121.  
  122. request = new SaveOrder { Id = 1 };
  123. mockClient.Send(request);
  124. mockClient.Request.ShouldBeTheSameAs(request);
  125. mockClient.HttpVerb.ShouldEqual("PUT");
  126. }
  127.  
  128. [Test]
  129. public void Should_not_append_query_params_for_POST_and_PUT_requests()
  130. {
  131. SpyUrl(new SaveOrder { Description = "Description" }).ShouldNotContain("?").ShouldEqual("POST /orders");
  132. SpyUrl(new SaveOrder { Id = 1, Description = "Description" }).ShouldNotContain("?").ShouldEqual("PUT /orders/1");
  133. }
  134.  
  135. private string SpyUrl<T>(IRequest<T> request)
  136. {
  137. this.mockClient.Send(request);
  138. return this.mockClient.HttpVerb + " " + this.mockClient.Url;
  139. }
  140. }
  141.  
  142. public class RestClientMock : IRestClient
  143. {
  144. public string Url { get; set; }
  145. public string HttpVerb { get; set; }
  146. public object Request { get; set; }
  147.  
  148. public TResponse Get<TResponse>(string relativeOrAbsoluteUrl)
  149. {
  150. HttpVerb = HttpMethod.Get;
  151. Url = relativeOrAbsoluteUrl;
  152. return default(TResponse);
  153. }
  154.  
  155. public TResponse Delete<TResponse>(string relativeOrAbsoluteUrl)
  156. {
  157. HttpVerb = HttpMethod.Delete;
  158. Url = relativeOrAbsoluteUrl;
  159. return default(TResponse);
  160. }
  161.  
  162. public TResponse Post<TResponse>(string relativeOrAbsoluteUrl, object request)
  163. {
  164. Request = request;
  165. HttpVerb = HttpMethod.Post;
  166. Url = relativeOrAbsoluteUrl;
  167. return default(TResponse);
  168. }
  169.  
  170. public TResponse Put<TResponse>(string relativeOrAbsoluteUrl, object request)
  171. {
  172. Request = request;
  173. HttpVerb = HttpMethod.Put;
  174. Url = relativeOrAbsoluteUrl;
  175. return default(TResponse);
  176. }
  177.  
  178. public TResponse Patch<TResponse>(string relativeOrAbsoluteUrl, object request)
  179. {
  180. HttpVerb = HttpMethod.Patch;
  181. Url = relativeOrAbsoluteUrl;
  182. return default(TResponse);
  183. }
  184.  
  185. public TResponse PostFile<TResponse>(string relativeOrAbsoluteUrl, FileInfo fileToUpload, string mimeType)
  186. {
  187. throw new System.NotImplementedException();
  188. }
  189. }
  190. }
Add Comment
Please, Sign In to add comment