Skip to content

Commit badfcd6

Browse files
Send service headers prior to invoking the execute method to address cases where the service writes directly to the response using the HttpResponse data type.
1 parent 692396a commit badfcd6

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

dotnet/src/dotnetframework/GxClasses/Services/GxRestWrapper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ public virtual Task MethodBodyExecute(object key)
136136
{
137137
innerMethod = this._serviceMethod;
138138
bodyParameters = PreProcessApiSdtParameter( _procWorker, innerMethod, bodyParameters, this._variableAlias);
139-
}
139+
}
140+
ServiceHeaders();
140141
Dictionary<string, object> outputParameters = ReflectionHelper.CallMethod(_procWorker, innerMethod, bodyParameters, _gxContext);
141142
Dictionary<string, string> formatParameters = ReflectionHelper.ParametersFormat(_procWorker, innerMethod);
142143
setWorkerStatus(_procWorker);
143144
_procWorker.cleanup();
144145
RestProcess(_procWorker, outputParameters);
145146
wrapped = GetWrappedStatus(_procWorker, wrapped, outputParameters, outputParameters.Count);
146-
ServiceHeaders();
147147
return Serialize(outputParameters, formatParameters, wrapped);
148148
}
149149
catch (Exception e)
@@ -312,6 +312,7 @@ public virtual Task MethodUrlExecute(object key)
312312
string innerMethod = EXECUTE_METHOD;
313313
Dictionary<string, object> outputParameters;
314314
Dictionary<string, string> formatParameters = new Dictionary<string, string>();
315+
ServiceHeaders();
315316
if (!string.IsNullOrEmpty(_serviceMethodPattern))
316317
{
317318
innerMethod = _serviceMethodPattern;
@@ -332,7 +333,6 @@ public virtual Task MethodUrlExecute(object key)
332333
RestProcess(_procWorker, outputParameters);
333334
bool wrapped = false;
334335
wrapped = GetWrappedStatus(_procWorker, wrapped, outputParameters, parCount);
335-
ServiceHeaders();
336336
return Serialize(outputParameters, formatParameters, wrapped);
337337
}
338338
catch (Exception e)

dotnet/test/DotNetCoreWebUnitTest/Middleware/RestServiceTest.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Net.Http;
6+
using System.Net.Http.Headers;
57
using System.Reflection;
68
using System.Text.Json;
79
using System.Threading.Tasks;
@@ -11,6 +13,7 @@
1113
using GeneXus.Storage.GXAmazonS3;
1214
using GeneXus.Utils;
1315
using Microsoft.AspNetCore.Http;
16+
using Microsoft.Net.Http.Headers;
1417
using Xunit;
1518
namespace xUnitTesting
1619
{
@@ -22,7 +25,7 @@ public RestServiceTest() : base()
2225
ClassLoader.FindType("apps.saveimage", "GeneXus.Programs.apps", "saveimage", Assembly.GetExecutingAssembly(), true);//Force loading assembly for saveimage procedure
2326
server.AllowSynchronousIO = true;
2427
}
25-
28+
const string serviceBodyResponse = "OK";
2629
[Fact]
2730
public async Task TestMultiCall()
2831
{
@@ -33,7 +36,7 @@ public async Task TestMultiCall()
3336
response.EnsureSuccessStatusCode();
3437
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
3538
string responseBody = await response.Content.ReadAsStringAsync();
36-
Assert.Empty(responseBody);
39+
Assert.Equal($"{serviceBodyResponse}{serviceBodyResponse}{serviceBodyResponse}",responseBody);
3740
}
3841

3942
[Fact]
@@ -125,6 +128,18 @@ private async Task<HttpResponseMessage> RunController(HttpClient client)
125128
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); //When failed, turn on log.config to see server side error.
126129
return response;
127130
}
131+
string ACCESS_CONTROL_MAX_AGE_HEADER = "86400";
132+
[Fact]
133+
public async Task TestHttpResponseOnRestService()
134+
{
135+
HttpClient client = server.CreateClient();
136+
HttpResponseMessage response = await RunController(client);
137+
bool headerAllow = response.Headers.TryGetValues(HeaderNames.AccessControlMaxAge, out IEnumerable<string> values);
138+
Assert.True(headerAllow, $"The {HeaderNames.AccessControlMaxAge} header was not configured by the REST service.");
139+
if (headerAllow)
140+
Assert.Equal(ACCESS_CONTROL_MAX_AGE_HEADER, values.FirstOrDefault());
141+
}
142+
128143
}
129144

130145
}

dotnet/test/DotNetCoreWebUnitTest/apps/append.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using GeneXus.Procedure;
55
using System;
66
using GX;
7+
using GeneXus.Http.Server;
78

89
namespace GeneXus.Programs.apps
910
{
@@ -49,6 +50,7 @@ void executePrivate()
4950
{
5051
/* GeneXus formulas */
5152
/* Output device settings */
53+
httpResponse.AddString("OK");
5254
AV20character2 = StringUtil.Str((decimal)(AV18numeric1), 10, 0) + AV17Character + StringUtil.Str(AV19numeric2, 10, 0) + ClientInformation.Id.ToString();
5355
AV8datetime = DateTimeUtil.ResetTime(context.localUtil.YMDToD(2022, 5, 24));
5456
AV8rappo00B.gxTpr_Rapdaa = DateTimeUtil.ResetTime(context.localUtil.YMDToD(2022, 5, 24));
@@ -85,13 +87,15 @@ public override void initialize()
8587
AV8datetime = (DateTime.MinValue);
8688
/* GeneXus formulas. */
8789
context.Gx_err = 0;
90+
httpResponse = new GxHttpResponse(context);
8891
}
8992
private short AV18numeric1;
9093
private decimal AV19numeric2;
9194
private string AV17Character;
9295
private string AV20character2;
9396
private DateTime AV8datetime;
9497
private Sdtrappo00b AV8rappo00B;
98+
private GxHttpResponse httpResponse;
9599
}
96100

97101
}

0 commit comments

Comments
 (0)