Azure / Azure/azure-functions-dotnet-worker

IFunctionBindingsFeature is internal and pevent unit testing in middleware

Open
#2,540 5 comments 6 reactions 1 assignee Claimed by @brettsam View on GitHub
enhancement
Dominant language
C#
Stars
466
Forks
215
Avg merge
3d 10h
Merged PRs (30d)
7

Description

### Description

I'm currently writing a authentication middleware. When it identifies that the user bearer token is not valid it sets the response data to return a 401 response.

` public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
{
HttpRequestData? request = await context.GetHttpRequestDataAsync();

if (request is null)
{
await next(context);

return;
}

try
{
var authService = context.InstanceServices.GetRequiredService();

AuthorizedContext authorizedContext = await authService.Authorize(request);

context.Items.Add("AuthorizedContext", authorizedContext);

await next(context);
}
catch (HttpRequestException exception)
{
LogErrorMessage(request, exception);
context.GetInvocationResult().Value = request.CreateErrorResponse(exception);
}
catch (Exception exception)
{
LogErrorMessage(request, exception);
context.GetInvocationResult().Value = request.CreateErrorResponse(exception);
}
`

The function `GetInvocationResult` on context is a static function that calls another static function `GetBindings`. `GetBindings` executes this line of code: `context.Features.GetRequired();`. `GetRequired` is also a static function that calls `features.Get`

I would need to to create unit tests but I cannot mock static functions. I'd have to go mocking `features.Get` but it is impossible because IFunctionBindingsFeature is an internal interface.

I've been wondering if it is possible to open this up to allow unit testing possible. I want to be able to add unit tests when a user cannot be authenticated and check that the middleware returns the correct error.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.