Ejemplo n.º 1
0
        public async Task OnSending_CanUseSmallerType2()
        {
            //arrange
            var sendingCalled            = false;
            var sendingWithContentCalled = false;
            var sendingWithResultCalled  = false;

            var builder =
                CreateBuilder()
                .VerifyOnSending <IHalResource, IHalRequest>(ctx => { })
                .VerifyOnSendingWithContent <IHalRequest>(ctx => { })
                .VerifyOnSendingWithResult <IHalResource>(ctx => { })
                .WithResult(TestResource.Default1())
                .WithLink(MockUri)
                .WithContent(TestRequest.Default1())
                .Advanced.OnSending <IHalResource, IHalRequest>(ctx => sendingCalled = true)
                .OnSendingWithContent <IHalRequest>(ctx => sendingWithContentCalled  = true)
                .OnSendingWithResult <IHalResource>(ctx => sendingWithResultCalled   = true);

            //act
            var actual1 = await builder.ResultAsync <TestResource>();

            sendingCalled.ShouldBeTrue("Sending was not called");
            sendingWithContentCalled.ShouldBeTrue("SendingWithContent was not called");
            sendingWithResultCalled.ShouldBeTrue("SendingWithResult was not called");
        }
Ejemplo n.º 2
0
        public async Task WhenContentAndSendingHandlerTypesMismatchAndSuppressTypeException_ExpectResult()
        {
            var builder = CreateBuilder().WithResult(TestResource.Default1()).WithLink(MockUri);

            //act
            var actual = await builder.WithContent(TestRequest.Default1())
                         .AsPost()
                         .Advanced
                         .WithSuppressTypeMismatchExceptions().OnSendingWithContent <AlternateTestRequest>(ctx => { }).ResultAsync <TestResource>();

            actual.ShouldNotBeNull();
        }
Ejemplo n.º 3
0
        public async Task WhenContentAndSendingHandlerTypesMismatch_ExpectException()
        {
            var builder = CreateBuilder()
                          .VerifyOnSendingWithContent <AlternateTestRequest>(ctx =>
            {
                var content = ctx.Content;
            })
                          .WithLink(MockUri)
                          .WithContent(TestRequest.Default1())
                          .AsPost();

            //act
            await Should.ThrowAsync <TypeMismatchException>(async() =>
                                                            await builder.ResultAsync <TestResource>());
        }
Ejemplo n.º 4
0
        public async Task WhenSentHandlerIsObjectType_ExpectSuccess()
        {
            var builder = CreateBuilder().WithResult(TestResource.Default1()).WithLink(MockUri);

            //act
            var called = false;
            var result = await builder.WithContent(TestRequest.Default1())
                         .AsPost()
                         .Advanced
                         .OnSent <IHalResource>(ctx => { called = true; })
                         .ResultAsync <TestResource>();

            result.ShouldNotBeNull();
            called.ShouldBeTrue();
        }
Ejemplo n.º 5
0
        public async Task ErrorMessageWithMaxRedirectLooksPretty()
        {
            try
            {
                using (var server = LocalWebServer.ListenInBackground(new XUnitMockLogger(_logger)))
                {
                    server.WithAllResponses(new MockHttpResponseMessage(HttpStatusCode.Redirect).WithHeader("Location", server.ListeningUri.ToString()));

                    await new TypedBuilderFactory().Create().WithUri(server.ListeningUri).AsPost().WithContent(TestRequest.Default1())
                    .WithErrorType <TestError>()
                    //.Advanced
                    //.WithErrorFactory((context, message, arg3, arg4) => Task.FromResult(new TestError { Result = "TestErrorString" }))
                    //.WithExceptionFactory(context =>
                    //{
                    //    if (context.Response != null && context.Request != null)
                    //        context.Response.RequestMessage = context.Request;

                    //    return new HttpErrorException<TestError>((TestError)context.Error, context.Response);
                    //})
                    .SendAsync();
                }
            }
            catch (MaximumAutoRedirectsException ex)
            {
                _logger.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 6
0
        public async Task ErrorMessageWithPostLooksPretty()
        {
            try
            {
                using (var server = LocalWebServer.ListenInBackground(new XUnitMockLogger(_logger)))
                {
                    server.WithNextResponse(HttpStatusCode.BadRequest, "Another Error");

                    await new TypedBuilderFactory().Create().WithUri(server.ListeningUri).AsPost().WithContent(TestRequest.Default1())
                    .WithErrorType <TestError>()
                    .Advanced
                    .WithErrorFactory((context, message, arg3, arg4) => Task.FromResult(new TestError {
                        Result = "TestErrorString"
                    }))
                    .WithExceptionFactory(context =>
                    {
                        if (context.Response != null && context.Request != null)
                        {
                            context.Response.RequestMessage = context.Request;
                        }

                        return(new HttpErrorException <TestError>((TestError)context.Error, context.Response, context.Request));
                    })
                    .SendAsync();
                }
            }
            catch (HttpCallException ex)
            {
                var message = ex.GetExceptionMessage();

                _logger.WriteLine(message);
            }
        }