예제 #1
0
        public async Task HalBuilderFactory_ResolvesCustomConfigurations()
        {
            var container = RegistrationHelpers.CreateContainer();

            using (var server = LocalWebServer.ListenInBackground(new XUnitMockLogger(_logger)))
            {
                server.WithNextResponseOk(TestResource.SerializedDefault1);
                var factory = container.Resolve <IHalBuilderFactory>();

                var    builder     = factory.Create();
                string actual      = null;
                string actualTyped = null;
                var    resource    = await builder.WithLink(server.ListeningUri)
                                     .Advanced
                                     .WithCaching(false)
                                     .OnSending(HandlerPriority.Last, context =>
                {
                    actual      = context.Items["CustomHalConfiguration"] as string;
                    actualTyped = context.Items["CustomTypedConfiguration"] as string;
                })
                                     .ResultAsync <TestResource>();

                resource.ShouldBe(TestResource.Default1());
                actual.ShouldBe("CustomHalConfiguration: It Works!");
                actualTyped.ShouldBeNull();
            }
        }
예제 #2
0
        public async Task WhenResultAndSendingTypesMismatchAndSuppressTypeException_ExpectResult()
        {
            var builder = CreateBuilder().WithResult(TestResource.Default1()).WithLink(MockUri);

            //act
            var actual = await builder.Advanced.WithSuppressTypeMismatchExceptions().OnSendingWithResult <AlternateTestResource>(ctx => { }).ResultAsync <TestResource>();

            actual.ShouldNotBeNull();
        }
예제 #3
0
        public async Task WhenResultAndSendingHandlerTypesMismatch_ExpectException()
        {
            var builder = CreateBuilder().WithLink(MockUri);

            //act
            await Should.ThrowAsync <TypeMismatchException>(builder.Advanced.OnResult <TestResource>(ctx =>
            {
                ctx.Result = TestResource.Default1();
            }).ResultAsync <AlternateTestResource>());
        }
예제 #4
0
        public async Task WhenErrorAndErrorHandlerTypesMismatch_ExpectException()
        {
            var builder = CreateBuilder().WithError(TestResource.Default1()).WithLink(MockUri);

            // act & assert
            await Should.ThrowAsync <TypeMismatchException>(
                builder.WithErrorType <TestResource>().Advanced.OnError <AlternateTestResource>(ctx =>
            {
                var error = ctx.Error;
            }).ResultAsync <TestResource>());
        }
예제 #5
0
        public async Task WhenDefaultResultAndResultTypesMismatch_ExpectException()
        {
            var builder = CreateBuilder().WithError(TestResource.Default1()).WithLink(MockUri);

            //act
            await Should.ThrowAsync <TypeMismatchException>(
                builder.WithDefaultResult(TestResource.Default1())
                .Advanced.WithExceptionFactory(context => null)
                .ResultAsync <AlternateTestResource>()
                );
        }
예제 #6
0
        public async Task WhenDefaultResultAndResultTypesMismatchAndSuppressTypeException_ExpectNullResult()
        {
            var builder = CreateBuilder().WithError(TestResource.Default1()).WithLink(MockUri);

            //act
            var actual = await builder.WithDefaultResult(TestResource.Default1())
                         .Advanced.WithSuppressTypeMismatchExceptions()
                         .OnException(context => context.ExceptionHandled = true)
                         .ResultAsync <AlternateTestResource>();

            actual.ShouldBeNull();
        }
예제 #7
0
        public void CanSerializeResource()
        {
            var original = TestResource.Default1();

            var          json         = JsonConvert.SerializeObject(original, _settings);
            const string expected     = TestResource.SerializedDefault1;
            var          deserialized = JsonConvert.DeserializeObject <TestResource>(json, _settings);

            json.ShouldNotBeNullOrWhiteSpace();
            json.ShouldBe(expected, StringCompareShould.IgnoreCase);
            original.ShouldBe(deserialized);
        }
예제 #8
0
        public async Task WhenResultHandlerIsObjectType_ExpectSuccess()
        {
            var builder = CreateBuilder().WithResult(TestResource.Default1()).WithLink(MockUri);

            //act
            var called = false;
            var result = await builder
                         .Advanced
                         .OnResult <IHalResource>(ctx => { called = true; })
                         .ResultAsync <TestResource>();

            result.ShouldNotBeNull();
            called.ShouldBeTrue();
        }
예제 #9
0
        public async Task CanDeserializeResource()
        {
            using (var server = LocalWebServer.ListenInBackground(new XUnitMockLogger(_logger)))
            {
                var uri = server.ListeningUri;
                server
                .WithNextResponse(new MockHttpResponseMessage().WithContent(TestResource.SerializedDefault1).WithPrivateCacheHeader().WithDefaultExpiration());

                var result = await CreateBuilder()
                             .WithLink(uri).ResultAsync <TestResource>();

                result.ShouldNotBeNull();
                result.ShouldBe(TestResource.Default1());
            }
        }
예제 #10
0
        public async Task HalBuilder_CanResolveAndCall()
        {
            var container = RegistrationHelpers.CreateContainer();

            using (var server = LocalWebServer.ListenInBackground(new XUnitMockLogger(_logger)))
            {
                server.WithNextResponseOk(TestResource.SerializedDefault1);

                var builder = container.Resolve <IHalBuilder>();

                var resource = await builder.WithLink(server.ListeningUri).Advanced.WithCaching(false).ResultAsync <TestResource>();

                resource.ShouldBe(TestResource.Default1());
            }
        }
예제 #11
0
        public async Task WhenErrorAndErrorHandlerTypesMismatchAndSuppressTypeException_ExpectResult()
        {
            var builder = CreateBuilder().WithError(TestResource.Default1()).WithLink(MockUri);

            //act
            var actual = await builder
                         .WithErrorType <TestResource>()
                         .Advanced
                         .WithSuppressTypeMismatchExceptions()
                         .OnError <AlternateTestResource>(ctx =>
            {
                ctx.ErrorHandled = true;
            })
                         .ResultAsync <TestResource>();

            actual.ShouldBeNull();
        }
예제 #12
0
        public async Task WhenHandlerIsSuperTypeOfResult_ExpectException()
        {
            var builder = CreateBuilder().WithResult(TestResource.Default1()).WithLink(MockUri);

            // act & assert
            await Should.ThrowAsync <TypeMismatchException>(async() =>
            {
                var actual = await builder
                             .Advanced
                             .OnResult <SubTestResource>(ctx =>
                {
                    var r = ctx.Result;
                })
                             .ResultAsync <TestResource>();

                actual.ShouldBeNull();
            });
        }
예제 #13
0
        public async Task WhenErrorHandlerIsObjectType_ExpectSuccess()
        {
            var builder = CreateBuilder().WithError(TestResource.Default1()).WithLink(MockUri);

            //act
            var called = false;
            var result = await builder
                         .WithErrorType <TestResource>()
                         .Advanced
                         .OnError <object>(ctx =>
            {
                ctx.ErrorHandled = true;
                called           = true;
            })
                         .ResultAsync <SubTestResource>();

            result.ShouldBeNull();
            called.ShouldBeTrue();
        }
예제 #14
0
        public async Task HalBuilderFactory_ResolvesCustomCacheHandlers()
        {
            var container = RegistrationHelpers.CreateContainer();

            using (var server = LocalWebServer.ListenInBackground(new XUnitMockLogger(_logger)))
            {
                server.WithNextResponse(new MockHttpResponseMessage().WithContent(TestResource.SerializedDefault1).WithPrivateCacheHeader().WithDefaultExpiration());
                var factory = container.Resolve <IHalBuilderFactory>();

                var    builder  = factory.Create();
                string actual   = null;
                var    resource = await builder.WithLink(server.ListeningUri)
                                  .Advanced.WithCaching(true)
                                  .OnCacheMiss(HandlerPriority.Last, context =>
                {
                    actual = context.Items["CustomTypedCacheHandler"] as string;
                })
                                  .ResultAsync <TestResource>();

                resource.ShouldBe(TestResource.Default1());
                actual.ShouldBe("CustomTypedCacheHandler: It Works!");
            }
        }