Skip to content

Commit 75d32b0

Browse files
authored
Ignore headers with empty value if capability is enabled (#6100)
* Ignore headers with empty value if capability is enabled * nit * format fixes * logic error
1 parent fee8d84 commit 75d32b0

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/WebJobs.Script/Workers/Rpc/MessageExtensions/RpcMessageConversionExtensions.cs

+10
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ internal static TypedData ToRpcHttp(this HttpRequest request, ILogger logger, Ca
132132

133133
foreach (var pair in request.Headers)
134134
{
135+
if (ShouldIgnoreEmptyHeaderValues(capabilities) && string.IsNullOrEmpty(pair.Value.ToString()))
136+
{
137+
continue;
138+
}
139+
135140
http.Headers.Add(pair.Key.ToLowerInvariant(), pair.Value.ToString());
136141
}
137142

@@ -346,6 +351,11 @@ private static bool IsTypedDataCollectionSupported(Capabilities capabilities)
346351
return !string.IsNullOrEmpty(capabilities.GetCapabilityState(RpcWorkerConstants.TypedDataCollection));
347352
}
348353

354+
private static bool ShouldIgnoreEmptyHeaderValues(Capabilities capabilities)
355+
{
356+
return !string.IsNullOrEmpty(capabilities.GetCapabilityState(RpcWorkerConstants.IgnoreEmptyValuedRpcHttpHeaders));
357+
}
358+
349359
public static BindingInfo ToBindingInfo(this BindingMetadata bindingMetadata)
350360
{
351361
BindingInfo bindingInfo = new BindingInfo

src/WebJobs.Script/Workers/Rpc/RpcWorkerConstants.cs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static class RpcWorkerConstants
3535
public const string TypedDataCollection = "TypedDataCollection";
3636
public const string RpcHttpBodyOnly = "RpcHttpBodyOnly";
3737
public const string RpcHttpTriggerMetadataRemoved = "RpcHttpTriggerMetadataRemoved";
38+
public const string IgnoreEmptyValuedRpcHttpHeaders = "IgnoreEmptyValuedRpcHttpHeaders";
3839

3940
// Host Capabilites
4041
public const string V2Compatable = "V2Compatable";

test/WebJobs.Script.Tests/Workers/Rpc/RpcMessageConversionExtensionsTests.cs

+37
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,43 @@ public void HttpObjects_Query(string queryString, string[] expectedKeys, string[
160160
}
161161
}
162162

163+
[Theory]
164+
[InlineData(true, new string[] { "hello", "x-mx-key" }, new string[] { "world", "value" }, new string[] { "hello", "x-mx-key" }, new string[] { "world", "value" })]
165+
[InlineData(true, new string[] { "hello", "empty", "x-mx-key" }, new string[] { "world", "", "value" }, new string[] { "hello", "x-mx-key" }, new string[] { "world", "value" })] // Removes empty value query params
166+
[InlineData(false, new string[] { "hello", "x-mx-key" }, new string[] { "world", "value" }, new string[] { "hello", "x-mx-key" }, new string[] { "world", "value" })]
167+
[InlineData(false, new string[] { "hello", "empty", "x-mx-key" }, new string[] { "world", "", "value" }, new string[] { "hello", "empty", "x-mx-key" }, new string[] { "world", "", "value" })]
168+
169+
public void HttpObjects_Headers(bool ignoreEmptyValues, string[] headerKeys, string[] headerValues, string[] expectedKeys, string[] expectedValues)
170+
{
171+
var logger = MockNullLoggerFactory.CreateLogger();
172+
// Capability must be enabled
173+
var capabilities = new Capabilities(logger);
174+
175+
if (ignoreEmptyValues)
176+
{
177+
capabilities.UpdateCapabilities(new MapField<string, string>
178+
{
179+
{ RpcWorkerConstants.IgnoreEmptyValuedRpcHttpHeaders, "true" }
180+
});
181+
}
182+
183+
var headerDictionary = new HeaderDictionary();
184+
for (int i = 0; i < headerValues.Length; i++)
185+
{
186+
headerDictionary.Add(headerKeys[i], headerValues[i]);
187+
}
188+
189+
HttpRequest request = HttpTestHelpers.CreateHttpRequest("GET", $"http://localhost/api/httptrigger-scenarios", headerDictionary);
190+
191+
var rpcRequestObject = request.ToRpc(logger, capabilities);
192+
// Same key and value strings for each pair
193+
for (int i = 0; i < expectedKeys.Length; i++)
194+
{
195+
Assert.True(rpcRequestObject.Http.Headers.ContainsKey(expectedKeys[i]));
196+
Assert.Equal(expectedValues[i], rpcRequestObject.Http.Headers.GetValueOrDefault(expectedKeys[i]));
197+
}
198+
}
199+
163200
[Theory]
164201
[InlineData(BindingDirection.In, "blob", DataType.String)]
165202
[InlineData(BindingDirection.Out, "blob", DataType.Binary)]

0 commit comments

Comments
 (0)