An error occurred during the test if the CreateResponse extension method is used to return the Azure Function HttpResonseMessage

My Azure function code is as 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 read the result as shown below:

var result = await MyHttpTriggerFunction.Run(req, log).ConfigureAwait(false);

After the function is executed, when it tries to return a response in the result variable, the verification method fails.

**

System.InvalidOperationException: the request does not have an associated configuration object or the provided configuration was zero.

**

I made sure that the test project has the same System.Net.Http.HttpRequestMessageExtensiondll.

If I changed the function code so as not to use the extension method CreateResponse(this extension method refers to the VS 2017 template) and return the answer as shown below, I get the answer in the testing method, and the test case works 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;
}

stacktrace

StackTrace: at System.Net.Http.HttpRequestMessageExtensions.CreateResponse [] (HttpRequestMessage , HttpStatusCode statusCode, T, HttpConfiguration ) System.Net.Http.HttpRequestMessageExtensions.CreateResponse [] (HttpRequestMessage , HttpStatusCode statusCode, T) MyFunctionApp.MyHttpTriggerFunction.d__1.MoveNext() --- , - System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task ) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task ) System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() MyFunctionAppUnitTest.MyHttpTriggerFunctionTest.d__2.MoveNext() C:\Users\rsingh\Desktop\Git_Workspace\ActivationAPI\MyFunctionAppUnitTest\MyHttpTriggerFunctionTest.cs: 53 --- , - System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task ) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task ) Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.ThreadOperations.ExecuteWithAbortSafety(Action action) : MyFunctionAppUnitTest.MyHttpTriggerFunctionTest.MyHttpTriggerFunction_SuccessResult : System.InvalidOperationException: .

-

+4
2

.

, null.

httpserver HttpConfiguration.

// Arrange.
var configuration = new HttpConfiguration();
var request = new System.Net.Http.HttpRequestMessage();
request.Properties[System.Web.Http.Hosting.HttpPropertyKeys.HttpConfigurationKey] = configuration;

//...other code
+12

Azure, HTTP- HttpConfiguartion, (, , ) SetConfiguration HttpRequestMessage .

:

var configuration = new HttpConfiguration();
var request = new HttpRequestMessage();
request.SetConfiguration(configuration);
+6

Source: https://habr.com/ru/post/1678828/


All Articles