Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace ServiceStack.Service.Tests
- {
- using System;
- using System.IO;
- using NUnit.Framework;
- using ServiceStack.Service;
- using ServiceStack.ServiceClient.Web;
- using ServiceStack.ServiceHost;
- using ServiceStack.Text;
- [RestService("/customers/{id}", "GET")]
- [RestService("/customers/by-code/{code}", "GET")]
- public class GetCustomer : IRequest<GetCustomerResponse>
- {
- public int? Id { get; set; }
- public string Code { get; set; }
- public string Name { get; set; }
- }
- public class GetCustomerResponse
- {
- }
- [RestService("/orders", Verbs = "POST")]
- [RestService("/orders/{id}", Verbs = "PUT")]
- public class SaveOrder : IRequest<SaveOrderResponse>
- {
- public int? Id { get; set; }
- public string Description { get; set; }
- }
- public class SaveOrderResponse
- {
- }
- [RestService("/orders/{orderId}/order-lines/{lineNumber}", "GET")]
- [RestService("/order-lines/{orderId}/{lineNumber}", "GET")]
- public class GetOrderLine : IRequest<GetOrderLineResponse>
- {
- public int LineNumber { get; set; }
- public int OrderId { get; set; }
- }
- public class GetOrderLineResponse
- {
- }
- [TestFixture]
- public class RoutesResolvingTests
- {
- private RestClientMock mockClient;
- [SetUp]
- public void SetUp()
- {
- this.mockClient = new RestClientMock();
- }
- [Test]
- public void Should_resolve_different_urls_for_different_properties()
- {
- SpyUrl(new GetCustomer { Code = "CustomerCode" }).ShouldEqual("GET /customers/by-code/CustomerCode");
- SpyUrl(new GetCustomer { Id = 1 }).ShouldEqual("GET /customers/1");
- }
- [Test]
- public void Should_throw_on_ambiguous_routes_match()
- {
- var ex = Catch.Exception(() => SpyUrl(new GetCustomer { Code = "CustomerCode", Id = 1 }));
- ex.ShouldBeOfType<InvalidOperationException>()
- .Message
- .ShouldContain("Ambiguous matching routes found for '{0}' request:".Fmt(typeof(GetCustomer).Name))
- .ShouldContain("/customers/{id}")
- .ShouldContain("/customers/by-code/{code}");
- }
- [Test]
- public void Should_throw_when_none_of_path_matches()
- {
- Catch.Exception(() => SpyUrl(new GetCustomer())).ShouldBeOfType<InvalidOperationException>()
- .Message.ShouldEqual(@"None of the given rest routes matches 'GetCustomer' request:
- /customers/by-code/{code}: Could not match following variables: code
- /customers/{id}: Could not match following variables: id");
- }
- [Test]
- public void Should_escape_matched_path_parts()
- {
- SpyUrl(new GetCustomer { Code = "* +" }).ShouldEqual("GET /customers/by-code/*%20%2B");
- }
- [Test]
- public void Should_escape_non_matched_properties_and_append_them_as_url_parameters_for_GET_request()
- {
- SpyUrl(new GetCustomer { Code = "Code", Name = "? ?" }).ShouldEqual("GET /customers/by-code/Code?name=%3F%20%3F");
- }
- [Test]
- public void Should_choose_most_specific_url_when_several_urls_matched()
- {
- SpyUrl(new SaveOrder { Id = 5 }).ShouldEqual("PUT /orders/5");
- SpyUrl(new SaveOrder()).ShouldEqual("POST /orders");
- }
- [Test]
- public void Should_choose_shortest_path_for_routes_with_same_variables()
- {
- SpyUrl(new GetOrderLine { OrderId = 1, LineNumber = 2 }).ShouldEqual("GET /order-lines/1/2");
- }
- [Test]
- public void Should_send_request_dto_for_POST_and_PUT_requests()
- {
- var request = new SaveOrder();
- mockClient.Send(request);
- mockClient.Request.ShouldBeTheSameAs(request);
- mockClient.HttpVerb.ShouldEqual("POST");
- request = new SaveOrder { Id = 1 };
- mockClient.Send(request);
- mockClient.Request.ShouldBeTheSameAs(request);
- mockClient.HttpVerb.ShouldEqual("PUT");
- }
- [Test]
- public void Should_not_append_query_params_for_POST_and_PUT_requests()
- {
- SpyUrl(new SaveOrder { Description = "Description" }).ShouldNotContain("?").ShouldEqual("POST /orders");
- SpyUrl(new SaveOrder { Id = 1, Description = "Description" }).ShouldNotContain("?").ShouldEqual("PUT /orders/1");
- }
- private string SpyUrl<T>(IRequest<T> request)
- {
- this.mockClient.Send(request);
- return this.mockClient.HttpVerb + " " + this.mockClient.Url;
- }
- }
- public class RestClientMock : IRestClient
- {
- public string Url { get; set; }
- public string HttpVerb { get; set; }
- public object Request { get; set; }
- public TResponse Get<TResponse>(string relativeOrAbsoluteUrl)
- {
- HttpVerb = HttpMethod.Get;
- Url = relativeOrAbsoluteUrl;
- return default(TResponse);
- }
- public TResponse Delete<TResponse>(string relativeOrAbsoluteUrl)
- {
- HttpVerb = HttpMethod.Delete;
- Url = relativeOrAbsoluteUrl;
- return default(TResponse);
- }
- public TResponse Post<TResponse>(string relativeOrAbsoluteUrl, object request)
- {
- Request = request;
- HttpVerb = HttpMethod.Post;
- Url = relativeOrAbsoluteUrl;
- return default(TResponse);
- }
- public TResponse Put<TResponse>(string relativeOrAbsoluteUrl, object request)
- {
- Request = request;
- HttpVerb = HttpMethod.Put;
- Url = relativeOrAbsoluteUrl;
- return default(TResponse);
- }
- public TResponse Patch<TResponse>(string relativeOrAbsoluteUrl, object request)
- {
- HttpVerb = HttpMethod.Patch;
- Url = relativeOrAbsoluteUrl;
- return default(TResponse);
- }
- public TResponse PostFile<TResponse>(string relativeOrAbsoluteUrl, FileInfo fileToUpload, string mimeType)
- {
- throw new System.NotImplementedException();
- }
- }
- }
Add Comment
Please, Sign In to add comment