C# – Error while executing test, if using CreateResponse extention method to return Azure Function HttpResonseMessage

azureazure-functionscunit testingvisual-studio-2017

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

Best Answer

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
Related Topic