Skip to content

Commit

Permalink
Ignore headers with empty value if capability is enabled (#6100)
Browse files Browse the repository at this point in the history
* Ignore headers with empty value if capability is enabled

* nit

* format fixes

* logic error
  • Loading branch information
mhoeger authored May 28, 2020
1 parent fee8d84 commit 75d32b0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ internal static TypedData ToRpcHttp(this HttpRequest request, ILogger logger, Ca

foreach (var pair in request.Headers)
{
if (ShouldIgnoreEmptyHeaderValues(capabilities) && string.IsNullOrEmpty(pair.Value.ToString()))
{
continue;
}

http.Headers.Add(pair.Key.ToLowerInvariant(), pair.Value.ToString());
}

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

private static bool ShouldIgnoreEmptyHeaderValues(Capabilities capabilities)
{
return !string.IsNullOrEmpty(capabilities.GetCapabilityState(RpcWorkerConstants.IgnoreEmptyValuedRpcHttpHeaders));
}

public static BindingInfo ToBindingInfo(this BindingMetadata bindingMetadata)
{
BindingInfo bindingInfo = new BindingInfo
Expand Down
1 change: 1 addition & 0 deletions src/WebJobs.Script/Workers/Rpc/RpcWorkerConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static class RpcWorkerConstants
public const string TypedDataCollection = "TypedDataCollection";
public const string RpcHttpBodyOnly = "RpcHttpBodyOnly";
public const string RpcHttpTriggerMetadataRemoved = "RpcHttpTriggerMetadataRemoved";
public const string IgnoreEmptyValuedRpcHttpHeaders = "IgnoreEmptyValuedRpcHttpHeaders";

// Host Capabilites
public const string V2Compatable = "V2Compatable";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,43 @@ public void HttpObjects_Query(string queryString, string[] expectedKeys, string[
}
}

[Theory]
[InlineData(true, new string[] { "hello", "x-mx-key" }, new string[] { "world", "value" }, new string[] { "hello", "x-mx-key" }, new string[] { "world", "value" })]
[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
[InlineData(false, new string[] { "hello", "x-mx-key" }, new string[] { "world", "value" }, new string[] { "hello", "x-mx-key" }, new string[] { "world", "value" })]
[InlineData(false, new string[] { "hello", "empty", "x-mx-key" }, new string[] { "world", "", "value" }, new string[] { "hello", "empty", "x-mx-key" }, new string[] { "world", "", "value" })]

public void HttpObjects_Headers(bool ignoreEmptyValues, string[] headerKeys, string[] headerValues, string[] expectedKeys, string[] expectedValues)
{
var logger = MockNullLoggerFactory.CreateLogger();
// Capability must be enabled
var capabilities = new Capabilities(logger);

if (ignoreEmptyValues)
{
capabilities.UpdateCapabilities(new MapField<string, string>
{
{ RpcWorkerConstants.IgnoreEmptyValuedRpcHttpHeaders, "true" }
});
}

var headerDictionary = new HeaderDictionary();
for (int i = 0; i < headerValues.Length; i++)
{
headerDictionary.Add(headerKeys[i], headerValues[i]);
}

HttpRequest request = HttpTestHelpers.CreateHttpRequest("GET", $"http://localhost/api/httptrigger-scenarios", headerDictionary);

var rpcRequestObject = request.ToRpc(logger, capabilities);
// Same key and value strings for each pair
for (int i = 0; i < expectedKeys.Length; i++)
{
Assert.True(rpcRequestObject.Http.Headers.ContainsKey(expectedKeys[i]));
Assert.Equal(expectedValues[i], rpcRequestObject.Http.Headers.GetValueOrDefault(expectedKeys[i]));
}
}

[Theory]
[InlineData(BindingDirection.In, "blob", DataType.String)]
[InlineData(BindingDirection.Out, "blob", DataType.Binary)]
Expand Down

0 comments on commit 75d32b0

Please sign in to comment.