Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxl01 committed Oct 30, 2024
1 parent dad210c commit f32226d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Reflection;
using System.Text;
using Volo.Abp.Http.Modeling;

namespace Volo.Abp.Http.Client;
public class AbpHttpClientExecuteHttpActionOptions
{
public Action<ActionApiDescriptionModel, HttpClient>? ExecuteHttpAction{ get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Castle.DynamicProxy;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using Volo.Abp.Content;
Expand Down Expand Up @@ -43,6 +44,8 @@ public class ClientProxyBase<TService> : ITransientDependency
protected ICurrentApiVersionInfo CurrentApiVersionInfo => LazyServiceProvider.LazyGetRequiredService<ICurrentApiVersionInfo>();
protected ILocalEventBus LocalEventBus => LazyServiceProvider.LazyGetRequiredService<ILocalEventBus>();

protected IOptions<AbpHttpClientExecuteHttpActionOptions> ExecuteHttpActionOptions => LazyServiceProvider.LazyGetRequiredService<IOptions<AbpHttpClientExecuteHttpActionOptions>>();

protected virtual async Task RequestAsync(string methodName, ClientProxyRequestTypeValue? arguments = null)
{
await RequestAsync(BuildHttpProxyClientProxyContext(methodName, arguments));
Expand Down Expand Up @@ -145,7 +148,10 @@ await ClientAuthenticator.Authenticate(

HttpResponseMessage response;
try
{
{
//Allows users to customize the timeout for remote methods of specific requests
ExecuteHttpActionOptions.Value.ExecuteHttpAction?.Invoke(requestContext.Action, client);

response = await client.SendAsync(
requestMessage,
HttpCompletionOption.ResponseHeadersRead /*this will buffer only the headers, the content will be used as a stream*/,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Http.Client;
Expand Down Expand Up @@ -60,5 +61,17 @@ public override void ConfigureServices(ServiceConfigurationContext context)
options.FormDataConverts.Add(typeof(List<GetParamsNameValue>), typeof(TestObjectToFormData));
options.PathConverts.Add(typeof(int), typeof(TestObjectToPath));
});

Configure<AbpHttpClientExecuteHttpActionOptions>(options =>
{
options.ExecuteHttpAction = (method, httpclient) =>
{
if (method.Name.Equals("TimeOutRequestAsync"))
{
httpclient.Timeout = TimeSpan.FromMilliseconds(2000);
}
};
});

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ public interface IRegularTestController
Task<int> DeleteByIdAsync(int id);

Task<string> AbortRequestAsync(CancellationToken cancellationToken = default);

Task<string> TimeOutRequestAsync();

}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ public async Task<string> AbortRequestAsync(CancellationToken cancellationToken
await Task.Delay(100, cancellationToken);
return "AbortRequestAsync";
}

[HttpGet]
[Route("timeout-request")]
public async Task<string> TimeOutRequestAsync()
{
await Task.Delay(5000);
return "timeout-request";
}
}

public class Car
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,16 @@ public async Task AbortRequestAsync()
var exception = await Assert.ThrowsAsync<AbpRemoteCallException>(async () => await _controller.AbortRequestAsync(cts.Token));
exception.InnerException.InnerException.InnerException.Message.ShouldBe("The client aborted the request.");
}

[Fact]
public async Task TimeOutRequestAsync()
{
//This cannot be executed successfully because the unit test does not use SocketsHttpHandler for request processing,
//so the desired results cannot be obtained locally. At the same time,
//I can achieve the expected results by using other services locally.

//var exception = await Assert.ThrowsAsync<TaskCanceledException>(async () => await _controller.TimeOutRequestAsync());
//exception.InnerException.InnerException.InnerException.Message.ShouldBe("The client aborted the request.");
await Task.CompletedTask;
}
}

0 comments on commit f32226d

Please sign in to comment.