Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
file 955 lines (793 sloc) 32.022 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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Threading;
using NUnit.Framework;
using ServiceStack;
using ServiceStack.Data;
using ServiceStack.Host;
using ServiceStack.Logging;
using ServiceStack.MsgPack;
using ServiceStack.NetSerializer;
using ServiceStack.OrmLite;
using ServiceStack.Text;
using ServiceStack.Web;

namespace RazorRockstars.Console.Files
{
    /// New Proposal, keeping ServiceStack's message-based semantics:
    /// Inspired by Ivan's proposal: http://korneliuk.blogspot.com/2012/08/servicestack-reusing-dtos.html
    ///
    /// To align with ServiceStack's message-based design, an "Action":
    /// - is public and only supports a single argument the typed Request DTO
    /// - method name matches a HTTP Method or "Any" which is used as a fallback (for all methods) if it exists
    /// - only returns object or void
    ///
    /// Notes:
    /// Content Negotiation built-in, i.e. by default each method/route is automatically available in every registered Content-Type (HTTP Only).
    /// New API are also available in ServiceStack's typed service clients (they're actually even more succinct :)
    /// Any Views rendered is based on Request or Returned DTO type, see: http://razor.servicestack.net/#unified-stack

[Route("/reqstars/search", "GET")]
[Route("/reqstars/aged/{Age}")]
    public class SearchReqstars : IReturn<ReqstarsResponse>
    {
        public int? Age { get; set; }
    }

[Route("/reqstars", "GET")]
    public class AllReqstars : IReturn<List<Reqstar>> { }

[Route("/reqstars/cached/{Aged}", "GET")]
    public class CachedAllReqstars : IReturn<ReqstarsResponse>
    {
        public int Aged { get; set; }
    }

    public class ReqstarsResponse
    {
        public int Total { get; set; }
        public int? Aged { get; set; }
        public List<Reqstar> Results { get; set; }
        public ResponseStatus ResponseStatus { get; set; }
    }

[Route("/reqstars/reset")]
    public class ResetReqstar : IReturnVoid { }

[Route("/reqstars/{Id}", "GET")]
    public class GetReqstar : IReturn<Reqstar>
    {
        public int Id { get; set; }
    }

[Route("/reqstars/{Id}/delete")]
    public class DeleteReqstar : IReturnVoid
    {
        public int Id { get; set; }
    }

[Route("/reqstars")]
    public class Reqstar : IReturn<List<Reqstar>>
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int? Age { get; set; }
        public bool Alive { get; set; }

        public Reqstar() { }
        public Reqstar(int id, string firstName, string lastName, int age)
        {
            Id = id;
            FirstName = firstName;
            LastName = lastName;
            Age = age;
        }
    }

[Route("/reqstars/{Id}", "PATCH")]
    public class UpdateReqstar : IReturn<Reqstar>
    {
        public int Id { get; set; }
        public int Age { get; set; }

        public UpdateReqstar() { }
        public UpdateReqstar(int id, int age)
        {
            Id = id;
            Age = age;
        }
    }

[Route("/modelerror")]
[Route("/modelerror/{StatusCode}")]
[Route("/modelerror/{StatusCode}/{Message}")]
    public class ModelError : IReturn<ModelErrorResponse>
    {
        public int? StatusCode { get; set; }
        public string Message { get; set; }
    }

    public class ModelErrorResponse
    {
        public string Result { get; set; }
        public ResponseStatus ResponseStatus { get; set; }
    }

    public class RoutelessReqstar : IReturn<RoutelessReqstar>
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public class ReqstarsByNames : List<string> { }

[Route("/richrequest")]
    public class RichRequest : IReturn<RichRequest>
    {
        public List<string> StringList { get; set; }
        public HashSet<string> StringSet { get; set; }
        public string[] StringArray { get; set; }
    }

    public class ReqstarsService : Service
    {
        public static Reqstar[] SeedData = new[] {
            new Reqstar(1, "Foo", "Bar", 20),
            new Reqstar(2, "Something", "Else", 30),
            new Reqstar(3, "Foo2", "Bar2", 20),
        };

[EnableCors]
        public void Options(Reqstar reqstar) { }

        public void Any(ResetReqstar request)
        {
            Db.DeleteAll<Reqstar>();
            Db.Insert(SeedData);
        }

        public ReqstarsResponse Get(SearchReqstars request)
        {
            if (request.Age.HasValue && request.Age <= 0)
                throw new ArgumentException("Invalid Age");

            return new ReqstarsResponse //matches ReqstarsResponse.cshtml razor view
            {
                Aged = request.Age,
                Total = Db.Scalar<int>("select count(*) from Reqstar"),
                Results = request.Age.HasValue
                    ? Db.Select<Reqstar>(q => q.Age == request.Age.Value)
                    : Db.Select<Reqstar>()
            };
        }

        public List<Reqstar> Any(AllReqstars request)
        {
            return Db.Select<Reqstar>();
        }

        public object Any(CachedAllReqstars request)
        {
            if (request.Aged <= 0)
                throw new ArgumentException("Invalid Age");

            var cacheKey = typeof(CachedAllReqstars).Name;
            return base.Request.ToOptimizedResultUsingCache(base.Cache, cacheKey, () =>
                new ReqstarsResponse {
                    Aged = request.Aged,
                    Total = Db.Scalar<int>("select count(*) from Reqstar"),
                    Results = Db.Select<Reqstar>(q => q.Age == request.Aged)
                });
        }

[ClientCanSwapTemplates] //allow action-level filters
        public Reqstar Get(GetReqstar request)
        {
            return Db.SingleById<Reqstar>(request.Id);
        }

        public object Post(Reqstar request)
        {
            if (!request.Age.HasValue)
                throw new ArgumentException("Age is required");

            Db.Insert(request.ConvertTo<Reqstar>());
            return Db.Select<Reqstar>();
        }

        public Reqstar Patch(UpdateReqstar request)
        {
            Db.Update<Reqstar>(request, x => x.Id == request.Id);
            return Db.SingleById<Reqstar>(request.Id);
        }

        public void Any(DeleteReqstar request)
        {
            Db.DeleteById<Reqstar>(request.Id);
        }

        public RoutelessReqstar Any(RoutelessReqstar request)
        {
            return request;
        }

        public List<Reqstar> Any(ReqstarsByNames request)
        {
            return Db.Select<Reqstar>(q => Sql.In(q.FirstName, request.ToArray()));
        }

        public MixedDataContractResponse Any(MixedDataContract request)
        {
            return new MixedDataContractResponse();
        }

        public InheritsResponse Any(Inherits request)
        {
            return new InheritsResponse();
        }

        public RichRequest Get(RichRequest request)
        {
            return request;
        }

        public object Any(ModelError request)
        {
            if (request.StatusCode.HasValue)
            {
                throw new HttpError(
                    request.StatusCode.Value,
                    typeof(ArgumentException).Name,
                    request.Message);
            }
            throw new ArgumentException(request.Message + " was triggered by client");
        }
    }


[Route("/ignore/{ignore}")]
    public class IgnoreRoute1
    {
        public string Name { get; set; }
    }
[Route("/ignore/{ignore}/between")]
    public class IgnoreRoute2
    {
        public string Name { get; set; }
    }
[Route("/ignore/{ignore}/and/{ignore}")]
[Route("/ignore/{ignore}/with/{name}")]
    public class IgnoreRoute3
    {
        public string Name { get; set; }
    }
[Route("/ignorewildcard/{ignore*}")]
    public class IgnoreWildcardRoute
    {
        public string Name { get; set; }
    }

    public class IgnoreService : Service
    {
        public object Get(IgnoreRoute1 request)
        {
            return request;
        }

        public object Get(IgnoreRoute2 request)
        {
            return request;
        }

        public object Get(IgnoreRoute3 request)
        {
            return request;
        }

        public object Get(IgnoreWildcardRoute request)
        {
            return request;
        }
    }

[DataContract(Name = "MixName", Namespace = "http://mix.namespace.com")]
    public class MixedDataContract : IReturn<MixedDataContractResponse>
    {
[DataMember(Name = "MixId", Order = 1, EmitDefaultValue = false, IsRequired = true)]
        public int Id { get; set; }
    }

[DataContract(Name = "MixedDCResponse")]
    public class MixedDataContractResponse
    {
[DataMember(Name = "MixTotal")]
        public int Total { get; set; }
[DataMember(Order = 2)]
        public int? Aged { get; set; }
[DataMember(EmitDefaultValue = false)]
        public List<Rockstar> Results { get; set; }
[DataMember(IsRequired = true)]
        public string Required { get; set; }

[DataMember]
        public MixDataType MixDataType { get; set; }
    }

    public class MixDataType
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class Inherits : InheritsBase
    {
        public string Name { get; set; }
    }

    public class InheritsBase
    {
        public int Id { get; set; }
    }

    public class InheritsResponse
    {
        public string Result { get; set; }
    }

[Route("/filterattributes")]
    public class RequestDto : IReturn<ResponseDto> { }
    public class ResponseDto
    {
        public string Foo { get; set; }
    }

[MyResponseFilter]
[MyRequestFilter]
    public class MyService : Service
    {
        public object Get(RequestDto request)
        {
            return new ResponseDto { Foo = "Bar" };
        }
    }

    public class MyResponseFilterAttribute : ResponseFilterAttribute
    {
        public static int Called = 0;
        public override void Execute(IRequest req, IResponse res, object responseDto)
        {
            Called++;
            var x = responseDto;
        }
    }

    public class MyRequestFilterAttribute : RequestFilterAttribute
    {
        public static int Called = 0;
        public override void Execute(IRequest req, IResponse res, object responseDto)
        {
            Called++;
            var x = responseDto;
        }
    }

[TestFixture]
    public class ReqStarsServiceTests
    {
        private const string ListeningOn = "http://*:1337/";
        public const string Host = "http://localhost:1337";

        private const string BaseUri = Host + "/";

        private AppHost appHost;

        private Stopwatch startedAt;

[TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            LogManager.LogFactory = new ConsoleLogFactory();
            startedAt = Stopwatch.StartNew();
            appHost = new AppHost {
                EnableRazor = true, //Uncomment for faster tests!
            };
            appHost.Plugins.Add(new MsgPackFormat());
            //appHost.Plugins.Add(new NetSerializerFormat());
            //Fast
            appHost.Init();
            HostContext.Config.DebugMode = true;
            appHost.Start(ListeningOn);
        }

        private IDbConnection db;

[SetUp]
        public void SetUp()
        {
            db = appHost.TryResolve<IDbConnectionFactory>().OpenDbConnection();
            db.DropAndCreateTable<Reqstar>();
            db.Insert(ReqstarsService.SeedData);
        }

[TearDown]
        public void TearDown()
        {
            db.Dispose();
        }

[TestFixtureTearDown]
        public void TestFixtureTearDown()
        {
            "Time Taken {0}ms".Fmt(startedAt.ElapsedMilliseconds).Print();
            appHost.Dispose();
        }

[Explicit("Debug Run")]
[Test]
        public void RunFor10Mins()
        {
            Thread.Sleep(TimeSpan.FromMinutes(10));
        }

[Explicit("Concurrent Run")]
[Test]
        public void Concurrent_GetReqstar_JSON()
        {
            const int NoOfThreads = 100;

            var client = new JsonServiceClient(BaseUri);

            int completed = 0;
            Exception lastEx = null;
            NoOfThreads.TimesAsync(i => {
                try
                {
                    var response = client.Get(new GetReqstar { Id = 1 });
                    Assert.That(response.Id, Is.EqualTo(1));
                }
                catch (Exception ex)
                {
                    lastEx = ex;
                }
                finally
                {
                    Interlocked.Increment(ref completed);
                }
            });

            while (completed < NoOfThreads)
                Thread.Sleep(100);

            if (lastEx != null)
                throw lastEx;
        }


[Explicit("Concurrent Run")]
[Test]
        public void Concurrent_GetReqstar_Razor()
        {
            const int NoOfThreads = 100;

            int completed = 0;
            Exception lastEx = null;
            string lastText = null;
            NoOfThreads.TimesAsync(i => {
                try
                {
                    lastText = "{0}/reqstars/1".Fmt(Host).GetStringFromUrl();
                }
                catch (Exception ex)
                {
                    lastEx = ex;
                }
                finally
                {
                    Interlocked.Increment(ref completed);
                }
            });

            while (completed < NoOfThreads)
                Thread.Sleep(100);

            lastText.Print();

            if (lastEx != null)
                throw lastEx;
        }

        protected static IRestClient[] RestClients =
{
new JsonServiceClient(BaseUri),
new XmlServiceClient(BaseUri),
new JsvServiceClient(BaseUri),
new MsgPackServiceClient(BaseUri),
            //new NetSerializerServiceClient(BaseUri),
};

        protected static IServiceClient[] ServiceClients =
            RestClients.OfType<IServiceClient>().ToArray();



[Test, TestCaseSource("RestClients")]
        public void Does_allow_sending_collections(IServiceClient client)
        {
            var results = client.Send<List<Reqstar>>(new ReqstarsByNames { "Foo", "Foo2" });
            results.PrintDump();
        }

[Test, TestCaseSource("RestClients")]
        public void Does_execute_request_and_response_filter_attributes(IRestClient client)
        {
            MyRequestFilterAttribute.Called = MyResponseFilterAttribute.Called = 0;
            var response = client.Get(new RequestDto());
            Assert.That(response.Foo, Is.EqualTo("Bar"));
            Assert.That(MyRequestFilterAttribute.Called, Is.EqualTo(1));
            Assert.That(MyResponseFilterAttribute.Called, Is.EqualTo(1));
        }

[Test]
        public void Can_Process_OPTIONS_request_with_Cors_ActionFilter()
        {
            var webReq = (HttpWebRequest)WebRequest.Create(Host + "/reqstars");
            webReq.Method = "OPTIONS";
            using (var webRes = webReq.GetResponse())
            {
                Assert.That(webRes.Headers["Access-Control-Allow-Origin"], Is.EqualTo("*"));
                Assert.That(webRes.Headers["Access-Control-Allow-Methods"], Is.EqualTo("GET, POST, PUT, DELETE, OPTIONS"));
                Assert.That(webRes.Headers["Access-Control-Allow-Headers"], Is.EqualTo("Content-Type"));

                var response = webRes.GetResponseStream().ReadFully();
                Assert.That(response.Length, Is.EqualTo(0));
            }
        }


[Test, TestCaseSource("RestClients")]
        public void Can_GET_AllReqstars(IRestClient client)
        {
            var allReqstars = client.Get<List<Reqstar>>("/reqstars");
            Assert.That(allReqstars.Count, Is.EqualTo(ReqstarsService.SeedData.Length));
        }

[Test, TestCaseSource("ServiceClients")]
        public void Can_SEND_AllReqstars_PrettyTypedApi(IServiceClient client)
        {
            var allReqstars = client.Send(new AllReqstars());
            Assert.That(allReqstars.Count, Is.EqualTo(ReqstarsService.SeedData.Length));
        }

[Test, TestCaseSource("RestClients")]
        public void Can_GET_AllReqstars_PrettyRestApi(IRestClient client)
        {
            var request = new AllReqstars();
            var response = client.Get(request);

            Assert.That(request.ToUrl("GET"), Is.EqualTo("/reqstars"));
            Assert.That(response.Count, Is.EqualTo(ReqstarsService.SeedData.Length));
        }

[Test]
        public void Can_GET_AllReqstars_View()
        {
            var html = "{0}/reqstars".Fmt(Host).GetStringFromUrl(accept: "text/html");
            html.Print();
            Assert.That(html, Is.StringContaining("<!--view:AllReqstars.cshtml-->"));
            Assert.That(html, Is.StringContaining("<!--template:HtmlReport.cshtml-->"));
        }


[Test, TestCaseSource("RestClients")]
        public void Can_GET_SearchReqstars(IRestClient client)
        {
            var response = client.Get<ReqstarsResponse>("/reqstars/search");
            Assert.That(response.Results.Count, Is.EqualTo(ReqstarsService.SeedData.Length));
        }

[Test, TestCaseSource("ServiceClients")]
        public void Disallows_SEND_SearchReqstars_PrettyTypedApi(IServiceClient client)
        {
            try
            {
                var response = client.Send(new SearchReqstars());
                Assert.Fail("POST's to SearchReqstars should not be allowed");
            }
            catch (WebServiceException webEx)
            {
                Assert.That(webEx.StatusCode, Is.EqualTo(405));
                Assert.That(webEx.StatusDescription, Is.EqualTo("Method Not Allowed"));
            }
        }

[Test, TestCaseSource("RestClients")]
        public void Can_GET_SearchReqstars_PrettyRestApi(IRestClient client)
        {
            var request = new SearchReqstars();
            var response = client.Get(request);

            Assert.That(request.ToUrl("GET"), Is.EqualTo("/reqstars/search"));
            Assert.That(response.Results.Count, Is.EqualTo(ReqstarsService.SeedData.Length));
        }

[Test, TestCaseSource("RestClients")]
        public void Invalid_GET_SearchReqstars_throws_typed_Response_PrettyRestApi(IRestClient client)
        {
            try
            {
                var response = client.Get(new SearchReqstars { Age = -1 });
                Assert.Fail("POST's to SearchReqstars should not be allowed");
            }
            catch (WebServiceException webEx)
            {
                Assert.That(webEx.StatusCode, Is.EqualTo(400));
                Assert.That(webEx.StatusDescription, Is.EqualTo("ArgumentException"));

                Assert.That(webEx.ResponseStatus.ErrorCode, Is.EqualTo("ArgumentException"));
                Assert.That(webEx.ResponseStatus.Message, Is.EqualTo("Invalid Age"));

                Assert.That(webEx.ResponseDto as ReqstarsResponse, Is.Not.Null);
            }
        }


[Test, TestCaseSource("RestClients")]
        public void Can_GET_SearchReqstars_aged_20(IRestClient client)
        {
            var response = client.Get<ReqstarsResponse>("/reqstars/aged/20");
            Assert.That(response.Results.Count,
                Is.EqualTo(ReqstarsService.SeedData.Count(x => x.Age == 20)));
        }

[Test, TestCaseSource("ServiceClients")]
        public void Disallows_SEND_SearchReqstars_aged_20_PrettyTypedApi(IServiceClient client)
        {
            try
            {
                var response = client.Send(new SearchReqstars { Age = 20 });
                Assert.Fail("POST's to SearchReqstars should not be allowed");
            }
            catch (WebServiceException webEx)
            {
                Assert.That(webEx.StatusCode, Is.EqualTo(405));
                Assert.That(webEx.StatusDescription, Is.EqualTo("Method Not Allowed"));
            }
        }

[Test, TestCaseSource("RestClients")]
        public void Can_GET_SearchReqstars_aged_20_PrettyRestApi(IRestClient client)
        {
            var request = new SearchReqstars { Age = 20 };
            var response = client.Get(request);

            Assert.That(request.ToUrl("GET"), Is.EqualTo("/reqstars/aged/20"));
            Assert.That(response.Results.Count,
                Is.EqualTo(ReqstarsService.SeedData.Count(x => x.Age == 20)));
        }


[Test, TestCaseSource("RestClients")]
        public void Can_GET_GetReqstar(IRestClient client)
        {
            var response = client.Get<Reqstar>("/reqstars/1");
            Assert.That(response.FirstName,
                Is.EqualTo(ReqstarsService.SeedData[0].FirstName));
        }

[Test, TestCaseSource("RestClients")]
        public void Can_GET_GetReqstar_PrettyRestApi(IRestClient client)
        {
            var request = new GetReqstar { Id = 1 };
            var response = client.Get(request);

            Assert.That(request.ToUrl("GET"), Is.EqualTo("/reqstars/1"));
            Assert.That(response.FirstName,
                Is.EqualTo(ReqstarsService.SeedData[0].FirstName));
        }

[Test, TestCaseSource("ServiceClients")]
        public void Disallows_SEND_GetReqstar_PrettyTypedApi(IServiceClient client)
        {
            try
            {
                var response = client.Send(new GetReqstar { Id = 1 });
                Assert.Fail("POST's to GetReqstar should not be allowed");
            }
            catch (WebServiceException webEx)
            {
                Assert.That(webEx.StatusCode, Is.EqualTo(405));
                Assert.That(webEx.StatusDescription, Is.EqualTo("Method Not Allowed"));
            }
        }

[Test]
        public void Can_GET_GetReqstar_View()
        {
            var html = "{0}/reqstars/1".Fmt(Host).GetStringFromUrl(accept: "text/html");
            html.Print();
            Assert.That(html, Is.StringContaining("<!--view:GetReqstar.cshtml-->"));
            Assert.That(html, Is.StringContaining("<!--template:HtmlReport.cshtml-->"));
        }


        public class EmptyResponse { }

[Test, TestCaseSource("RestClients")]
        public void Can_DELETE_Reqstar(IRestClient client)
        {
            var response = client.Delete<EmptyResponse>("/reqstars/1/delete");

            var reqstarsLeft = db.Select<Reqstar>();

            Assert.That(reqstarsLeft.Count,
                Is.EqualTo(ReqstarsService.SeedData.Length - 1));
        }

[Test, TestCaseSource("ServiceClients")]
        public void Can_DELETE_Reqstar_PrettyTypedApi(IServiceClient client)
        {
            client.Send(new DeleteReqstar { Id = 1 });

            var reqstarsLeft = db.Select<Reqstar>();

            Assert.That(reqstarsLeft.Count,
                Is.EqualTo(ReqstarsService.SeedData.Length - 1));
        }

[Test, TestCaseSource("RestClients")]
        public void Can_DELETE_Reqstar_PrettyRestApi(IRestClient client)
        {
            client.Delete(new DeleteReqstar { Id = 1 });

            var reqstarsLeft = db.Select<Reqstar>();

            Assert.That(reqstarsLeft.Count,
                Is.EqualTo(ReqstarsService.SeedData.Length - 1));
        }


[Test, TestCaseSource("RestClients")]
        public void Can_PATCH_UpdateReqstar(IRestClient client)
        {
            var response = client.Patch<Reqstar>("/reqstars/1", new UpdateReqstar(1, 18));
            Assert.That(response.Age, Is.EqualTo(18));
        }

[Test, TestCaseSource("RestClients")]
        public void Can_PATCH_UpdateReqstar_PrettyRestApi(IRestClient client)
        {
            var response = client.Patch(new UpdateReqstar(1, 18));
            Assert.That(response.Age, Is.EqualTo(18));
        }


[Test, TestCaseSource("RestClients")]
        public void Can_CREATE_Reqstar(IRestClient client)
        {
            var response = client.Post<List<Reqstar>>("/reqstars", new Reqstar(4, "Just", "Created", 25));

            Assert.That(response.Count,
                Is.EqualTo(ReqstarsService.SeedData.Length + 1));
        }

[Test, TestCaseSource("ServiceClients")]
        public void Can_CREATE_Reqstar_PrettyTypedApi(IServiceClient client)
        {
            var response = client.Send(new Reqstar(4, "Just", "Created", 25));

            Assert.That(response.Count,
                Is.EqualTo(ReqstarsService.SeedData.Length + 1));
        }

[Test, TestCaseSource("RestClients")]
        public void Can_CREATE_Reqstar_PrettyRestApi(IRestClient client)
        {
            var response = client.Post(new Reqstar(4, "Just", "Created", 25));

            Assert.That(response.Count,
                Is.EqualTo(ReqstarsService.SeedData.Length + 1));
        }

[Test, TestCaseSource("RestClients")]
        public void Fails_to_CREATE_Empty_Reqstar_PrettyRestApi(IRestClient client)
        {
            try
            {
                var response = client.Post(new Reqstar());
                Assert.Fail("Should've thrown 400 Bad Request Error");
            }
            catch (WebServiceException webEx)
            {
                Assert.That(webEx.StatusCode, Is.EqualTo(400));
                Assert.That(webEx.StatusDescription, Is.EqualTo("ArgumentException"));

                Assert.That(webEx.ResponseStatus.ErrorCode, Is.EqualTo("ArgumentException"));
                Assert.That(webEx.ResponseStatus.Message, Is.EqualTo("Age is required"));

                Assert.That(webEx.ResponseDto as ErrorResponse, Is.Not.Null);
            }
        }

[Test, TestCaseSource("RestClients")]
        public void Can_GET_ResetReqstars(IRestClient client)
        {
            db.DeleteAll<Reqstar>();

            var response = client.Get<EmptyResponse>("/reqstars/reset");

            var reqstarsLeft = db.Select<Reqstar>();

            Assert.That(reqstarsLeft.Count, Is.EqualTo(ReqstarsService.SeedData.Length));
        }

[Test, TestCaseSource("ServiceClients")]
        public void Can_SEND_ResetReqstars_PrettyTypedApi(IServiceClient client)
        {
            db.DeleteAll<Reqstar>();

            client.Send(new ResetReqstar());

            var reqstarsLeft = db.Select<Reqstar>();

            Assert.That(reqstarsLeft.Count, Is.EqualTo(ReqstarsService.SeedData.Length));
        }

[Test, TestCaseSource("RestClients")]
        public void Can_GET_ResetReqstars_PrettyRestApi(IRestClient client)
        {
            db.DeleteAll<Reqstar>();

            client.Get(new ResetReqstar());

            var reqstarsLeft = db.Select<Reqstar>();

            Assert.That(reqstarsLeft.Count, Is.EqualTo(ReqstarsService.SeedData.Length));
        }

[Test, TestCaseSource("RestClients")]
        public void Can_GET_RoutelessReqstar_PrettyRestApi(IRestClient client)
        {
            var request = new RoutelessReqstar {
                Id = 1,
                FirstName = "Foo",
                LastName = "Bar",
            };

            var response = client.Get(request);

            var format = ((ServiceClientBase)client).Format;
            Assert.That(request.ToUrl("GET", format), Is.EqualTo(
                "/{0}/reply/RoutelessReqstar?id=1&firstName=Foo&lastName=Bar".Fmt(format)));
            Assert.That(response.Id, Is.EqualTo(request.Id));
            Assert.That(response.FirstName, Is.EqualTo(request.FirstName));
            Assert.That(response.LastName, Is.EqualTo(request.LastName));
        }

[Test, TestCaseSource("RestClients")]
        public void Can_POST_RoutelessReqstar_PrettyRestApi(IRestClient client)
        {
            var request = new RoutelessReqstar {
                Id = 1,
                FirstName = "Foo",
                LastName = "Bar",
            };

            var response = client.Post(request);

            var format = ((ServiceClientBase)client).Format;
            Assert.That(request.ToUrl("POST", format), Is.EqualTo(
                "/{0}/reply/RoutelessReqstar".Fmt(format)));
            Assert.That(response.Id, Is.EqualTo(request.Id));
            Assert.That(response.FirstName, Is.EqualTo(request.FirstName));
            Assert.That(response.LastName, Is.EqualTo(request.LastName));
        }

[Test, TestCaseSource("RestClients")]
        public void Can_GET_RichRequest_PrettyRestApi(IRestClient client)
        {
            var request = new RichRequest {
                StringArray = new[] { "a", "b", "c" },
                StringList = new List<string> { "d", "e", "f" },
                StringSet = new HashSet<string> { "g", "h", "i" },
            };

            Assert.That(request.ToUrl("GET"), Is.EqualTo("/richrequest?stringList=d,e,f&stringSet=g,h,i&stringArray=a,b,c".Replace(",","%2C")));

            var response = client.Get(request);

            Assert.That(response.StringArray, Is.EquivalentTo(request.StringArray));
            Assert.That(response.StringList, Is.EquivalentTo(request.StringList));
            Assert.That(response.StringSet, Is.EquivalentTo(request.StringSet));
        }

[Test]
        public void Does_Cache_RazorPage()
        {
            var html = "{0}/reqstars/cached/10".Fmt(Host).GetStringFromUrl();
            Assert.That(html, Is.StringContaining("<h1>Counter:10</h1>"));
            html = "{0}/reqstars/cached/20".Fmt(Host).GetStringFromUrl();
            Assert.That(html, Is.StringContaining("<h1>Counter:10</h1>"));
        }

[Test]
        public void Does_ignore_all_types_of_routes()
        {
            var response1 = "{0}/ignore/AnyThing?Name=foo".Fmt(Host).GetJsonFromUrl().FromJson<IgnoreRoute1>();
            Assert.That(response1.Name, Is.EqualTo("foo"));
            
            var response2 = "{0}/ignore/AnyThing/between?Name=foo".Fmt(Host).GetJsonFromUrl().FromJson<IgnoreRoute2>();
            Assert.That(response2.Name, Is.EqualTo("foo"));

            var response3 = "{0}/ignore/AnyThing/and/everything?Name=foo".Fmt(Host).GetJsonFromUrl().FromJson<IgnoreRoute3>();
            Assert.That(response3.Name, Is.EqualTo("foo"));
            response3 = "{0}/ignore/AnyThing/with/foo".Fmt(Host).GetJsonFromUrl().FromJson<IgnoreRoute3>();
            Assert.That(response3.Name, Is.EqualTo("foo"));
            
            var response4 = "{0}/ignorewildcard?Name=foo".Fmt(Host).GetJsonFromUrl().FromJson<IgnoreWildcardRoute>();
            Assert.That(response4.Name, Is.EqualTo("foo"));
            response4 = "{0}/ignorewildcard/a?Name=foo".Fmt(Host).GetJsonFromUrl().FromJson<IgnoreWildcardRoute>();
            Assert.That(response4.Name, Is.EqualTo("foo"));
            response4 = "{0}/ignorewildcard/a/b?Name=foo".Fmt(Host).GetJsonFromUrl().FromJson<IgnoreWildcardRoute>();
            Assert.That(response4.Name, Is.EqualTo("foo"));
        }

[Test]
        public void Does_handle_ignored_routes()
        {
            var restPath = new RestPath(typeof(IgnoreRoute3), "/ignore/{ignore}/with/{name}");
            var pathComponents = RestPath.GetPathPartsForMatching("/ignore/AnyThing/with/foo");
            var score = restPath.MatchScore("GET", pathComponents);

            Assert.That(score, Is.GreaterThan(0));

            var request = (IgnoreRoute3) restPath.CreateRequest("/ignore/AnyThing/with/foo");
            Assert.That(request.Name, Is.EqualTo("foo"));
        }
    }
    
}
Something went wrong with that request. Please try again.