diff --git a/src/clients/Elsa.Client/Extensions/ServiceCollectionExtensions.cs b/src/clients/Elsa.Client/Extensions/ServiceCollectionExtensions.cs index 3dff00c49..204b05078 100644 --- a/src/clients/Elsa.Client/Extensions/ServiceCollectionExtensions.cs +++ b/src/clients/Elsa.Client/Extensions/ServiceCollectionExtensions.cs @@ -17,7 +17,7 @@ namespace Elsa.Client.Extensions { public static class ServiceCollectionExtensions { - public static IServiceCollection AddElsaClient(this IServiceCollection services, Action? configure = default) + public static IServiceCollection AddElsaClient(this IServiceCollection services, Action? configure = default, Func? httpClientFactory = default) { if (configure != null) services.Configure(configure); @@ -33,31 +33,27 @@ namespace Elsa.Client.Extensions jsonSerializerSettings.Converters.Add(new JsonStringEnumConverter()); jsonSerializerSettings.Converters.Add(new TypeConverter()); jsonSerializerSettings.Converters.Add(new VersionOptionsConverter()); - + var refitSettings = new RefitSettings { ContentSerializer = new SystemTextJsonContentSerializer(jsonSerializerSettings) }; - - services.AddRefitClient(refitSettings).ConfigureHttpClient((sp, client) => + + if (httpClientFactory == null) { - var serverUrl = sp.GetRequiredService>().Value.ServerUrl; - client.BaseAddress = serverUrl; - }).AddHttpMessageHandler().AddHttpMessageHandler(); + services.AddRefitClient(refitSettings).ConfigureHttpClient((sp, client) => + { + var serverUrl = sp.GetRequiredService>().Value.ServerUrl; + client.BaseAddress = serverUrl; + }); + } + else + { + services.AddScoped(_ => RestService.For(httpClientFactory(), refitSettings)); + } - services.AddTransient(); - return services - .AddTransient(); + .AddTransient(); + } } - } - - public class Spy : DelegatingHandler - { - protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) - { - var response = await base.SendAsync(request, cancellationToken); - return response; - } - } -} \ No newline at end of file + } \ No newline at end of file diff --git a/src/dashboards/blazor/ElsaDashboard.Blazor/Shared/Rpc/IWorkflowDefinitionService.cs b/src/dashboards/blazor/ElsaDashboard.Blazor/Shared/Rpc/IWorkflowDefinitionService.cs index d66bed3a8..939298bbb 100644 --- a/src/dashboards/blazor/ElsaDashboard.Blazor/Shared/Rpc/IWorkflowDefinitionService.cs +++ b/src/dashboards/blazor/ElsaDashboard.Blazor/Shared/Rpc/IWorkflowDefinitionService.cs @@ -7,7 +7,7 @@ namespace ElsaDashboard.Blazor.Shared.Rpc [ProtoContract] public interface IWorkflowDefinitionService { - ValueTask> GetAsync(string workflowDefinitionId, VersionOptions version); + //ValueTask> GetAsync(string workflowDefinitionId, VersionOptions version); } public record Test(int a, bool b); diff --git a/test/component/Elsa.ComponentTests/Clients/RestClient/WorkflowDefinitions/PostTests.cs b/test/component/Elsa.ComponentTests/Clients/RestClient/WorkflowDefinitions/PostTests.cs index 9b00e87cc..190480254 100644 --- a/test/component/Elsa.ComponentTests/Clients/RestClient/WorkflowDefinitions/PostTests.cs +++ b/test/component/Elsa.ComponentTests/Clients/RestClient/WorkflowDefinitions/PostTests.cs @@ -1,44 +1,24 @@ -using System; -using System.Net; using System.Threading.Tasks; using AutoFixture; using Elsa.Activities.Console; -using Elsa.Client; -using Elsa.Client.Extensions; using Elsa.Client.Models; using Elsa.ComponentTests.Helpers; -using Elsa.Testing.Shared.AutoFixture; -using Elsa.Testing.Shared.Helpers; -using Microsoft.Extensions.DependencyInjection; using Xunit; namespace Elsa.ComponentTests.Clients.RestClient.WorkflowDefinitions { [Collection(ComponentTestsCollection.Name)] - public class PostTests : IDisposable + public class PostTests : ElsaClientTestBase { - private readonly IFixture _fixture; - private readonly TemporaryFolder _tempFolder; - private readonly ElsaHostApplicationFactory _hostApplicationFactory; - private readonly IElsaClient _elsaClient; - - public PostTests(ElsaHostApplicationFactory hostApplicationFactory) + public PostTests(ElsaHostApplicationFactory hostApplicationFactory) : base(hostApplicationFactory) { - _fixture = new Fixture().Customize(new NodaTimeCustomization()); - _tempFolder = new TemporaryFolder(); - _hostApplicationFactory = hostApplicationFactory; - hostApplicationFactory.SetDbConnectionString($@"Data Source={_tempFolder.Folder}elsa.db;Cache=Shared"); - var httpClient = hostApplicationFactory.CreateClient(); - - var services = new ServiceCollection().AddElsaClient(options => options.ServerUrl = httpClient.BaseAddress!).BuildServiceProvider(); - _elsaClient = services.GetRequiredService(); } [Fact(DisplayName = "Posting a new workflow definition returns HTTP 201.")] public async Task Post01() { var request = CreateWorkflowDefinitionRequest(); - var workflowDefinition = await _elsaClient.WorkflowDefinitions.PostAsync(request); + var workflowDefinition = await ElsaClient.WorkflowDefinitions.PostAsync(request); Assert.Equal(request.Name, workflowDefinition.Name); } @@ -46,7 +26,7 @@ namespace Elsa.ComponentTests.Clients.RestClient.WorkflowDefinitions { var writeLine = new ActivityDefinition { - ActivityId = _fixture.Create(), + ActivityId = Fixture.Create(), Type = nameof(WriteLine), Properties = new ActivityDefinitionProperties { @@ -54,20 +34,14 @@ namespace Elsa.ComponentTests.Clients.RestClient.WorkflowDefinitions } }; - var readLine = new ActivityDefinition { ActivityId = _fixture.Create(), Type = nameof(ReadLine) }; - var activities = new[] { writeLine, readLine }; - var connections = new[] { new ConnectionDefinition(writeLine.ActivityId, readLine.ActivityId, OutcomeNames.Done) }; + var readLine = new ActivityDefinition {ActivityId = Fixture.Create(), Type = nameof(ReadLine)}; + var activities = new[] {writeLine, readLine}; + var connections = new[] {new ConnectionDefinition(writeLine.ActivityId, readLine.ActivityId, OutcomeNames.Done)}; - return _fixture.Build() + return Fixture.Build() .With(x => x.Activities, activities) .With(x => x.Connections, connections) .Create(); } - - public void Dispose() - { - _hostApplicationFactory.Dispose(); - _tempFolder.Dispose(); - } } } \ No newline at end of file diff --git a/test/component/Elsa.ComponentTests/Endpoints/WorkflowDefinitions/PostTests.cs b/test/component/Elsa.ComponentTests/Endpoints/WorkflowDefinitions/PostTests.cs index 4835ebfce..04287203e 100644 --- a/test/component/Elsa.ComponentTests/Endpoints/WorkflowDefinitions/PostTests.cs +++ b/test/component/Elsa.ComponentTests/Endpoints/WorkflowDefinitions/PostTests.cs @@ -63,7 +63,7 @@ namespace Elsa.ComponentTests.Endpoints.WorkflowDefinitions public void Dispose() { - _hostApplicationFactory.Dispose(); + _httpClient.Dispose(); _tempFolder.Dispose(); } } diff --git a/test/component/Elsa.ComponentTests/Helpers/ElsaClientTestBase.cs b/test/component/Elsa.ComponentTests/Helpers/ElsaClientTestBase.cs new file mode 100644 index 000000000..6bcd384ce --- /dev/null +++ b/test/component/Elsa.ComponentTests/Helpers/ElsaClientTestBase.cs @@ -0,0 +1,18 @@ +using Elsa.Client; +using Elsa.Client.Extensions; +using Microsoft.Extensions.DependencyInjection; + +namespace Elsa.ComponentTests.Helpers +{ + public abstract class ElsaClientTestBase : TestBase + { + + protected ElsaClientTestBase(ElsaHostApplicationFactory hostApplicationFactory) : base(hostApplicationFactory) + { + var services = new ServiceCollection().AddElsaClient(options => options.ServerUrl = HttpClient.BaseAddress!, () => HttpClient).BuildServiceProvider(); + ElsaClient = services.GetRequiredService(); + } + + protected IElsaClient ElsaClient { get; } + } +} \ No newline at end of file diff --git a/test/component/Elsa.ComponentTests/Helpers/TestBase.cs b/test/component/Elsa.ComponentTests/Helpers/TestBase.cs new file mode 100644 index 000000000..691557726 --- /dev/null +++ b/test/component/Elsa.ComponentTests/Helpers/TestBase.cs @@ -0,0 +1,21 @@ +using System.Net.Http; + +namespace Elsa.ComponentTests.Helpers +{ + public abstract class TestBase : WorkflowsComponentTestBase + { + protected TestBase(ElsaHostApplicationFactory hostApplicationFactory) : base(hostApplicationFactory) + { + HttpClient = hostApplicationFactory.CreateClient(); + } + + protected HttpClient HttpClient { get; } + + public override void Dispose() + { + HttpClient.Dispose(); + + base.Dispose(); + } + } +} \ No newline at end of file diff --git a/test/component/Elsa.ComponentTests/Helpers/WorkflowsComponentTestBase.cs b/test/component/Elsa.ComponentTests/Helpers/WorkflowsComponentTestBase.cs new file mode 100644 index 000000000..89874e942 --- /dev/null +++ b/test/component/Elsa.ComponentTests/Helpers/WorkflowsComponentTestBase.cs @@ -0,0 +1,29 @@ +using System; +using AutoFixture; +using Elsa.Testing.Shared.AutoFixture; +using Elsa.Testing.Shared.Helpers; + +namespace Elsa.ComponentTests.Helpers +{ + public abstract class WorkflowsComponentTestBase : IDisposable + { + + protected WorkflowsComponentTestBase(ElsaHostApplicationFactory hostApplicationFactory) + { + HostApplicationFactory = hostApplicationFactory; + Fixture = new Fixture().Customize(new NodaTimeCustomization()); + TempFolder = new TemporaryFolder(); + hostApplicationFactory.SetDbConnectionString($@"Data Source={TempFolder.Folder}elsa.db;Cache=Shared"); + } + + protected ElsaHostApplicationFactory HostApplicationFactory { get; } + protected IFixture Fixture { get; } + protected TemporaryFolder TempFolder { get; } + + + public virtual void Dispose() + { + TempFolder.Dispose(); + } + } +} \ No newline at end of file diff --git a/test/integration/Elsa.Core.IntegrationTests/StorageProvider/StorageWorkflowProviderTests.cs b/test/integration/Elsa.Core.IntegrationTests/StorageProvider/StorageWorkflowProviderTests.cs index 6b117349c..d462fff47 100644 --- a/test/integration/Elsa.Core.IntegrationTests/StorageProvider/StorageWorkflowProviderTests.cs +++ b/test/integration/Elsa.Core.IntegrationTests/StorageProvider/StorageWorkflowProviderTests.cs @@ -10,7 +10,7 @@ using Xunit.Abstractions; namespace Elsa.Core.IntegrationTests.StorageProvider { - public class StorageWorkflowProviderTests : WorkflowsTestBase + public class StorageWorkflowProviderTests : WorkflowsUnitTestBase { public StorageWorkflowProviderTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { diff --git a/test/integration/Elsa.Core.IntegrationTests/Workflows/BasicWorkflowTests.cs b/test/integration/Elsa.Core.IntegrationTests/Workflows/BasicWorkflowTests.cs index 7d3a2ba98..71acefe2b 100644 --- a/test/integration/Elsa.Core.IntegrationTests/Workflows/BasicWorkflowTests.cs +++ b/test/integration/Elsa.Core.IntegrationTests/Workflows/BasicWorkflowTests.cs @@ -6,7 +6,7 @@ using Xunit.Abstractions; namespace Elsa.Core.IntegrationTests.Workflows { - public class BasicWorkflowTests : WorkflowsTestBase + public class BasicWorkflowTests : WorkflowsUnitTestBase { public BasicWorkflowTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { diff --git a/test/integration/Elsa.Core.IntegrationTests/Workflows/FinishWorkflowTests.cs b/test/integration/Elsa.Core.IntegrationTests/Workflows/FinishWorkflowTests.cs index e8b356daf..c2058b1bb 100644 --- a/test/integration/Elsa.Core.IntegrationTests/Workflows/FinishWorkflowTests.cs +++ b/test/integration/Elsa.Core.IntegrationTests/Workflows/FinishWorkflowTests.cs @@ -7,7 +7,7 @@ using Xunit.Abstractions; namespace Elsa.Core.IntegrationTests.Workflows { - public class FinishWorkflowTests : WorkflowsTestBase + public class FinishWorkflowTests : WorkflowsUnitTestBase { private readonly Fixture _fixture; diff --git a/test/integration/Elsa.Core.IntegrationTests/Workflows/ForEachWorkflowTests.cs b/test/integration/Elsa.Core.IntegrationTests/Workflows/ForEachWorkflowTests.cs index 3124db3f6..712ac2f53 100644 --- a/test/integration/Elsa.Core.IntegrationTests/Workflows/ForEachWorkflowTests.cs +++ b/test/integration/Elsa.Core.IntegrationTests/Workflows/ForEachWorkflowTests.cs @@ -7,7 +7,7 @@ using Xunit.Abstractions; namespace Elsa.Core.IntegrationTests.Workflows { - public class ForEachWorkflowTests : WorkflowsTestBase + public class ForEachWorkflowTests : WorkflowsUnitTestBase { public ForEachWorkflowTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { diff --git a/test/integration/Elsa.Core.IntegrationTests/Workflows/ForLoopWorkflowTests.cs b/test/integration/Elsa.Core.IntegrationTests/Workflows/ForLoopWorkflowTests.cs index 665b00b4d..6da8b57da 100644 --- a/test/integration/Elsa.Core.IntegrationTests/Workflows/ForLoopWorkflowTests.cs +++ b/test/integration/Elsa.Core.IntegrationTests/Workflows/ForLoopWorkflowTests.cs @@ -6,7 +6,7 @@ using Xunit.Abstractions; namespace Elsa.Core.IntegrationTests.Workflows { - public class ForLoopWorkflowTests : WorkflowsTestBase + public class ForLoopWorkflowTests : WorkflowsUnitTestBase { public ForLoopWorkflowTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { diff --git a/test/integration/Elsa.Core.IntegrationTests/Workflows/ForkJoinWorkflowTests.cs b/test/integration/Elsa.Core.IntegrationTests/Workflows/ForkJoinWorkflowTests.cs index 59e15555c..79a8be245 100644 --- a/test/integration/Elsa.Core.IntegrationTests/Workflows/ForkJoinWorkflowTests.cs +++ b/test/integration/Elsa.Core.IntegrationTests/Workflows/ForkJoinWorkflowTests.cs @@ -11,7 +11,7 @@ using Xunit.Abstractions; namespace Elsa.Core.IntegrationTests.Workflows { - public class ForkJoinWorkflowTests : WorkflowsTestBase + public class ForkJoinWorkflowTests : WorkflowsUnitTestBase { public ForkJoinWorkflowTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { diff --git a/test/integration/Elsa.Core.IntegrationTests/Workflows/IfElseWorkflowTests.cs b/test/integration/Elsa.Core.IntegrationTests/Workflows/IfElseWorkflowTests.cs index 52f8381b8..bf7c8f53f 100644 --- a/test/integration/Elsa.Core.IntegrationTests/Workflows/IfElseWorkflowTests.cs +++ b/test/integration/Elsa.Core.IntegrationTests/Workflows/IfElseWorkflowTests.cs @@ -6,7 +6,7 @@ using Xunit.Abstractions; namespace Elsa.Core.IntegrationTests.Workflows { - public class IfElseWorkflowTests : WorkflowsTestBase + public class IfElseWorkflowTests : WorkflowsUnitTestBase { public IfElseWorkflowTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { diff --git a/test/integration/Elsa.Core.IntegrationTests/Workflows/ParallelForEachWorkflowTests.cs b/test/integration/Elsa.Core.IntegrationTests/Workflows/ParallelForEachWorkflowTests.cs index 96f04d590..706003db3 100644 --- a/test/integration/Elsa.Core.IntegrationTests/Workflows/ParallelForEachWorkflowTests.cs +++ b/test/integration/Elsa.Core.IntegrationTests/Workflows/ParallelForEachWorkflowTests.cs @@ -7,7 +7,7 @@ using Xunit.Abstractions; namespace Elsa.Core.IntegrationTests.Workflows { - public class ParallelForEachWorkflowTests : WorkflowsTestBase + public class ParallelForEachWorkflowTests : WorkflowsUnitTestBase { public ParallelForEachWorkflowTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { diff --git a/test/integration/Elsa.Core.IntegrationTests/Workflows/SwitchWorkflowTests.cs b/test/integration/Elsa.Core.IntegrationTests/Workflows/SwitchWorkflowTests.cs index eff629f1d..ac85b1fa0 100644 --- a/test/integration/Elsa.Core.IntegrationTests/Workflows/SwitchWorkflowTests.cs +++ b/test/integration/Elsa.Core.IntegrationTests/Workflows/SwitchWorkflowTests.cs @@ -6,7 +6,7 @@ using Xunit.Abstractions; namespace Elsa.Core.IntegrationTests.Workflows { - public class SwitchWorkflowTests : WorkflowsTestBase + public class SwitchWorkflowTests : WorkflowsUnitTestBase { public SwitchWorkflowTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { diff --git a/test/integration/Elsa.Core.IntegrationTests/Workflows/WhileWorkflowTests.cs b/test/integration/Elsa.Core.IntegrationTests/Workflows/WhileWorkflowTests.cs index 5f8e5553f..4fc2a57b2 100644 --- a/test/integration/Elsa.Core.IntegrationTests/Workflows/WhileWorkflowTests.cs +++ b/test/integration/Elsa.Core.IntegrationTests/Workflows/WhileWorkflowTests.cs @@ -6,7 +6,7 @@ using Xunit.Abstractions; namespace Elsa.Core.IntegrationTests.Workflows { - public class WhileWorkflowTests : WorkflowsTestBase + public class WhileWorkflowTests : WorkflowsUnitTestBase { public WhileWorkflowTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { diff --git a/test/shared/Elsa.Testing.Shared/Elsa.Testing.Shared.csproj b/test/shared/Elsa.Testing.Shared/Elsa.Testing.Shared.csproj index 9b22125bb..868ae0ed7 100644 --- a/test/shared/Elsa.Testing.Shared/Elsa.Testing.Shared.csproj +++ b/test/shared/Elsa.Testing.Shared/Elsa.Testing.Shared.csproj @@ -19,11 +19,11 @@ - - - - - + + + + + @@ -34,7 +34,7 @@ - + diff --git a/test/shared/Elsa.Testing.Shared/Helpers/WorkflowsTestBase.cs b/test/shared/Elsa.Testing.Shared/Unit/WorkflowsUnitTestBase.cs similarity index 92% rename from test/shared/Elsa.Testing.Shared/Helpers/WorkflowsTestBase.cs rename to test/shared/Elsa.Testing.Shared/Unit/WorkflowsUnitTestBase.cs index 538362f9c..55c8c41ad 100644 --- a/test/shared/Elsa.Testing.Shared/Helpers/WorkflowsTestBase.cs +++ b/test/shared/Elsa.Testing.Shared/Unit/WorkflowsUnitTestBase.cs @@ -11,11 +11,11 @@ using YesSql.Provider.Sqlite; namespace Elsa.Testing.Shared.Helpers { - public abstract class WorkflowsTestBase : IAsyncLifetime, IDisposable + public abstract class WorkflowsUnitTestBase : IAsyncLifetime, IDisposable { private readonly TemporaryFolder _tempFolder; - protected WorkflowsTestBase(ITestOutputHelper testOutputHelper, Action? configureServices = default) + protected WorkflowsUnitTestBase(ITestOutputHelper testOutputHelper, Action? configureServices = default) { _tempFolder = new TemporaryFolder(); TestOutputHelper = testOutputHelper;