My Azure Function code is like below
public static class MyHttpTriggerFunction
{
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
// some business logic
if (valid)
{
return req.CreateResponse(HttpStatusCode.OK, true);
}
else
{
return req.CreateResponse(HttpStatusCode.BadRequest, "some error message");
}
}
}
In my test project I am reading the result like below:
var result = await MyHttpTriggerFunction.Run(req, log).ConfigureAwait(false);
After executing the function, when it try to return the response in result variable, the test method fails with exception.
**
System.InvalidOperationException: The request does not have an associated configuration object or the provided configuration was null.
**
I have made sure that test project has the same System.Net.Http.HttpRequestMessageExtension
dll.
If I change the function code not to use CreateResponse
extension method (this extension method is from the VS 2017 template's code )
and return the response like below, I get the response in test method and the test case runs fine.
var res = new HttpResponseMessage();
if (valid)
{
res.StatusCode = HttpStatusCode.OK;
res.Content = new ObjectContent<bool>(true, new JsonMediaTypeFormatter());
return res;
}
else
{
res.StatusCode = HttpStatusCode.BadRequest;
res.Content = new ObjectContent<string>("some error message", new JsonMediaTypeFormatter());
return res;
}
Below is the stacktrace of error
Result StackTrace: at System.Net.Http.HttpRequestMessageExtensions.CreateResponse[T](HttpRequestMessage request, HttpStatusCode statusCode, T value, HttpConfiguration configuration) at System.Net.Http.HttpRequestMessageExtensions.CreateResponse[T](HttpRequestMessage request, HttpStatusCode statusCode, T value) at MyFunctionApp.MyHttpTriggerFunction.d__1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() at MyFunctionAppUnitTest.MyHttpTriggerFunctionTest.d__2.MoveNext() in C:\Users\rsingh\Desktop\Git_Workspace\ActivationAPI\MyFunctionAppUnitTest\MyHttpTriggerFunctionTest.cs:line 53 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.ThreadOperations.ExecuteWithAbortSafety(Action action) Result Message: Test method MyFunctionAppUnitTest.MyHttpTriggerFunctionTest.MyHttpTriggerFunction_SuccessResult threw exception: System.InvalidOperationException: The request does not have an associated configuration object or the provided configuration was null.
Am I missing something trivial
The Error message is telling you the problem.
The request does not have an associated configuration object or the provided configuration was null.
When testing the request out side of a httpserver you need to give the request a HttpConfiguration.
// Arrange.
var configuration = new HttpConfiguration();
var request = new System.Net.Http.HttpRequestMessage();
request.Properties[System.Web.Http.Hosting.HttpPropertyKeys.HttpConfigurationKey] = configuration;
//...other code
This is not specific to Azure Functions, but in order to execute this test outside of the context of an actual HTTP request you need to make sure you create an HttpConfiguartion
instance, configure it as required (e.g. add any formatters you may need) and call SetConfiguration
on the HttpRequestMessage
instance with that object.
Example:
var configuration = new HttpConfiguration();
var request = new HttpRequestMessage();
request.SetConfiguration(configuration);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With