Skip to content

Commit

Permalink
[CustomHandler]Send query params as JObject and Identities as JArray (A…
Browse files Browse the repository at this point in the history
  • Loading branch information
pragnagopa authored Sep 11, 2020
1 parent c4100a7 commit 1ea19bf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
5 changes: 3 additions & 2 deletions release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
-->
- Update Python Worker to 1.1.5 [Release Note](https://github.com/Azure/azure-functions-python-worker/releases/tag/1.1.5)
- Update Python Library to 1.3.1 [Release Note](https://github.com/Azure/azure-functions-python-library/releases/tag/1.3.1)
- [CustomHandler][Breaking] If enableForwardingHttpRequest is false, http output binding response is expected to be a valid Json object with following optional fields :
- **[BreakingChange]**[CustomHandler] If enableForwardingHttpRequest is false, http output binding response is expected to be a valid Json object with following optional fields :
`{
"statusCode" : "",
"status" : "",
"body": "",
"headers" : {}
}`
Exception is thrown if HttpOutputBindingResponse is not valid Json.
- **[BreakingChange]**[CustomHandler]Send query params as JObject and Identities as JArray PR #6621

**Release sprint:** Sprint 84
[ [bugs](https://github.com/Azure/azure-functions-host/issues?q=is%3Aissue+milestone%3A%22Functions+Sprint+84%22+label%3Abug+is%3Aclosed) | [features](https://github.com/Azure/azure-functions-host/issues?q=is%3Aissue+milestone%3A%22Functions+Sprint+84%22+label%3Afeature+is%3Aclosed) ]
[ [bugs](https://github.com/Azure/azure-functions-host/issues?q=is%3Aissue+milestone%3A%22Functions+Sprint+84%22+label%3Abug+is%3Aclosed) | [features](https://github.com/Azure/azure-functions-host/issues?q=is%3Aissue+milestone%3A%22Functions+Sprint+84%22+label%3Afeature+is%3Aclosed) ]
12 changes: 6 additions & 6 deletions src/WebJobs.Script/Extensions/HttpRequestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public static async Task<JObject> GetRequestAsJObject(this HttpRequest request)
jObjectHttp["Method"] = request.Method.ToString();
if (request.Query != null)
{
jObjectHttp["Query"] = request.GetQueryCollectionAsString();
jObjectHttp["Query"] = request.GetQueryCollectionAsJObject();
}
if (request.Headers != null)
{
Expand All @@ -179,7 +179,7 @@ public static async Task<JObject> GetRequestAsJObject(this HttpRequest request)

if (request.HttpContext?.User?.Identities != null)
{
jObjectHttp["Identities"] = GetUserIdentitiesAsString(request.HttpContext.User.Identities);
jObjectHttp["Identities"] = GetUserIdentitiesAsJArray(request.HttpContext.User.Identities);
}

// parse request body as content-type
Expand All @@ -198,9 +198,9 @@ public static async Task<JObject> GetRequestAsJObject(this HttpRequest request)
return jObjectHttp;
}

internal static string GetQueryCollectionAsString(this HttpRequest request)
internal static JObject GetQueryCollectionAsJObject(this HttpRequest request)
{
return JsonConvert.SerializeObject(request.GetQueryCollectionAsDictionary());
return JObject.FromObject(request.GetQueryCollectionAsDictionary());
}

internal static IDictionary<string, string> GetQueryCollectionAsDictionary(this HttpRequest request)
Expand All @@ -214,9 +214,9 @@ internal static IDictionary<string, string> GetQueryCollectionAsDictionary(this
return queryParamsDictionary;
}

internal static string GetUserIdentitiesAsString(IEnumerable<ClaimsIdentity> claimsIdentities)
internal static JArray GetUserIdentitiesAsJArray(IEnumerable<ClaimsIdentity> claimsIdentities)
{
return JsonConvert.SerializeObject(claimsIdentities, new JsonSerializerSettings
return JArray.FromObject(claimsIdentities, new JsonSerializer
{
// Claims property in Identities had circular reference to property 'Subject'
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
Expand Down
15 changes: 11 additions & 4 deletions test/WebJobs.Script.Tests/Extensions/HttpRequestExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,23 @@ public void IsAppServiceInternalRequest_ReturnsExpectedResult()
}

[Fact]
public void ConvertUserIdentitiesToString_RemovesCircularReference()
public void ConvertUserIdentitiesToJArray_RemovesCircularReference()
{
string expectedUserIdentities = "[{\"AuthenticationType\":\"TestAuthType\",\"IsAuthenticated\":true";
IIdentity identity = new TestIdentity();
Claim claim = new Claim("authlevel", "admin", "test", "LOCAL AUTHORITY", "LOCAL AUTHORITY");
List<Claim> claims = new List<Claim>() { claim };
ClaimsIdentity claimsIdentity = new ClaimsIdentity(identity, claims);
List<ClaimsIdentity> claimsIdentities = new List<ClaimsIdentity>() { claimsIdentity };
string userIdentitiesString = HttpRequestExtensions.GetUserIdentitiesAsString(claimsIdentities);
Assert.Contains(expectedUserIdentities, userIdentitiesString);
var userIdentitiesString = HttpRequestExtensions.GetUserIdentitiesAsJArray(claimsIdentities);
Assert.Contains("TestAuthType", userIdentitiesString[0]["AuthenticationType"].ToString());
}

[Fact]
public void GetQueryCollectionAsJObject_Expected()
{
var testHttpRequest = HttpWorkerTestUtilities.GetTestHttpRequest();
var query = testHttpRequest.GetQueryCollectionAsJObject();
Assert.Equal("Ink And Toner", query["name"]);
}

[Fact]
Expand Down

0 comments on commit 1ea19bf

Please sign in to comment.