Skip to content

Commit

Permalink
Cherry pick branch 'genexuslabs:httpreponse-on-rest-services' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiamurialdo authored and genexusbot committed Oct 4, 2023
1 parent d78e2ee commit 71c479f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ public virtual Task MethodBodyExecute(object key)
{
innerMethod = this._serviceMethod;
bodyParameters = PreProcessApiSdtParameter( _procWorker, innerMethod, bodyParameters, this._variableAlias);
}
}
ServiceHeaders();
Dictionary<string, object> outputParameters = ReflectionHelper.CallMethod(_procWorker, innerMethod, bodyParameters, _gxContext);
Dictionary<string, string> formatParameters = ReflectionHelper.ParametersFormat(_procWorker, innerMethod);
setWorkerStatus(_procWorker);
_procWorker.cleanup();
RestProcess(_procWorker, outputParameters);
wrapped = GetWrappedStatus(_procWorker, wrapped, outputParameters, outputParameters.Count);
ServiceHeaders();
return Serialize(outputParameters, formatParameters, wrapped);
}
catch (Exception e)
Expand Down Expand Up @@ -312,6 +312,7 @@ public virtual Task MethodUrlExecute(object key)
string innerMethod = EXECUTE_METHOD;
Dictionary<string, object> outputParameters;
Dictionary<string, string> formatParameters = new Dictionary<string, string>();
ServiceHeaders();
if (!string.IsNullOrEmpty(_serviceMethodPattern))
{
innerMethod = _serviceMethodPattern;
Expand All @@ -332,7 +333,6 @@ public virtual Task MethodUrlExecute(object key)
RestProcess(_procWorker, outputParameters);
bool wrapped = false;
wrapped = GetWrappedStatus(_procWorker, wrapped, outputParameters, parCount);
ServiceHeaders();
return Serialize(outputParameters, formatParameters, wrapped);
}
catch (Exception e)
Expand Down
19 changes: 17 additions & 2 deletions dotnet/test/DotNetCoreWebUnitTest/Middleware/RestServiceTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;
Expand All @@ -11,6 +13,7 @@
using GeneXus.Storage.GXAmazonS3;
using GeneXus.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;
using Xunit;
namespace xUnitTesting
{
Expand All @@ -22,7 +25,7 @@ public RestServiceTest() : base()
ClassLoader.FindType("apps.saveimage", "GeneXus.Programs.apps", "saveimage", Assembly.GetExecutingAssembly(), true);//Force loading assembly for saveimage procedure
server.AllowSynchronousIO = true;
}

const string serviceBodyResponse = "OK";
[Fact]
public async Task TestMultiCall()
{
Expand All @@ -33,7 +36,7 @@ public async Task TestMultiCall()
response.EnsureSuccessStatusCode();
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
string responseBody = await response.Content.ReadAsStringAsync();
Assert.Empty(responseBody);
Assert.Equal($"{serviceBodyResponse}{serviceBodyResponse}{serviceBodyResponse}",responseBody);
}

[Fact]
Expand Down Expand Up @@ -125,6 +128,18 @@ private async Task<HttpResponseMessage> RunController(HttpClient client)
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); //When failed, turn on log.config to see server side error.
return response;
}
string ACCESS_CONTROL_MAX_AGE_HEADER = "86400";
[Fact]
public async Task TestHttpResponseOnRestService()
{
HttpClient client = server.CreateClient();
HttpResponseMessage response = await RunController(client);
bool headerAllow = response.Headers.TryGetValues(HeaderNames.AccessControlMaxAge, out IEnumerable<string> values);
Assert.True(headerAllow, $"The {HeaderNames.AccessControlMaxAge} header was not configured by the REST service.");
if (headerAllow)
Assert.Equal(ACCESS_CONTROL_MAX_AGE_HEADER, values.FirstOrDefault());
}

}

}
4 changes: 4 additions & 0 deletions dotnet/test/DotNetCoreWebUnitTest/apps/append.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using GeneXus.Procedure;
using System;
using GX;
using GeneXus.Http.Server;

namespace GeneXus.Programs.apps
{
Expand Down Expand Up @@ -49,6 +50,7 @@ void executePrivate()
{
/* GeneXus formulas */
/* Output device settings */
httpResponse.AddString("OK");
AV20character2 = StringUtil.Str((decimal)(AV18numeric1), 10, 0) + AV17Character + StringUtil.Str(AV19numeric2, 10, 0) + ClientInformation.Id.ToString();
AV8datetime = DateTimeUtil.ResetTime(context.localUtil.YMDToD(2022, 5, 24));
AV8rappo00B.gxTpr_Rapdaa = DateTimeUtil.ResetTime(context.localUtil.YMDToD(2022, 5, 24));
Expand Down Expand Up @@ -85,13 +87,15 @@ public override void initialize()
AV8datetime = (DateTime.MinValue);
/* GeneXus formulas. */
context.Gx_err = 0;
httpResponse = new GxHttpResponse(context);
}
private short AV18numeric1;
private decimal AV19numeric2;
private string AV17Character;
private string AV20character2;
private DateTime AV8datetime;
private Sdtrappo00b AV8rappo00B;
private GxHttpResponse httpResponse;
}

}

0 comments on commit 71c479f

Please sign in to comment.