Added Update test for PersonAppServiceClientProxy_Tests

pull/112/head
Halil İbrahim Kalkan 7 years ago
parent c8301f37c1
commit 506be99144

@ -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);

@ -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<string, object> methodArguments, IJsonSerializer jsonSerializer)
[CanBeNull]
public static HttpContent BuildContent(ActionApiDescriptionModel action,IReadOnlyDictionary<string, object> 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<string, object> 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<string, object> 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();
}
}
}

@ -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();

@ -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()

Loading…
Cancel
Save