Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 362
Ramkumar Singh Avatar asked Jun 08 '17 23:06

Ramkumar Singh


2 Answers

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
like image 91
Nkosi Avatar answered Oct 21 '22 19:10

Nkosi


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);
like image 22
Fabio Cavalcante Avatar answered Oct 21 '22 19:10

Fabio Cavalcante