wigmorewelsh / FunctionTestHost

TestHost for Azure Isolated Functions
Mozilla Public License 2.0
10 stars 1 forks source link

Is there a way to consume HTTP endpoints with custom routes and HTTP headers? #35

Open plamber opened 7 months ago

plamber commented 7 months ago

Hi,

Thank you very much for your dedicated work. I greatly appreciate your efforts. While exploring the integration of the solution into our project, I encountered challenges in addressing two specific use cases.

  1. Firstly, we utilize an Azure Middleware responsible for validating Bearer tokens for authentication and authorization. Unfortunately, I haven't been able to identify a method to invoke an HTTP function while simultaneously passing header values required for token validation.

  2. Additionally, we have functions featuring custom routes, necessitating the ability to call an endpoint based on the route rather than the function name. I am uncertain whether this particular use case is supported by the current solution.

For instance, we have a function with a custom route defined as follows:

public async Task<MailNotificationResponse> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "v1/{itemId}")] HttpRequestData req, string itemId)

Thank you once again for your valuable feedback and support.

Cheers, Patrick

wigmorewelsh commented 7 months ago

Hi Patrick

Your right neither are supported at the moment. But aren't large to implement.

I assume your auth middleware is a typical middleware running in the function app? This should just be a case of extending the api to support passing in headers and flowing them to the function.

Supporting dynamic routes will need a rethink around routing inside the host. Again not a huge change.

I'm putting together a list of up for grab items as well as a readme detailing what's supported, will add these on the list.

Regards Tim

plamber commented 7 months ago

Hi @wigmorewelsh, Thank you for the feedback.

The middleware is declared like this:

public class AuthenticationMiddleware : IFunctionsWorkerMiddleware
{

    public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
    {
        await next(context);
    }
}

And injected in the host like this

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults((configuration) =>
    {
        _ = configuration.UseMiddleware<AuthenticationMiddleware>();
   });

This allows me to read the header values and perform validation before the function code is being executed.

Support for headers and routing is important to us and would allow us to perform proper integration testing of our APIs.

I was wondering, we are also working a lot with Azure Queues. Do you have suggestions how to perform integration tests with them?

Thank you, Patrick

wigmorewelsh commented 7 months ago

Hi

For Azure Queues you can either rely on admin endpoints that allow you to call any binding, or implement IDataMapperFactory to extend the supported bindings and trigger them in any way you like. I'm aware of at least one other group who use Azure Queues with Functions who would be interested in an extention. I'll sit down and document an example over the weekend its pretty low level.

Regards

Tim

martin-poelzl-AP commented 4 months ago

Support for routing, headers and parameters would be appreciated. If you could point me in the right direction I may come up with a PR.

Thanks Martin