Browse Source

feat(wrapper): 增加一个标头指示是否需要处理返回结果

pull/411/head
cKey 4 years ago
parent
commit
8f5b022cbe
  1. 2
      aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/AbpHttpWrapConsts.cs
  2. 5
      aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/WrapResultChecker.cs
  3. 10
      aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController_Tests.cs
  4. 21
      aspnet-core/tests/LINGYUN.Abp.AspNetCore.Tests/LINGYUN/Abp/AspNetCore/AbpAspNetCoreTestBase.cs

2
aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/AbpHttpWrapConsts.cs

@ -3,5 +3,7 @@
public static class AbpHttpWrapConsts
{
public const string AbpWrapResult = "_AbpWrapResult";
public const string AbpDontWrapResult = "_AbpDontWrapResult";
}
}

5
aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/WrapResultChecker.cs

@ -44,6 +44,11 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Wrapper
protected virtual bool CheckForBase(FilterContext context)
{
if (context.HttpContext.Request.Headers.ContainsKey(AbpHttpWrapConsts.AbpDontWrapResult))
{
return false;
}
if (context.ActionDescriptor is ControllerActionDescriptor descriptor)
{
if (!context.ActionDescriptor.HasObjectResult())

10
aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController_Tests.cs

@ -11,6 +11,16 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Results
{
public class WrapResultController_Tests : AbpAspNetCoreMvcTestBase
{
[Fact]
public async Task Should_Return_Not_Wrap_Result_For_Request_Headers()
{
RequestHeaders.Add(AbpHttpWrapConsts.AbpDontWrapResult, "true");
var result = await GetResponseAsObjectAsync<TestResultObject>("/api/wrap-result-test/wrap");
result.ShouldNotBeNull();
result.Name.ShouldBe("Wrap");
}
[Fact]
public async Task Should_Return_Not_Wrap_Result_For_Abp_Remote_Stream_Content()
{

21
aspnet-core/tests/LINGYUN.Abp.AspNetCore.Tests/LINGYUN/Abp/AspNetCore/AbpAspNetCoreTestBase.cs

@ -1,6 +1,8 @@
using Microsoft.Net.Http.Headers;
using Shouldly;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Json;
@ -17,15 +19,22 @@ namespace LINGYUN.Abp.AspNetCore
public abstract class AbpAspNetCoreTestBase<TStartup> : AbpAspNetCoreIntegratedTestBase<TStartup>
where TStartup : class
{
protected virtual async Task<T> GetResponseAsObjectAsync<T>(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK)
protected IDictionary<string, string> RequestHeaders { get; }
protected AbpAspNetCoreTestBase()
{
RequestHeaders = new Dictionary<string, string>();
}
protected virtual async Task<T> GetResponseAsObjectAsync<T>(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK, bool xmlHttpRequest = false)
{
var strResponse = await GetResponseAsStringAsync(url, expectedStatusCode);
var strResponse = await GetResponseAsStringAsync(url, expectedStatusCode, xmlHttpRequest);
return JsonSerializer.Deserialize<T>(strResponse, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
}
protected virtual async Task<string> GetResponseAsStringAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK)
protected virtual async Task<string> GetResponseAsStringAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK, bool xmlHttpRequest = false)
{
using (var response = await GetResponseAsync(url, expectedStatusCode))
using (var response = await GetResponseAsync(url, expectedStatusCode, xmlHttpRequest))
{
return await response.Content.ReadAsStringAsync();
}
@ -40,6 +49,10 @@ namespace LINGYUN.Abp.AspNetCore
{
requestMessage.Headers.Add(HeaderNames.XRequestedWith, "XMLHttpRequest");
}
foreach (var header in RequestHeaders)
{
requestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
var response = await Client.SendAsync(requestMessage);
response.StatusCode.ShouldBe(expectedStatusCode);
return response;

Loading…
Cancel
Save