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(); } }
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(); }
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>()); }
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>()); }
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>() ); }
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(); }
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); }
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(); }
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()); } }
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()); } }
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(); }
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(); }); }
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(); }
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!"); } }