Browse Source

Add parameter values for path binding (PoC code).

pull/113/head
Halil İbrahim Kalkan 9 years ago
parent
commit
8f9d41817e
  1. 1
      src/Volo.Abp.AspNetCore.TestBase/Volo.Abp.AspNetCore.TestBase.csproj
  2. 5
      src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/AbpAspNetCoreIntegratedTestBase.cs
  3. 66
      src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs
  4. 36
      src/Volo.Abp.TestBase/AbpIntegratedTest.cs
  5. 72
      src/Volo.Abp.TestBase/AbpTestBaseWithServiceProvider.cs
  6. 18
      test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/PersonAppServiceClientProxy_Tests.cs
  7. 2
      test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/Repositories/MemoryDb_Basic_Repository_Tests.cs

1
src/Volo.Abp.AspNetCore.TestBase/Volo.Abp.AspNetCore.TestBase.csproj

@ -14,6 +14,7 @@
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.AspNetCore\Volo.Abp.AspNetCore.csproj" />
<ProjectReference Include="..\Volo.Abp.Http.Client\Volo.Abp.Http.Client.csproj" />
<ProjectReference Include="..\Volo.Abp.TestBase\Volo.Abp.TestBase.csproj" />
</ItemGroup>
<ItemGroup>

5
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<TStartup>
public abstract class AbpAspNetCoreIntegratedTestBase<TStartup> : AbpTestBaseWithServiceProvider
where TStartup : class
{
protected TestServer Server { get; }
protected HttpClient Client { get; }
protected IServiceProvider ServiceProvider { get; }
protected override IServiceProvider ServiceProvider { get; }
protected AbpAspNetCoreIntegratedTestBase()
{

66
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<ParameterApiDescriptionModel> 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<ParameterApiDescriptionModel> 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);
}
}
}

36
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<TStartupModule> : IDisposable
public class AbpIntegratedTest<TStartupModule> : 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<IUnitOfWorkManager>();
using (var uow = uowManager.Begin())
{
action();
uow.Complete();
}
}
}
protected virtual async Task UseUnitOfWorkAsync(Func<Task> action)
{
using (IServiceScope scope = ServiceProvider.CreateScope())
{
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
using (var uow = uowManager.Begin())
{
await action();
await uow.CompleteAsync();
}
}
}
public void Dispose()
{
Application.Shutdown();

72
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<IUnitOfWorkManager>();
using (var uow = uowManager.Begin())
{
action();
uow.Complete();
}
}
}
protected virtual async Task WithUnitOfWorkAsync(Func<Task> action)
{
using (var scope = ServiceProvider.CreateScope())
{
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
using (var uow = uowManager.Begin())
{
await action();
await uow.CompleteAsync();
}
}
}
protected virtual TResult WithUnitOfWork<TResult>(Func<TResult> func)
{
using (var scope = ServiceProvider.CreateScope())
{
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
using (var uow = uowManager.Begin())
{
var result = func();
uow.Complete();
return result;
}
}
}
protected virtual async Task<TResult> WithUnitOfWorkAsync<TResult>(Func<Task<TResult>> func)
{
using (var scope = ServiceProvider.CreateScope())
{
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
using (var uow = uowManager.Begin())
{
var result = await func();
await uow.CompleteAsync();
return result;
}
}
}
}
}

18
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<Person> _personRepository;
public PersonAppServiceClientProxy_Tests()
{
_peopleAppService = ServiceProvider.GetRequiredService<IPeopleAppService>();
_personRepository = ServiceProvider.GetRequiredService<IRepository<Person>>();
}
[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);
}
}
}

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

Loading…
Cancel
Save