Skip to content

Commit

Permalink
Add query as property to HttpRequestData (#1408)
Browse files Browse the repository at this point in the history
* added query property to http request

* extract value, account for multiple queries of same key

* does accessor throw if null?

* added test case

* use httputility parsing rather than parsing ourselves

* Update release_notes.md

* usings

* unnecessary usings

* build error

* Query get method virtual rather than abstract

* avoid unnecessary calls to ParseQueryString

* fixes
  • Loading branch information
madelinegordon committed Mar 17, 2023
1 parent d7baa52 commit 37070d8
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
<!-- Please add your release notes in the following format:
- My change description (#PR/#issue)
-->
- Add query as property to HttpRequestData (#1408)
9 changes: 9 additions & 0 deletions src/DotNetWorker.Core/Http/HttpRequestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Security.Claims;
using System.Web;

namespace Microsoft.Azure.Functions.Worker.Http
{
Expand All @@ -13,6 +15,8 @@ namespace Microsoft.Azure.Functions.Worker.Http
/// </summary>
public abstract class HttpRequestData
{
private NameValueCollection? _query;

/// <summary>
/// Initializes a new instance of the <see cref="HttpRequestData"/> class.
/// </summary>
Expand Down Expand Up @@ -62,5 +66,10 @@ public HttpRequestData(FunctionContext functionContext)
/// </summary>
/// <returns>The response instance.</returns>
public abstract HttpResponseData CreateResponse();

/// <summary>
/// Gets the <see cref="NameValueCollection"/> containing the request query.
/// </summary>
public virtual NameValueCollection Query => _query ??= HttpUtility.ParseQueryString(Url.Query);
}
}
5 changes: 3 additions & 2 deletions test/E2ETests/E2EApps/E2EApp/Http/BasicHttpFunctions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Net;
using System.Net;
using System.Text.Json;
using System.Web;
using Microsoft.Azure.Functions.Worker;
Expand All @@ -19,7 +19,8 @@ public static HttpResponseData HelloFromQuery(
{
var logger = context.GetLogger(nameof(HelloFromQuery));
logger.LogInformation(".NET Worker HTTP trigger function processed a request");
var queryName = HttpUtility.ParseQueryString(req.Url.Query)["name"];

var queryName = req.Query["name"];

if (!string.IsNullOrEmpty(queryName))
{
Expand Down
3 changes: 1 addition & 2 deletions test/E2ETests/E2EApps/E2EApp/Queue/QueueTestFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public HttpAndQueue QueueOutputPocoList(
{
context.GetLogger<QueueTestFunctions>().LogInformation(".NET HTTP trigger processed a request.");

var query = QueryHelpers.ParseQuery(request.Url.Query);
string queueMessageId = query["queueMessageId"];
string queueMessageId = request.Query["queueMessageId"];
var outputItems = new List<TestData>();

if (queueMessageId != null)
Expand Down
1 change: 1 addition & 0 deletions test/E2ETests/E2ETests/HttpEndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public HttpEndToEndTests(FunctionAppFixture fixture)
[InlineData("HelloFromQuery", "?name=Test", HttpStatusCode.OK, "Hello Test")]
[InlineData("HelloFromQuery", "?name=John&lastName=Doe", HttpStatusCode.OK, "Hello John")]
[InlineData("HelloFromQuery", "?emptyProperty=&name=Jane", HttpStatusCode.OK, "Hello Jane")]
[InlineData("HelloFromQuery", "?name=John&name=Jane", HttpStatusCode.OK, "Hello John,Jane")]
[InlineData("ExceptionFunction", "", HttpStatusCode.InternalServerError, "")]
[InlineData("HelloFromQuery", "", HttpStatusCode.BadRequest, "")]
public async Task HttpTriggerTests(string functionName, string queryString, HttpStatusCode expectedStatusCode, string expectedMessage)
Expand Down

0 comments on commit 37070d8

Please sign in to comment.