Example #1
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));
        }
Example #2
0
        public void ExecuteWithRequestAndMappedParameters()
        {
            var request = Request.Has(Method.Get)
                          .And("/posts/my-post/comments/my-comment/users/my-user".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", "my-user"),
            });
            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))
                          .Handle((postId, commentId, userId, page)
                                  => Completes.WithSuccess(Response.Of(ResponseStatus.Ok, JsonSerialization.Serialized(
                                                                           $"{postId} {commentId} {userId}"))));

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

            AssertResponsesAreEquals(
                Response.Of(ResponseStatus.Ok, JsonSerialization.Serialized("my-post my-comment my-user")),
                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);
    }
Example #4
0
 public RequestHandler2 <T, R> Query <R>(string name, R defaultValue)
 => new RequestHandler2 <T, R>(
     Method,
     Path,
     Resolver,
     ParameterResolver.Query(name, defaultValue),
     ErrorHandler,
     MediaTypeMapper);
Example #5
0
        public void QueryShouldReturnDefaultWhenItIsNotPresent()
        {
            var resolver = ParameterResolver.Query <int>("pageSize", 50);

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

            Assert.Equal(50, result);
            Assert.Equal(ParameterResolver.Type.Query, resolver.Type);
        }
Example #6
0
        public void QueryWithType()
        {
            var resolver = ParameterResolver.Query <int>("page");

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

            Assert.Equal(10, result);
            Assert.Equal(ParameterResolver.Type.Query, resolver.Type);
        }
Example #7
0
        public void Query()
        {
            var resolver = ParameterResolver.Query <string>("page");

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

            Assert.Equal("10", result);
            Assert.Equal(ParameterResolver.Type.Query, resolver.Type);
        }
 public RequestHandler4 <T, TR, TU, TI> Query <TI>(string name, Type queryClass)
 => new RequestHandler4 <T, TR, TU, TI>(
     Method,
     Path,
     ResolverParam1,
     ResolverParam2,
     ResolverParam3,
     ParameterResolver.Query <TI>(name),
     ErrorHandler,
     MediaTypeMapper);
        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);
        }
Example #10
0
 public RequestHandler6 <T, TR, TU, TI, TJ, TK> Query <TK>(string name) =>
 new RequestHandler6 <T, TR, TU, TI, TJ, TK>(
     Method,
     Path,
     ResolverParam1,
     ResolverParam2,
     ResolverParam3,
     ResolverParam4,
     ResolverParam5,
     ParameterResolver.Query <TK>(name),
     ErrorHandler,
     MediaTypeMapper);
        public void AddingHandlerQuery()
        {
            var request = Request.Has(Method.Get)
                          .And("/user?filter=abc".ToMatchableUri())
                          .And(Version.Http1_1);
            var mappedParameters =
                new Action.MappedParameters(1, Method.Get, "ignored", new List <Action.MappedParameter>());

            var handler = new RequestHandler0(Method.Get, "/user").Query("filter");

            AssertResolvesAreEquals(ParameterResolver.Query <string>("filter"), handler.Resolver);
            Assert.Equal("abc", 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);
        }
Example #13
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));
        }
Example #14
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));
        }
Example #15
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));
        }