示例#1
0
        public void AddingHandlerParam()
        {
            var request = Request.Has(Method.Get)
                          .And("/posts/my-post/comment/my-comment/votes/10/user/admin".ToMatchableUri())
                          .And(Version.Http1_1);
            var mappedParameters =
                new Action.MappedParameters(1, Method.Get, "ignored", new List <Action.MappedParameter>
            {
                new Action.MappedParameter("String", "my-post"),
                new Action.MappedParameter("String", "my-comment"),
                new Action.MappedParameter("String", 10),
                new Action.MappedParameter("String", "admin"),
                new Action.MappedParameter("String", "justanother")
            });

            var
                handler = CreateRequestHandler(
                Method.Get,
                "/posts/{postId}/comment/{commentId}/votes/{votesNumber}/user/{userId}/{another}",
                ParameterResolver.Path <string>(0),
                ParameterResolver.Path <string>(1),
                ParameterResolver.Path <int>(2),
                ParameterResolver.Path <string>(3)
                )
                          .Param <string>();

            AssertResolvesAreEquals(ParameterResolver.Path <string>(4), handler.ResolverParam5);
            Assert.Equal("justanother", handler.ResolverParam5.Apply(request, mappedParameters));
        }
示例#2
0
        public void AddingHandlerBodyWithMapper()
        {
            var request = Request.Has(Method.Post)
                          .And("/posts/my-post/comment/my-comment".ToMatchableUri())
                          .And(Body.From("{\"given\":\"John\",\"family\":\"Doe\"}"))
                          .And(Version.Http1_1);
            var mappedParameters =
                new Action.MappedParameters(1, Method.Post, "ignored", new List <Action.MappedParameter>
            {
                new Action.MappedParameter("String", "my-post"),
                new Action.MappedParameter("String", "my-comment")
            });

#pragma warning disable 618
            var handler = CreateRequestHandler(
                Method.Post,
                "/posts/{postId}/comment/{commentId}/votes/{votesNumber}",
                ParameterResolver.Path <string>(0),
                ParameterResolver.Path <string>(1),
                ParameterResolver.Path <int>(2),
                ParameterResolver.Query("page", 10)
                )
                          .Body <NameData>(typeof(TestMapper));
#pragma warning restore 618

            AssertResolvesAreEquals(ParameterResolver.Body <NameData>(), handler.ResolverParam5);
            Assert.Equal(new NameData("John", "Doe"), handler.ResolverParam5.Apply(request, mappedParameters));
        }
示例#3
0
        public void ExecuteWithRequestAndMappedParameters()
        {
            var request = Request.Has(Method.Get)
                          .And("/posts/my-post/comments/my-comment".ToMatchableUri())
                          .And(Version.Http1_1);
            var mappedParameters =
                new Action.MappedParameters(1, Method.Get, "ignored", new List <Action.MappedParameter>
            {
                new Action.MappedParameter("string", "my-post"),
                new Action.MappedParameter("string", "my-comment"),
            });
            var handler = CreateRequestHandler(
                Method.Get,
                "/posts/{postId}",
                ParameterResolver.Path <string>(0))
                          .Handle(postId
                                  => Completes.WithSuccess(Response.Of(Response.ResponseStatus.Ok, JsonSerialization.Serialized(
                                                                           $"{postId}"))));

            var response = handler.Execute(request, mappedParameters, Logger).Outcome;

            AssertResponsesAreEquals(
                Response.Of(Response.ResponseStatus.Ok, JsonSerialization.Serialized("my-post")),
                response);
        }
    public void HandlerWithOneParam()
    {
        var handler = CreateRequestHandler(
            Method.Get,
            "/posts/{postId}/comment/{commentId}/user/{userId}/contact/{contactId}",
            ParameterResolver.Path <string>(0),
            ParameterResolver.Path <string>(1),
            ParameterResolver.Path <string>(2),
            ParameterResolver.Path <string>(3),
            ParameterResolver.Query("page", 10),
            ParameterResolver.Query("pageSize", 10))
                      .Handle((postId, commentId, userId, contactId, page, pageSize) =>
                              Completes.WithSuccess(Response.Of(ResponseStatus.Ok, JsonSerialization.Serialized(
                                                                    $"{postId} {commentId}"))));

        var response = handler
                       .Execute(Request.WithMethod(Method.Get), "my-post", "my-comment", "admin", "c1", 10, 10, Logger).Outcome;

        Assert.NotNull(handler);
        Assert.Equal(Method.Get, handler.Method);
        Assert.Equal("/posts/{postId}/comment/{commentId}/user/{userId}/contact/{contactId}", handler.Path);
        Assert.Equal(typeof(string), handler.ResolverParam1.ParamClass);
        Assert.Equal(typeof(string), handler.ResolverParam2.ParamClass);
        Assert.Equal(typeof(string), handler.ResolverParam3.ParamClass);
        Assert.Equal(typeof(string), handler.ResolverParam4.ParamClass);
        AssertResponsesAreEquals(Response.Of(ResponseStatus.Ok, JsonSerialization.Serialized("my-post my-comment")), response);
    }
示例#5
0
        public void ActionSignature()
        {
            var handler = CreateRequestHandler(
                Method.Get,
                "/posts/{postId}",
                ParameterResolver.Path <string>(0));

            Assert.Equal("String postId", handler.ActionSignature);
        }
示例#6
0
        public void Path()
        {
            var resolver = ParameterResolver.Path <string>(0);

            var result = resolver.Apply(_request, _mappedParameters);

            Assert.Equal("my-post", result);
            Assert.Equal(ParameterResolver.Type.Path, resolver.Type);
        }
 public RequestHandler4 <T, TR, TU, TI> Param <TI>()
 => new RequestHandler4 <T, TR, TU, TI>(
     Method,
     Path,
     ResolverParam1,
     ResolverParam2,
     ResolverParam3,
     ParameterResolver.Path <TI>(3),
     ErrorHandler,
     MediaTypeMapper);
 public RequestHandler5 <T, TR, TU, TI, TJ> Param <TJ>()
 => new RequestHandler5 <T, TR, TU, TI, TJ>(
     Method,
     Path,
     ResolverParam1,
     ResolverParam2,
     ResolverParam3,
     ResolverParam4,
     ParameterResolver.Path <TJ>(4),
     ErrorHandler,
     MediaTypeMapper);
    public void GenerateActionWithTwoPathParameters()
    {
        var handler = new RequestHandlerFake(
            Method.Get,
            "/user/{userId}/comment/{commentId}",
            new List <IParameterResolver> {
            ParameterResolver.Path <string>(0), ParameterResolver.Path <int>(0)
        });

        Assert.Equal("String userId, Int32 commentId", handler.ActionSignature);
    }
    public void GenerateActionSignatureWithOnePathParameterReturnsSignatureWithOneParam()
    {
        var handler = new RequestHandlerFake(
            Method.Get,
            "/user/{userId}",
            new List <IParameterResolver> {
            ParameterResolver.Path <string>(0)
        });

        Assert.Equal("String userId", handler.ActionSignature);
    }
    public void GenerateActionWithOnePathParameterAndBodyJustReturnPathParameterSignature()
    {
        var handler = new RequestHandlerFake(
            Method.Get,
            "/user/{userId}",
            new List <IParameterResolver> {
            ParameterResolver.Path <string>(0), ParameterResolver.Body <NameData>()
        });

        Assert.Equal("String userId", handler.ActionSignature);
    }
        public void ActionSignature()
        {
            var handler = CreateRequestHandler(
                Method.Get,
                "/posts/{postId}/comment/{commentId}/user/{userId}",
                ParameterResolver.Path <string>(0),
                ParameterResolver.Path <string>(1),
                ParameterResolver.Query("page", 10));

            Assert.Equal("String postId, String commentId", handler.ActionSignature);
        }
示例#13
0
        public void ThrowExceptionWhenNoHandlerIsDefined()
        {
            var handler = CreateRequestHandler(
                Method.Get,
                "/posts/{postId}",
                ParameterResolver.Path <string>(0));

            var exception = Assert.Throws <HandlerMissingException>(() => handler.Execute(Request.WithMethod(Method.Get),
                                                                                          "my-post", Logger));

            Assert.Equal("No handler defined for GET /posts/{postId}", exception.Message);
        }
 public void UnsortedPathParametersThrowsException()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         return(new RequestHandlerFake(
                    Method.Get,
                    "/user/{userId}",
                    new List <IParameterResolver>
         {
             ParameterResolver.Body <NameData>(), ParameterResolver.Path <string>(0)
         }));
     });
 }
        public void AddingHandlerParam()
        {
            var request = Request.Has(Method.Get)
                          .And("/user/admin".ToMatchableUri())
                          .And(Version.Http1_1);
            var mappedParameters =
                new Action.MappedParameters(1, Method.Get, "ignored", new List <Action.MappedParameter>
            {
                new Action.MappedParameter("String", "admin")
            });

            var handler = new RequestHandler0(Method.Get, "/user/{userId}").Param <string>();

            AssertResolvesAreEquals(ParameterResolver.Path <string>(0), handler.Resolver);
            Assert.Equal("admin", handler.Resolver.Apply(request, mappedParameters));
        }
        public void ThrowExceptionWhenNoHandlerIsDefined()
        {
            var handler = CreateRequestHandler(
                Method.Get,
                "/posts/{postId}/comment/{commentId}/user/{userId}",
                ParameterResolver.Path <string>(0),
                ParameterResolver.Path <string>(1),
                ParameterResolver.Path <string>(2),
                ParameterResolver.Query("page", 10),
                ParameterResolver.Query("pageSize", 10));

            var exception = Assert.Throws <HandlerMissingException>(() => handler.Execute(Request.WithMethod(Method.Get), "my-post",
                                                                                          "my-comment", "admin", 10, 10, Logger));

            Assert.Equal("No handler defined for GET /posts/{postId}/comment/{commentId}/user/{userId}", exception.Message);
        }
示例#17
0
        public void ExecuteWithRequestAndMappedParametersWithWrongSignatureType()
        {
            var request = Request.Has(Method.Get)
                          .And("/posts/my-post".ToMatchableUri())
                          .And(Version.Http1_1);
            var mappedParameters =
                new Action.MappedParameters(1, Method.Get, "ignored", new List <Action.MappedParameter>
            {
                new Action.MappedParameter("String", "my-post")
            });
            var handler = CreateRequestHandler(Method.Get, "/posts/{postId}", ParameterResolver.Path <int>(0))
                          .Handle(postId => Completes.WithSuccess(Response.Of(Response.ResponseStatus.Ok, JsonSerialization.Serialized("it is my-post"))));

            var exception = Assert.Throws <ArgumentException>(() => handler.Execute(request, mappedParameters, Logger));

            Assert.Equal("Value my-post is of mimeType String instead of Int32", exception.Message);
        }
示例#18
0
        public void AddingHandlerQuery()
        {
            var request = Request.Has(Method.Post)
                          .And("/user/admin?filter=abc".ToMatchableUri())
                          .And(Version.Http1_1);
            var mappedParameters =
                new Action.MappedParameters(1, Method.Get, "ignored",
                                            Enumerable.Empty <Action.MappedParameter>().ToList());

            var handler = CreateRequestHandler(
                Method.Get,
                "/user/{userId}",
                ParameterResolver.Path <string>(0))
                          .Query("filter");

            AssertResolvesAreEquals(ParameterResolver.Query <string>("filter"), handler.ResolverParam2);
            Assert.Equal("abc", handler.ResolverParam2.Apply(request, mappedParameters));
        }
示例#19
0
        public void HandlerWithOneParam()
        {
            var handler = CreateRequestHandler(
                Method.Get,
                "/posts/{postId}",
                ParameterResolver.Path <string>(0))
                          .Handle(postId =>
                                  Completes.WithSuccess(Response.Of(Response.ResponseStatus.Ok, JsonSerialization.Serialized(
                                                                        $"{postId}"))));

            var response = handler
                           .Execute(Request.WithMethod(Method.Get), "my-post", Logger).Outcome;

            Assert.NotNull(handler);
            Assert.Equal(Method.Get, handler.Method);
            Assert.Equal("/posts/{postId}", handler.Path);
            Assert.Equal(typeof(string), handler.Resolver.ParamClass);
            AssertResponsesAreEquals(
                Response.Of(Response.ResponseStatus.Ok, JsonSerialization.Serialized("my-post")), response);
        }
示例#20
0
        public void AddingHandlerHeader()
        {
            var hostHeader = RequestHeader.Of("Host", "www.vlingo.io");
            var request    = Request.Has(Method.Get)
                             .And("/user/admin".ToMatchableUri())
                             .And(Headers.Of(hostHeader))
                             .And(Version.Http1_1);
            var mappedParameters =
                new Action.MappedParameters(1, Method.Get, "ignored",
                                            Enumerable.Empty <Action.MappedParameter>().ToList());

            var handler = CreateRequestHandler(
                Method.Get,
                "/user/{userId}",
                ParameterResolver.Path <string>(0))
                          .Header("Host");

            AssertResolvesAreEquals(ParameterResolver.Header("Host"), handler.ResolverParam2);
            Assert.Equal(hostHeader, handler.ResolverParam2.Apply(request, mappedParameters));
        }
示例#21
0
        public void AddingHandlerQuery()
        {
            var request = Request.Has(Method.Post)
                          .And("/posts/my-post/comment/my-comment?filter=abc".ToMatchableUri())
                          .And(Version.Http1_1);
            var mappedParameters =
                new Action.MappedParameters(1, Method.Get, "ignored", Enumerable.Empty <Action.MappedParameter>().ToList());

            var handler = CreateRequestHandler(
                Method.Get,
                "/posts/{postId}/comment/{commentId}/votes/{votesId}",
                ParameterResolver.Path <string>(0),
                ParameterResolver.Path <string>(1),
                ParameterResolver.Path <string>(2),
                ParameterResolver.Query("page", 10)
                )
                          .Query("filter");

            AssertResolvesAreEquals(ParameterResolver.Query <string>("filter"), handler.ResolverParam5);
            Assert.Equal("abc", handler.ResolverParam5.Apply(request, mappedParameters));
        }
示例#22
0
        public void AddingHandlerParam()
        {
            var request = Request.Has(Method.Get)
                          .And("/user/admin/picture/2".ToMatchableUri())
                          .And(Version.Http1_1);
            var mappedParameters =
                new Action.MappedParameters(1, Method.Get, "ignored", new List <Action.MappedParameter>
            {
                new Action.MappedParameter("String", "admin"),
                new Action.MappedParameter("int", 1)
            });

            var handler = CreateRequestHandler(
                Method.Get,
                "/user/{userId}/picture/{pictureId}",
                ParameterResolver.Path <string>(0))
                          .Param <int>();

            AssertResolvesAreEquals(ParameterResolver.Path <int>(1), handler.ResolverParam2);
            Assert.Equal(1, handler.ResolverParam2.Apply(request, mappedParameters));
        }
示例#23
0
        public void AddingHandlerBody()
        {
            var request = Request.Has(Method.Post)
                          .And("/user/admin/name".ToMatchableUri())
                          .And(Body.From("{\"given\":\"John\",\"family\":\"Doe\"}"))
                          .And(Version.Http1_1);
            var mappedParameters =
                new Action.MappedParameters(1, Method.Post, "ignored", new List <Action.MappedParameter>
            {
                new Action.MappedParameter("String", "admin")
            });

            var handler = CreateRequestHandler(
                Method.Post,
                "/user/{userId}/picture/{pictureId}",
                ParameterResolver.Path <string>(0))
                          .Body <NameData>();

            AssertResolvesAreEquals(ParameterResolver.Body <NameData>(), handler.ResolverParam2);
            Assert.Equal(new NameData("John", "Doe"), handler.ResolverParam2.Apply(request, mappedParameters));
        }
        public void AddingHandlerBody()
        {
            var request = Request.Has(Method.Post)
                          .And("/posts/my-post/comment/my-comment".ToMatchableUri())
                          .And(Body.From("{\"given\":\"John\",\"family\":\"Doe\"}"))
                          .And(Version.Http1_1);
            var mappedParameters =
                new Action.MappedParameters(1, Method.Post, "ignored", new List <Action.MappedParameter>
            {
                new Action.MappedParameter("String", "my-post"),
                new Action.MappedParameter("String", "my-comment")
            });

            var handler = CreateRequestHandler(
                Method.Post,
                "/posts/{postId}/comment/{commentId}",
                ParameterResolver.Path <string>(0),
                ParameterResolver.Path <string>(1))
                          .Body <NameData>();

            AssertResolvesAreEquals(ParameterResolver.Body <NameData>(), handler.ResolverParam3);
            Assert.Equal(new NameData("John", "Doe"), handler.ResolverParam3.Apply(request, mappedParameters));
        }
示例#25
0
        public void AddingHandlerHeader()
        {
            var hostHeader = RequestHeader.Of("Host", "www.vlingo.io");
            var request    = Request.Has(Method.Get)
                             .And("/posts/my-post/comment/my-comment".ToMatchableUri())
                             .And(Headers.Of(hostHeader))
                             .And(Version.Http1_1);
            var mappedParameters =
                new Action.MappedParameters(1, Method.Get, "ignored", Enumerable.Empty <Action.MappedParameter>().ToList());

            var handler = CreateRequestHandler(
                Method.Get,
                "/posts/{postId}/comment/{commentId}/votes/{votesNumber}",
                ParameterResolver.Path <string>(0),
                ParameterResolver.Path <string>(1),
                ParameterResolver.Path <string>(2),
                ParameterResolver.Query("page", 10)
                )
                          .Header("Host");

            AssertResolvesAreEquals(ParameterResolver.Header("Host"), handler.ResolverParam5);
            Assert.Equal(hostHeader, handler.ResolverParam5.Apply(request, mappedParameters));
        }
示例#26
0
 public RequestHandler1 <T> Param <T>()
 => new RequestHandler1 <T>(Method, Path, ParameterResolver.Path <T>(0), ErrorHandler, MediaTypeMapper);
示例#27
0
 public RequestHandler3 <T, TR, TU> Param <TU>()
 => new RequestHandler3 <T, TR, TU>(Method, Path, ResolverParam1, ResolverParam2, ParameterResolver.Path <TU>(2), ErrorHandler, MediaTypeMapper);
示例#28
0
 public RequestHandler7 <T, TR, TU, TI, TJ, TK, TL> Param <TL>() =>
 new RequestHandler7 <T, TR, TU, TI, TJ, TK, TL>(Method, Path, ResolverParam1, ResolverParam2, ResolverParam3, ResolverParam4, ResolverParam5, ResolverParam6,
                                                 ParameterResolver.Path <TL>(6),
                                                 ErrorHandler,
                                                 MediaTypeMapper);
示例#29
0
        public void ActionSignatureWithBlankParamNameThrowsException()
        {
            var exception = Assert.Throws <ArgumentException>(() => CreateRequestHandler(Method.Get, "/posts/{ }", ParameterResolver.Path <string>(0))
                                                              .Handle(postId => Completes.WithSuccess(Response.Of(Response.ResponseStatus.Ok, JsonSerialization.Serialized(postId)))));

            Assert.Equal("Empty path parameter name for GET /posts/{ }", exception.Message);
        }
示例#30
0
 public RequestHandler2 <T, R> Param <R>()
 => new RequestHandler2 <T, R>(Method, Path, Resolver, ParameterResolver.Path <R>(1), ErrorHandler, MediaTypeMapper);