Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore headers with empty value if capability is enabled #6100

Merged
merged 4 commits into from May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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