From 71c479fb5eebcdb936e6a42882db86bc60a0ca20 Mon Sep 17 00:00:00 2001 From: Claudia Murialdo Date: Wed, 4 Oct 2023 15:30:01 -0300 Subject: [PATCH] Cherry pick branch 'genexuslabs:httpreponse-on-rest-services' into beta --- .../GxClasses/Services/GxRestWrapper.cs | 6 +++--- .../Middleware/RestServiceTest.cs | 19 +++++++++++++++++-- .../test/DotNetCoreWebUnitTest/apps/append.cs | 4 ++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/dotnet/src/dotnetframework/GxClasses/Services/GxRestWrapper.cs b/dotnet/src/dotnetframework/GxClasses/Services/GxRestWrapper.cs index 50efa3b2f..b8fb2e3a8 100644 --- a/dotnet/src/dotnetframework/GxClasses/Services/GxRestWrapper.cs +++ b/dotnet/src/dotnetframework/GxClasses/Services/GxRestWrapper.cs @@ -136,14 +136,14 @@ public virtual Task MethodBodyExecute(object key) { innerMethod = this._serviceMethod; bodyParameters = PreProcessApiSdtParameter( _procWorker, innerMethod, bodyParameters, this._variableAlias); - } + } + ServiceHeaders(); Dictionary outputParameters = ReflectionHelper.CallMethod(_procWorker, innerMethod, bodyParameters, _gxContext); Dictionary 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) @@ -312,6 +312,7 @@ public virtual Task MethodUrlExecute(object key) string innerMethod = EXECUTE_METHOD; Dictionary outputParameters; Dictionary formatParameters = new Dictionary(); + ServiceHeaders(); if (!string.IsNullOrEmpty(_serviceMethodPattern)) { innerMethod = _serviceMethodPattern; @@ -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) diff --git a/dotnet/test/DotNetCoreWebUnitTest/Middleware/RestServiceTest.cs b/dotnet/test/DotNetCoreWebUnitTest/Middleware/RestServiceTest.cs index 639a2ef30..c2e3bec54 100644 --- a/dotnet/test/DotNetCoreWebUnitTest/Middleware/RestServiceTest.cs +++ b/dotnet/test/DotNetCoreWebUnitTest/Middleware/RestServiceTest.cs @@ -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; @@ -11,6 +13,7 @@ using GeneXus.Storage.GXAmazonS3; using GeneXus.Utils; using Microsoft.AspNetCore.Http; +using Microsoft.Net.Http.Headers; using Xunit; namespace xUnitTesting { @@ -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() { @@ -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] @@ -125,6 +128,18 @@ private async Task 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 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()); + } + } } diff --git a/dotnet/test/DotNetCoreWebUnitTest/apps/append.cs b/dotnet/test/DotNetCoreWebUnitTest/apps/append.cs index b8b85712e..b2e92227b 100644 --- a/dotnet/test/DotNetCoreWebUnitTest/apps/append.cs +++ b/dotnet/test/DotNetCoreWebUnitTest/apps/append.cs @@ -4,6 +4,7 @@ using GeneXus.Procedure; using System; using GX; +using GeneXus.Http.Server; namespace GeneXus.Programs.apps { @@ -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)); @@ -85,6 +87,7 @@ public override void initialize() AV8datetime = (DateTime.MinValue); /* GeneXus formulas. */ context.Gx_err = 0; + httpResponse = new GxHttpResponse(context); } private short AV18numeric1; private decimal AV19numeric2; @@ -92,6 +95,7 @@ public override void initialize() private string AV20character2; private DateTime AV8datetime; private Sdtrappo00b AV8rappo00B; + private GxHttpResponse httpResponse; } }