This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add request value lookup binding (#3)
- Loading branch information
1 parent
1a4de2c
commit 1ed583d
Showing
27 changed files
with
1,073 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Request Value Lookup | ||
|
||
Get a value from one of multiple places in a request. Currently supports: | ||
|
||
- Query | ||
- Header | ||
|
||
## Attribute | ||
|
||
| Name | Type | Description | | ||
| -------- | ---------------------- | ----------------------------- | | ||
| Location | `RequestValueLocation` | Location to check for value | | ||
| Name | `string` | Priamary key name to look for | | ||
| Aliases | `string[]` | Alternate key names | | ||
|
||
## Example | ||
|
||
```csharp | ||
public static class VersionedHttpTrigger | ||
{ | ||
[FunctionName(nameof(VersionedHttpTrigger))] | ||
public static async Task<IActionResult> RunAsync( | ||
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "version")] | ||
HttpRequest req, ILogger log, [ | ||
RequestValue( | ||
Location = RequestValueLocation.Header | RequestValueLocation.Query, | ||
Name = "apiVersion", | ||
Aliases = new[] { "x-api-version" } | ||
)] | ||
string version | ||
) | ||
{ | ||
if (string.IsNullOrEmpty(version)) | ||
{ | ||
return new BadRequestResult(); | ||
} | ||
|
||
log.LogInformation("Triggered for version {Version}", version); | ||
return new OkObjectResult(version); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/Integration.FunctionApp.IntegrationTests/Functions/VersionedHttpTriggerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Integration.FunctionApp.Functions; | ||
using JoachimDalen.AzureFunctions.TestUtils.Attributes; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Integration.FunctionApp.IntegrationTests.Functions | ||
{ | ||
[TestClass] | ||
public class VersionedHttpTriggerTests : BaseFunctionTest | ||
{ | ||
[TestMethod] | ||
[StartFunctions(nameof(VersionedHttpTrigger))] | ||
public async Task VersionedHttpTrigger_ValueInQuery_ReturnsValue() | ||
{ | ||
var response = await Fixture.Client.GetAsync("/api/version?apiVersion=v1"); | ||
var responseBody = await response.Content.ReadAsStringAsync(); | ||
|
||
Assert.IsTrue(response.IsSuccessStatusCode); | ||
Assert.AreEqual("v1", responseBody); | ||
} | ||
|
||
[TestMethod] | ||
[StartFunctions(nameof(VersionedHttpTrigger))] | ||
public async Task VersionedHttpTrigger_ValueInHeader_ReturnsValue() | ||
{ | ||
var request = new HttpRequestMessage(HttpMethod.Get, "/api/version"); | ||
request.Headers.Add("x-api-version", "v2"); | ||
var response = await Fixture.Client.SendAsync(request); | ||
var responseBody = await response.Content.ReadAsStringAsync(); | ||
|
||
Assert.IsTrue(response.IsSuccessStatusCode); | ||
Assert.AreEqual("v2", responseBody); | ||
} | ||
|
||
[TestMethod] | ||
[StartFunctions(nameof(VersionedHttpTrigger))] | ||
public async Task VersionedHttpTrigger_ValueInQuery_ReturnsError() | ||
{ | ||
var response = await Fixture.Client.GetAsync("/api/version"); | ||
|
||
Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Integration.FunctionApp/Functions/VersionedHttpTrigger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Threading.Tasks; | ||
using System.Web.Http; | ||
using JoachimDalen.AzureFunctions.Extensions.Attributes; | ||
using JoachimDalen.AzureFunctions.Extensions.Models; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Azure.WebJobs; | ||
using Microsoft.Azure.WebJobs.Extensions.Http; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Integration.FunctionApp.Functions | ||
{ | ||
public static class VersionedHttpTrigger | ||
{ | ||
[FunctionName("VersionedHttpTrigger")] | ||
public static async Task<IActionResult> RunAsync( | ||
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "version")] | ||
HttpRequest req, ILogger log, [ | ||
RequestValue(Location = RequestValueLocation.Header | RequestValueLocation.Query, Name = "apiVersion", | ||
Aliases = new[] | ||
{ | ||
"x-api-version" | ||
}) | ||
] | ||
string version | ||
) | ||
{ | ||
if (string.IsNullOrEmpty(version)) | ||
{ | ||
return new BadRequestResult(); | ||
} | ||
|
||
log.LogInformation("Triggered for version {Version}", version); | ||
return new OkObjectResult(version); | ||
} | ||
} | ||
} |
6 changes: 5 additions & 1 deletion
6
src/Integration.FunctionApp/Models/MultipartRequestBodyData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
namespace Integration.FunctionApp.Models | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Integration.FunctionApp.Models | ||
{ | ||
public class MultipartRequestBodyData | ||
{ | ||
[Required] | ||
public string Username { get; set; } | ||
|
||
public string Email { get; set; } | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/JoachimDalen.AzureFunctions.Extensions.UnitTests/HttpRequestExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Net.Mime; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace JoachimDalen.AzureFunctions.Extensions.UnitTests | ||
{ | ||
[TestClass] | ||
public class HttpRequestExtensionsTests | ||
{ | ||
[DataTestMethod] | ||
[DataRow("form-data;name=\"file\"; filename=\"fileOne.png\"")] | ||
[DataRow("form-data;name=\"file\"; filename=fileOne.png")] | ||
public void GetEscapedContentDispositionFileName_Data_ReturnsCorrect(string contentDisposition) | ||
{ | ||
var contentDispositionHeaderValue = ContentDispositionHeaderValue.Parse(contentDisposition); | ||
Assert.AreEqual("fileOne.png", contentDispositionHeaderValue.GetEscapedContentDispositionFileName()); | ||
} | ||
|
||
[TestMethod] | ||
public async Task HasFiles_Files_ReturnsTrue() | ||
{ | ||
var bytes = Encoding.UTF8.GetBytes("Hello"); | ||
var multipart = new MultipartFormDataContent | ||
{ | ||
{new ByteArrayContent(bytes, 0, bytes.Length), "file", "greeting.txt"} | ||
}; | ||
|
||
var mp = (await multipart.ReadAsMultipartAsync()).Contents.First(); | ||
Assert.IsTrue(mp.HasFiles("file")); | ||
} | ||
|
||
[TestMethod] | ||
public async Task HasFiles_StringContent_ReturnsFalse() | ||
{ | ||
var multipart = new MultipartFormDataContent | ||
{ | ||
new StringContent("Hello", Encoding.UTF8, MediaTypeNames.Text.Plain) | ||
}; | ||
|
||
var mp = (await multipart.ReadAsMultipartAsync()).Contents.First(); | ||
Assert.IsFalse(mp.HasFiles("file")); | ||
} | ||
|
||
[TestMethod] | ||
public async Task HasData_JsonContent_ReturnsTrue() | ||
{ | ||
var multipart = new MultipartFormDataContent | ||
{ | ||
new StringContent("Hello", Encoding.UTF8, MediaTypeNames.Application.Json) | ||
}; | ||
|
||
var mp = (await multipart.ReadAsMultipartAsync()).Contents.First(); | ||
Assert.IsTrue(mp.HasData()); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ureFunctions.Extensions.UnitTests/JoachimDalen.AzureFunctions.Extensions.UnitTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" /> | ||
<PackageReference Include="coverlet.collector" Version="1.3.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\JoachimDalen.AzureFunctions.Extensions\JoachimDalen.AzureFunctions.Extensions.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.