I have an isolated Azure Function that makes couple of HTTP POST calls. I am trying to write an integration test for them. But the test setup fails with a gRPC error.
Here is the Program.cs that configures the HttpClient with services.AddHttpClient();
public class Program
{
public static void Main()
{
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
services.AddHttpClient();
}).Build();
host.Run();
}
}
The sample function looks like this:
public class AzFunctionEndpoint
{
public AzFunctionEndpoint(IHttpClientFactory httpClientFactory, IConfiguration configuration, ILoggerFactory loggerFactory)
{
logger = loggerFactory.CreateLogger<ResolveEndpoint>();
this.httpClientFactory = httpClientFactory;
this.configuration = configuration;
}
[Function("azfunction")]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = "azs/azfunction")] HttpRequestData req)
{
// Couple of other HTTP calls using httpClientFactory
// return
var res = req.CreateResponse(HttpStatusCode.OK);
return res;
}
private readonly IConfiguration configuration;
private readonly IHttpClientFactory httpClientFactory;
private readonly ILogger logger;
}
The Function runs correctly on local machine and when deployed to Azure.
Now I try and create an integration test with
public class AzFunctionEndpointIntegrationTests
{
public AzFunctionEndpointIntegrationTests()
{
factory = new WebApplicationFactory<Program>();
var clientFactory = factory.Services.GetService<IHttpClientFactory>();
// THIS LINE CAUSES gRPC host error
client = clientFactory.CreateClient();
}
[Fact]
public async Task AzFunction_Should_BeOK()
{
// POST to the azure function
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(HttpMethods.Post), "api/azs/azfunction");
var response = await client.SendAsync(request);
response.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);
}
private HttpClient client;
private WebApplicationFactory<Program> factory;
}
The test code that tries to create HttpClient to invoke my function causes this exception
client = clientFactory.CreateClient();
System.InvalidOperationException : The gRPC channel URI 'http://:51828' could not be parsed.
I don't quite understand what is this error ! Any help on writing integration tests for Azure Functions is appreciated.