From 506be991446818ecd6792c47838721a57521b609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Sat, 16 Sep 2017 11:28:12 +0300 Subject: [PATCH] Added Update test for PersonAppServiceClientProxy_Tests --- .../DynamicHttpProxyInterceptor.cs | 12 ++-- .../DynamicProxying/RequestPayloadBuilder.cs | 56 ++++++++++++++++++- .../Services/AsyncCrudAppService.cs | 2 + .../PersonAppServiceClientProxy_Tests.cs | 28 ++++++++++ 4 files changed, 90 insertions(+), 8 deletions(-) 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 e5063765d6..a9941384a2 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,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using System.Net.Http; +using System.Net.Http.Headers; using System.Net.Mime; using System.Reflection; using System.Text; @@ -61,7 +62,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying invocation.ReturnValue = GenericInterceptAsyncMethod .MakeGenericMethod(invocation.Method.ReturnType.GenericTypeArguments[0]) - .Invoke(this, new object[] {invocation}); + .Invoke(this, new object[] { invocation }); return Task.CompletedTask; } @@ -89,13 +90,10 @@ namespace Volo.Abp.Http.Client.DynamicProxying var actionApiDescription = await _apiDescriptionFinder.FindActionAsync(proxyConfig, invocation.Method); var url = proxyConfig.BaseUrl + UrlBuilder.GenerateUrlWithParameters(actionApiDescription, invocation.ArgumentsDictionary); - var requestMessage = new HttpRequestMessage(actionApiDescription.GetHttpMethod(), url); - - var body = RequestPayloadBuilder.GenerateBody(actionApiDescription, invocation.ArgumentsDictionary, _jsonSerializer); - if (body != null) + var requestMessage = new HttpRequestMessage(actionApiDescription.GetHttpMethod(), url) { - requestMessage.Content = new StringContent(body, Encoding.UTF8, "application/json"); //TODO: application/json to a constant - } + Content = RequestPayloadBuilder.BuildContent(actionApiDescription, invocation.ArgumentsDictionary, _jsonSerializer) + }; var response = await client.SendAsync(requestMessage); diff --git a/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/RequestPayloadBuilder.cs b/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/RequestPayloadBuilder.cs index fd0a4d123c..7719e031b0 100644 --- a/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/RequestPayloadBuilder.cs +++ b/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/RequestPayloadBuilder.cs @@ -1,5 +1,8 @@ using System.Collections.Generic; using System.Linq; +using System.Net.Http; +using System.Text; +using JetBrains.Annotations; using Volo.Abp.Http.Modeling; using Volo.Abp.Json; @@ -7,7 +10,26 @@ namespace Volo.Abp.Http.Client.DynamicProxying { public static class RequestPayloadBuilder { - public static string GenerateBody(ActionApiDescriptionModel action, IReadOnlyDictionary methodArguments, IJsonSerializer jsonSerializer) + [CanBeNull] + public static HttpContent BuildContent(ActionApiDescriptionModel action,IReadOnlyDictionary methodArguments, IJsonSerializer jsonSerializer) + { + var body = GenerateBody(action, methodArguments, jsonSerializer); + + if (body != null) + { + return new StringContent(body, Encoding.UTF8, "application/json"); //TODO: application/json to a constant + } + + body = GenerateFormPostData(action, methodArguments); + if (body != null) + { + return new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded"); //TODO: application/x-www-form-urlencoded to a constant + } + + return null; + } + + private static string GenerateBody(ActionApiDescriptionModel action, IReadOnlyDictionary methodArguments, IJsonSerializer jsonSerializer) { var parameters = action .Parameters @@ -34,5 +56,37 @@ namespace Volo.Abp.Http.Client.DynamicProxying return jsonSerializer.Serialize(value); } + + private static string GenerateFormPostData(ActionApiDescriptionModel action, IReadOnlyDictionary methodArguments) + { + var parameters = action + .Parameters + .Where(p => p.BindingSourceId == "Form") + .ToArray(); + + if (!parameters.Any()) + { + return null; + } + + var postDataBuilder = new StringBuilder(); + + var isFirstParam = true; + foreach (var queryStringParameter in parameters) + { + var value = HttpActionParameterHelper.FindParameterValue(methodArguments, queryStringParameter); + if (value == null) + { + continue; + } + + postDataBuilder.Append(isFirstParam ? "?" : "&"); + postDataBuilder.Append(queryStringParameter.Name + "=" + System.Net.WebUtility.UrlEncode(value.ToString())); + + isFirstParam = false; + } + + return postDataBuilder.ToString(); + } } } diff --git a/src/Volo.Abp/Volo/Abp/Application/Services/AsyncCrudAppService.cs b/src/Volo.Abp/Volo/Abp/Application/Services/AsyncCrudAppService.cs index 2aaf538f1c..966100513a 100644 --- a/src/Volo.Abp/Volo/Abp/Application/Services/AsyncCrudAppService.cs +++ b/src/Volo.Abp/Volo/Abp/Application/Services/AsyncCrudAppService.cs @@ -117,6 +117,8 @@ namespace Volo.Abp.Application.Services var entity = await GetEntityByIdAsync(id); + //TODO: Check if input has id different than given id and normalize if it's default value, throw ex otherwise + MapToEntity(input, entity); await CurrentUnitOfWork.SaveChangesAsync(); 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 f4866beb3c..c2bef5bc7b 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 @@ -73,6 +73,34 @@ namespace Volo.Abp.Http.DynamicProxying personInDb.ShouldNotBeNull(); personInDb.Id.ShouldBe(person.Id); } + + [Fact] + public async Task Update() + { + var firstPerson = _personRepository.GetList().First(); + var uniquePersonName = Guid.NewGuid().ToString(); + + var person = await _peopleAppService.Update( + firstPerson.Id, + new PersonDto + { + Id = firstPerson.Id, + Name = uniquePersonName, + Age = firstPerson.Age + } + ); + + person.ShouldNotBeNull(); + person.Id.ShouldBe(firstPerson.Id); + person.Name.ShouldBe(uniquePersonName); + person.Age.ShouldBe(firstPerson.Age); + + var personInDb = _personRepository.GetList().FirstOrDefault(p => p.Id == firstPerson.Id); + personInDb.ShouldNotBeNull(); + personInDb.Id.ShouldBe(person.Id); + personInDb.Name.ShouldBe(person.Name); + personInDb.Age.ShouldBe(person.Age); + } [Fact] public async Task GetWithComplexType()