Skip to content

Commit

Permalink
Add E2E tests
Browse files Browse the repository at this point in the history
  • Loading branch information
njqdev authored and fabiocav committed Jul 3, 2023
1 parent 9c726ca commit 11f7550
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
56 changes: 54 additions & 2 deletions test/E2ETests/E2EApps/E2EApp/Http/BasicHttpFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

using System.Net;
using System.Text.Json;
using System.Web;
using Microsoft.Azure.Functions.Worker;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -72,6 +71,59 @@ public class CallerName
return new MyResponse { Name = "Test" };
}

[Function(nameof(HelloWithNoResponse))]
public static Task HelloWithNoResponse(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)][FromBody] HttpRequestData req,
FunctionContext context)
{
var logger = context.GetLogger(nameof(HelloWithNoResponse));
logger.LogInformation($".NET Worker HTTP trigger function processed a request");

return Task.CompletedTask;
}

[Function(nameof(PocoFromBody))]
public static HttpResponseData PocoFromBody(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestData req,
[FromBody] CallerName caller,
FunctionContext context)
{
var logger = context.GetLogger(nameof(PocoFromBody));
logger.LogInformation(".NET Worker HTTP trigger function processed a request");

var response = req.CreateResponse(HttpStatusCode.OK);
response.WriteString($"Greetings {caller.Name}");
return response;
}

[Function(nameof(PocoBeforeRouteParameters))]
public static Task PocoBeforeRouteParameters(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "{region}/{category}/" + nameof(PocoBeforeRouteParameters))] [FromBody] CallerName caller,
string region,
string category,
FunctionContext context)
{
var logger = context.GetLogger(nameof(PocoBeforeRouteParameters));
logger.LogInformation(".NET Worker HTTP trigger function processed a request");
return Task.CompletedTask;
}

[Function(nameof(PocoAfterRouteParameters))]
public static HttpResponseData PocoAfterRouteParameters(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "{region}/{category}/" + nameof(PocoAfterRouteParameters))] HttpRequestData req,
string region,
string category,
[FromBody] CallerName caller,
FunctionContext context)
{
var logger = context.GetLogger(nameof(PocoAfterRouteParameters));
logger.LogInformation(".NET Worker HTTP trigger function processed a request");

var response = req.CreateResponse(HttpStatusCode.OK);
response.WriteString($"{region} {category} {caller.Name}");
return response;
}

public class MyResponse
{
public string Name { get; set; }
Expand Down
15 changes: 12 additions & 3 deletions test/E2ETests/E2EApps/E2EApp/Http/FailingHttpFunctions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Azure.Functions.Worker.Pipeline;
using Microsoft.Extensions.Logging;
using static Microsoft.Azure.Functions.Worker.E2EApp.BasicHttpFunctions;

namespace Microsoft.Azure.Functions.Worker.E2EApp
{
Expand All @@ -19,6 +18,16 @@ public static class FailingHttpFunctions
var logger = context.GetLogger(nameof(ExceptionFunction));
logger.LogInformation(".NET Worker HTTP trigger function processed a request");
throw new Exception("This should never succeed!");
}

[Function(nameof(PocoWithoutBindingSource))]
public static HttpResponseData PocoWithoutBindingSource(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] CallerName caller,
FunctionContext context)
{
var logger = context.GetLogger(nameof(PocoWithoutBindingSource));
logger.LogInformation(".NET Worker HTTP trigger function processed a request");
throw new Exception("This should never succeed!");
}
}
}
5 changes: 5 additions & 0 deletions test/E2ETests/E2ETests/Fixtures/FunctionAppFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ public async Task InitializeAsync()
_funcProcess.StartInfo.ArgumentList.Add("HelloFromQuery");
_funcProcess.StartInfo.ArgumentList.Add("HelloFromJsonBody");
_funcProcess.StartInfo.ArgumentList.Add("HelloUsingPoco");
_funcProcess.StartInfo.ArgumentList.Add("HelloWithNoResponse");
_funcProcess.StartInfo.ArgumentList.Add("PocoFromBody");
_funcProcess.StartInfo.ArgumentList.Add("PocoBeforeRouteParameters");
_funcProcess.StartInfo.ArgumentList.Add("PocoAfterRouteParameters");
_funcProcess.StartInfo.ArgumentList.Add("ExceptionFunction");
_funcProcess.StartInfo.ArgumentList.Add("PocoWithoutBindingSource");
}

await CosmosDBHelpers.TryCreateDocumentCollectionsAsync(_logger);
Expand Down
25 changes: 25 additions & 0 deletions test/E2ETests/E2ETests/HttpEndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public HttpEndToEndTests(FunctionAppFixture fixture)
[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("HelloWithNoResponse", "", HttpStatusCode.NoContent, "")]
[InlineData("ExceptionFunction", "", HttpStatusCode.InternalServerError, "")]
[InlineData("HelloFromQuery", "", HttpStatusCode.BadRequest, "")]
public async Task HttpTriggerTests(string functionName, string queryString, HttpStatusCode expectedStatusCode, string expectedMessage)
Expand Down Expand Up @@ -55,6 +56,30 @@ public async Task HttpTriggerTestsMediaTypeDoNotMatter(string functionName, stri
Assert.Equal(expectedBody, responseBody);
}

[Theory]
[InlineData("PocoFromBody", "", "{ \"Name\": \"John\" }", "application/json", HttpStatusCode.OK, "Greetings John")]
[InlineData("PocoBeforeRouteParameters", "eu/caller/", "{ \"Name\": \"b\" }", "application/json", HttpStatusCode.NoContent, "")]
[InlineData("PocoAfterRouteParameters", "eu/caller/", "{ \"Name\": \"c\" }", "application/json", HttpStatusCode.OK, "eu caller c")]
public async Task HttpTriggerTests_PocoFromBody(string functionName, string route, string body, string mediaType, HttpStatusCode expectedStatusCode, string expectedBody)
{
HttpResponseMessage response = await HttpHelpers.InvokeHttpTriggerWithBody($"{route}{functionName}", body, expectedStatusCode, mediaType);
string responseBody = await response.Content.ReadAsStringAsync();

Assert.Equal(expectedStatusCode, response.StatusCode);
Assert.Equal(expectedBody, responseBody);
}


[Fact]
public async Task HttpTriggerTests_PocoWithoutBindingSource()
{
const HttpStatusCode expectedStatusCode = HttpStatusCode.InternalServerError;

HttpResponseMessage response = await HttpHelpers.InvokeHttpTriggerWithBody("PocoWithoutBindingSource", "{ \"Name\": \"John\" }", expectedStatusCode, "application/json");

Assert.Equal(expectedStatusCode, response.StatusCode);
}

[Fact]
public async Task HttpTriggerTestsPocoResult()
{
Expand Down

0 comments on commit 11f7550

Please sign in to comment.