diff --git a/src/Volo.Abp.AspNetCore.TestBase/Volo.Abp.AspNetCore.TestBase.csproj b/src/Volo.Abp.AspNetCore.TestBase/Volo.Abp.AspNetCore.TestBase.csproj index 7090e4bf39..88e853ae3a 100644 --- a/src/Volo.Abp.AspNetCore.TestBase/Volo.Abp.AspNetCore.TestBase.csproj +++ b/src/Volo.Abp.AspNetCore.TestBase/Volo.Abp.AspNetCore.TestBase.csproj @@ -14,6 +14,7 @@ + diff --git a/src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/AbpAspNetCoreIntegratedTestBase.cs b/src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/AbpAspNetCoreIntegratedTestBase.cs index 81c85197b1..efdcf0eeb9 100644 --- a/src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/AbpAspNetCoreIntegratedTestBase.cs +++ b/src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/AbpAspNetCoreIntegratedTestBase.cs @@ -6,17 +6,18 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.TestBase; namespace Volo.Abp.AspNetCore.TestBase { - public abstract class AbpAspNetCoreIntegratedTestBase + public abstract class AbpAspNetCoreIntegratedTestBase : AbpTestBaseWithServiceProvider where TStartup : class { protected TestServer Server { get; } protected HttpClient Client { get; } - protected IServiceProvider ServiceProvider { get; } + protected override IServiceProvider ServiceProvider { get; } protected AbpAspNetCoreIntegratedTestBase() { diff --git a/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs b/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs index 1fdae184de..c0bcfe6ecd 100644 --- a/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs +++ b/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; @@ -58,7 +59,9 @@ namespace Volo.Abp.Http.Client.DynamicProxying using (var client = _httpClientFactory.Create()) { - var response = await client.GetAsync(proxyConfig.BaseUrl + actionApiDescription.Url); + var url = GenerateUrlWithParameters(actionApiDescription, invocation); + + var response = await client.GetAsync(proxyConfig.BaseUrl + url); if (!response.IsSuccessStatusCode) { throw new AbpException("Remote service returns error!"); @@ -83,5 +86,64 @@ namespace Volo.Abp.Http.Client.DynamicProxying return _options.HttpClientProxies.GetOrDefault(typeof(TService)) ?? throw new AbpException($"Could not get DynamicHttpClientProxyConfig for {typeof(TService).FullName}."); } + + public static string GenerateUrlWithParameters(ActionApiDescriptionModel action, IAbpMethodInvocation invocation) + { + //TODO: Can be optimized using StringBuilder? + var url = ReplacePathVariables(action.Url, action.Parameters, invocation); + //url = AddQueryStringParameters(url, action.Parameters); + return url; + } + + private static string ReplacePathVariables(string url, IList actionParameters, IAbpMethodInvocation invocation) + { + var pathParameters = actionParameters + .Where(p => p.BindingSourceId == "Path") + .ToArray(); + + if (!pathParameters.Any()) + { + return url; + } + + foreach (var pathParameter in pathParameters) + { + url = url.Replace($"{{{pathParameter.Name}}}", FindParameterValue(invocation.Method, invocation.Arguments, pathParameter.Name)); + } + + return url; + } + + //private static string AddQueryStringParameters(string url, IList actionParameters) + //{ + // var queryStringParameters = actionParameters + // .Where(p => p.BindingSourceId.IsIn("ModelBinding", "Query")) + // .ToArray(); + + // if (!queryStringParameters.Any()) + // { + // return url; + // } + + // var qsBuilderParams = queryStringParameters + // .Select(p => $"{{ name: '{p.Name.ToCamelCase()}', value: {ProxyScriptingJsFuncHelper.GetParamNameInJsFunc(p)} }}") + // .JoinAsString(", "); + + // return url + $"' + abp.utils.buildQueryString([{qsBuilderParams}]) + '"; + //} + + private static string FindParameterValue(MethodInfo method, object[] arguments, string parameterName) + { + var methodParameters = method.GetParameters(); + for (int i = 0; i < methodParameters.Length; i++) + { + if (methodParameters[i].Name == parameterName) + { + return arguments[i].ToString(); + } + } + + throw new AbpException("Could not find parameter in the invocation: " + parameterName); + } } } \ No newline at end of file diff --git a/src/Volo.Abp.TestBase/AbpIntegratedTest.cs b/src/Volo.Abp.TestBase/AbpIntegratedTest.cs index 802ba0ae47..d65cddfb2e 100644 --- a/src/Volo.Abp.TestBase/AbpIntegratedTest.cs +++ b/src/Volo.Abp.TestBase/AbpIntegratedTest.cs @@ -1,17 +1,15 @@ using System; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Modularity; -using Volo.Abp.Uow; -using System.Threading.Tasks; namespace Volo.Abp.TestBase { - public class AbpIntegratedTest : IDisposable + public class AbpIntegratedTest : AbpTestBaseWithServiceProvider, IDisposable where TStartupModule : IAbpModule { protected IAbpApplication Application { get; } - protected IServiceProvider ServiceProvider => Application.ServiceProvider; + protected override IServiceProvider ServiceProvider => Application.ServiceProvider; protected IServiceScope MainServiceScope { get; } @@ -58,36 +56,6 @@ namespace Volo.Abp.TestBase } - protected virtual void UseUnitOfWork(Action action) - { - using (IServiceScope scope = ServiceProvider.CreateScope()) - { - var uowManager = scope.ServiceProvider.GetRequiredService(); - - using (var uow = uowManager.Begin()) - { - action(); - - uow.Complete(); - } - } - } - - protected virtual async Task UseUnitOfWorkAsync(Func action) - { - using (IServiceScope scope = ServiceProvider.CreateScope()) - { - var uowManager = scope.ServiceProvider.GetRequiredService(); - - using (var uow = uowManager.Begin()) - { - await action(); - - await uow.CompleteAsync(); - } - } - } - public void Dispose() { Application.Shutdown(); diff --git a/src/Volo.Abp.TestBase/AbpTestBaseWithServiceProvider.cs b/src/Volo.Abp.TestBase/AbpTestBaseWithServiceProvider.cs new file mode 100644 index 0000000000..4f8eb7641d --- /dev/null +++ b/src/Volo.Abp.TestBase/AbpTestBaseWithServiceProvider.cs @@ -0,0 +1,72 @@ +using System; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Uow; + +namespace Volo.Abp.TestBase +{ + public abstract class AbpTestBaseWithServiceProvider + { + protected abstract IServiceProvider ServiceProvider { get; } + + protected virtual void WithUnitOfWork(Action action) + { + using (var scope = ServiceProvider.CreateScope()) + { + var uowManager = scope.ServiceProvider.GetRequiredService(); + + using (var uow = uowManager.Begin()) + { + action(); + + uow.Complete(); + } + } + } + + protected virtual async Task WithUnitOfWorkAsync(Func action) + { + using (var scope = ServiceProvider.CreateScope()) + { + var uowManager = scope.ServiceProvider.GetRequiredService(); + + using (var uow = uowManager.Begin()) + { + await action(); + + await uow.CompleteAsync(); + } + } + } + + protected virtual TResult WithUnitOfWork(Func func) + { + using (var scope = ServiceProvider.CreateScope()) + { + var uowManager = scope.ServiceProvider.GetRequiredService(); + + using (var uow = uowManager.Begin()) + { + var result = func(); + uow.Complete(); + return result; + } + } + } + + protected virtual async Task WithUnitOfWorkAsync(Func> func) + { + using (var scope = ServiceProvider.CreateScope()) + { + var uowManager = scope.ServiceProvider.GetRequiredService(); + + using (var uow = uowManager.Begin()) + { + var result = await func(); + await uow.CompleteAsync(); + return result; + } + } + } + } +} \ No newline at end of file diff --git a/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/PersonAppServiceClientProxy_Tests.cs b/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/PersonAppServiceClientProxy_Tests.cs index 4d6c18ceca..9faca4d701 100644 --- a/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/PersonAppServiceClientProxy_Tests.cs +++ b/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/PersonAppServiceClientProxy_Tests.cs @@ -1,8 +1,11 @@ -using System.Threading.Tasks; +using System.Linq; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Shouldly; using Volo.Abp.Application.Dtos; +using Volo.Abp.Domain.Repositories; using Volo.Abp.TestApp.Application; +using Volo.Abp.TestApp.Domain; using Xunit; namespace Volo.Abp.Http.DynamicProxying @@ -10,10 +13,12 @@ namespace Volo.Abp.Http.DynamicProxying public class PersonAppServiceClientProxy_Tests : AbpHttpTestBase { private readonly IPeopleAppService _peopleAppService; + private readonly IRepository _personRepository; public PersonAppServiceClientProxy_Tests() { _peopleAppService = ServiceProvider.GetRequiredService(); + _personRepository = ServiceProvider.GetRequiredService>(); } [Fact] @@ -23,5 +28,16 @@ namespace Volo.Abp.Http.DynamicProxying people.TotalCount.ShouldBeGreaterThan(0); people.Items.Count.ShouldBe(people.TotalCount); } + + [Fact] + public async Task Test_GetById() + { + var firstPerson = _personRepository.GetList().First(); + + var person = await _peopleAppService.Get(firstPerson.Id); + person.ShouldNotBeNull(); + person.Id.ShouldBe(firstPerson.Id); + person.Name.ShouldBe(firstPerson.Name); + } } } diff --git a/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/Repositories/MemoryDb_Basic_Repository_Tests.cs b/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/Repositories/MemoryDb_Basic_Repository_Tests.cs index 98812731f0..07b38b227f 100644 --- a/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/Repositories/MemoryDb_Basic_Repository_Tests.cs +++ b/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/Repositories/MemoryDb_Basic_Repository_Tests.cs @@ -34,7 +34,7 @@ namespace Volo.Abp.MemoryDb.Repositories _personRepository.Insert(new Person(Guid.NewGuid(), name, 42)); //Assert - UseUnitOfWork(() => + WithUnitOfWork(() => { _personRepository.FirstOrDefault(p => p.Name == name).ShouldNotBeNull(); });