public async Task ProcessDefaultInvocationRequest_CustomHandler_EnableRequestForwarding_False() { var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict); var customHandlerOptions = new HttpWorkerOptions() { Port = _defaultPort, Type = CustomHandlerType.Http }; handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .Callback <HttpRequestMessage, CancellationToken>((request, token) => ValidateeSimpleHttpTriggerSentAsDefaultInvocationRequest(request)) .ReturnsAsync(HttpWorkerTestUtilities.GetValidHttpResponseMessageWithJsonRes()); _httpClient = new HttpClient(handlerMock.Object); _defaultHttpWorkerService = new DefaultHttpWorkerService(_httpClient, new OptionsWrapper <HttpWorkerOptions>(customHandlerOptions), _testLogger, _testEnvironment); var testScriptInvocationContext = HttpWorkerTestUtilities.GetSimpleHttpTriggerScriptInvocationContext(TestFunctionName, _testInvocationId, _functionLogger); await _defaultHttpWorkerService.InvokeAsync(testScriptInvocationContext); var invocationResult = await testScriptInvocationContext.ResultSource.Task; var expectedHttpScriptInvocationResult = HttpWorkerTestUtilities.GetHttpScriptInvocationResultWithJsonRes(); var testLogs = _functionLogger.GetLogMessages(); Assert.True(testLogs.Count() == expectedHttpScriptInvocationResult.Logs.Count()); Assert.True(testLogs.All(m => m.FormattedMessage.Contains("invocation log"))); Assert.Equal(expectedHttpScriptInvocationResult.Outputs.Count(), invocationResult.Outputs.Count()); Assert.Equal(expectedHttpScriptInvocationResult.ReturnValue, invocationResult.Return); var responseJson = JObject.Parse(invocationResult.Outputs["res"].ToString()); Assert.Equal("my world", responseJson["Body"]); Assert.Equal("201", responseJson["StatusCode"]); }
public async Task ProcessSimpleHttpTriggerInvocationRequest_CustomHandler_EnableForwardingHttpRequest_True() { var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict); var customHandlerOptions = new HttpWorkerOptions() { Port = _defaultPort, EnableForwardingHttpRequest = true }; handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .Callback <HttpRequestMessage, CancellationToken>((request, token) => ValidateSimpleHttpTriggerInvocationRequest(request)) .ReturnsAsync(HttpWorkerTestUtilities.GetValidSimpleHttpResponseMessage()); _httpClient = new HttpClient(handlerMock.Object); _defaultHttpWorkerService = new DefaultHttpWorkerService(_httpClient, new OptionsWrapper <HttpWorkerOptions>(customHandlerOptions), _testLogger, _testEnvironment, new OptionsWrapper <ScriptJobHostOptions>(_scriptJobHostOptions)); var testScriptInvocationContext = HttpWorkerTestUtilities.GetSimpleHttpTriggerScriptInvocationContext(TestFunctionName, _testInvocationId, _functionLogger); await _defaultHttpWorkerService.InvokeAsync(testScriptInvocationContext); var invocationResult = await testScriptInvocationContext.ResultSource.Task; var expectedHttpResponseMessage = HttpWorkerTestUtilities.GetValidSimpleHttpResponseMessage(); var expectedResponseContent = await expectedHttpResponseMessage.Content.ReadAsStringAsync(); var testLogs = _functionLogger.GetLogMessages(); Assert.Equal(0, testLogs.Count()); Assert.Equal(1, invocationResult.Outputs.Count()); var httpOutputResponse = invocationResult.Outputs.FirstOrDefault().Value as HttpResponseMessage; Assert.NotNull(httpOutputResponse); Assert.Equal(expectedHttpResponseMessage.StatusCode, httpOutputResponse.StatusCode); Assert.Equal(expectedResponseContent, await httpOutputResponse.Content.ReadAsStringAsync()); var response = invocationResult.Return as HttpResponseMessage; Assert.NotNull(response); Assert.Equal(expectedHttpResponseMessage.StatusCode, response.StatusCode); Assert.Equal(expectedResponseContent, await response.Content.ReadAsStringAsync()); }