Пример #1
0
        public async Task ProcessDefaultInvocationRequest_Succeeds()
        {
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                        ItExpr.IsAny <HttpRequestMessage>(),
                                                                        ItExpr.IsAny <CancellationToken>())
            .Callback <HttpRequestMessage, CancellationToken>((request, token) => ValidateDefaultInvocationRequest(request))
            .ReturnsAsync(HttpWorkerTestUtilities.GetValidHttpResponseMessage());

            _httpClient = new HttpClient(handlerMock.Object);
            _defaultHttpWorkerService = new DefaultHttpWorkerService(_httpClient, new OptionsWrapper <HttpWorkerOptions>(_httpWorkerOptions), _testLogger, _testEnvironment, new OptionsWrapper <ScriptJobHostOptions>(_scriptJobHostOptions));
            Assert.Equal(_httpClient.Timeout, _scriptJobHostOptions.FunctionTimeout.Value.Add(TimeSpan.FromMinutes(1)));
            var testScriptInvocationContext = HttpWorkerTestUtilities.GetScriptInvocationContext(TestFunctionName, _testInvocationId, _functionLogger);
            await _defaultHttpWorkerService.ProcessDefaultInvocationRequest(testScriptInvocationContext);

            var invocationResult = await testScriptInvocationContext.ResultSource.Task;

            var expectedHttpScriptInvocationResult = HttpWorkerTestUtilities.GetTestHttpScriptInvocationResult();
            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);
        }
Пример #2
0
        private async void ValidateeSimpleHttpTriggerSentAsDefaultInvocationRequest(HttpRequestMessage httpRequestMessage)
        {
            Assert.Contains($"{HttpWorkerConstants.UserAgentHeaderValue}/{ScriptHost.Version}", httpRequestMessage.Headers.UserAgent.ToString());
            Assert.Equal(_testInvocationId.ToString(), httpRequestMessage.Headers.GetValues(HttpWorkerConstants.InvocationIdHeaderName).Single());
            Assert.Equal(ScriptHost.Version, httpRequestMessage.Headers.GetValues(HttpWorkerConstants.HostVersionHeaderName).Single());
            Assert.Equal(httpRequestMessage.RequestUri.ToString(), $"http://127.0.0.1:{_defaultPort}/{TestFunctionName}");

            HttpScriptInvocationContext httpScriptInvocationContext = await httpRequestMessage.Content.ReadAsAsync <HttpScriptInvocationContext>();

            // Verify Metadata
            var expectedMetadata = HttpWorkerTestUtilities.GetScriptInvocationBindingData();

            Assert.Equal(expectedMetadata.Count(), httpScriptInvocationContext.Metadata.Count());
            foreach (var key in expectedMetadata.Keys)
            {
                Assert.Equal(JsonConvert.SerializeObject(expectedMetadata[key]), httpScriptInvocationContext.Metadata[key]);
            }

            // Verify Data
            Assert.True(httpScriptInvocationContext.Data.Keys.Contains("testInputReq"));
            JObject resultHttpReq       = JObject.FromObject(httpScriptInvocationContext.Data["testInputReq"]);
            JObject expectedHttpRequest = await HttpWorkerTestUtilities.GetTestHttpRequest().GetRequestAsJObject();

            Assert.True(JToken.DeepEquals(expectedHttpRequest, resultHttpReq));
        }
Пример #3
0
        private async void ValidateDefaultInvocationRequest(HttpRequestMessage httpRequestMessage)
        {
            Assert.Contains($"{HttpWorkerConstants.UserAgentHeaderValue}/{ScriptHost.Version}", httpRequestMessage.Headers.UserAgent.ToString());
            Assert.Equal(_testInvocationId.ToString(), httpRequestMessage.Headers.GetValues(HttpWorkerConstants.InvocationIdHeaderName).Single());
            Assert.Equal(ScriptHost.Version, httpRequestMessage.Headers.GetValues(HttpWorkerConstants.HostVersionHeaderName).Single());
            Assert.Equal(httpRequestMessage.RequestUri.ToString(), $"http://127.0.0.1:{_defaultPort}/{TestFunctionName}");

            HttpScriptInvocationContext httpScriptInvocationContext = await httpRequestMessage.Content.ReadAsAsync <HttpScriptInvocationContext>();

            // Verify Metadata
            var expectedMetadata = HttpWorkerTestUtilities.GetScriptInvocationBindingData();

            Assert.Equal(expectedMetadata.Count(), httpScriptInvocationContext.Metadata.Count());
            foreach (var key in expectedMetadata.Keys)
            {
                Assert.Equal(JsonConvert.SerializeObject(expectedMetadata[key]), httpScriptInvocationContext.Metadata[key]);
            }

            // Verify Data
            var expectedData = HttpWorkerTestUtilities.GetScriptInvocationInputs();

            Assert.Equal(expectedData.Count(), httpScriptInvocationContext.Data.Count());
            foreach (var item in expectedData)
            {
                Assert.True(httpScriptInvocationContext.Data.Keys.Contains(item.name));
                Assert.Equal(JsonConvert.SerializeObject(item.val), httpScriptInvocationContext.Data[item.name]);
            }
        }
        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"]);
        }
Пример #5
0
        public void TestPathValue(string functionName, CustomHandlerType type, bool enableForwardingHttpRequest, string expectedValue, string hostValue = "localhost")
        {
            HttpRequest       testHttpRequest = HttpWorkerTestUtilities.GetTestHttpRequest(hostValue);
            HttpWorkerOptions testOptions     = new HttpWorkerOptions
            {
                Type = type,
                EnableForwardingHttpRequest = enableForwardingHttpRequest,
            };
            DefaultHttpWorkerService defaultHttpWorkerService = new DefaultHttpWorkerService(new HttpClient(), new OptionsWrapper <HttpWorkerOptions>(testOptions), _testLogger, _testEnvironment, new OptionsWrapper <ScriptJobHostOptions>(_scriptJobHostOptions));
            string actualValue = defaultHttpWorkerService.GetPathValue(testOptions, functionName, testHttpRequest);

            Assert.Equal(actualValue, expectedValue);
        }
Пример #6
0
        public async Task ProcessPing_Succeeds()
        {
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                        ItExpr.IsAny <HttpRequestMessage>(),
                                                                        ItExpr.IsAny <CancellationToken>())
            .Callback <HttpRequestMessage, CancellationToken>((request, token) => ValidateSimpleHttpTriggerInvocationRequest(request))
            .ReturnsAsync(HttpWorkerTestUtilities.GetSimpleNotFoundHttpResponseMessage());

            _httpClient = new HttpClient(handlerMock.Object);
            _defaultHttpWorkerService = new DefaultHttpWorkerService(_httpClient, new OptionsWrapper <HttpWorkerOptions>(_httpWorkerOptions), _testLogger, _testEnvironment, new OptionsWrapper <ScriptJobHostOptions>(_scriptJobHostOptions));
            await _defaultHttpWorkerService.PingAsync();

            handlerMock.VerifyAll();
        }
        public async Task ProcessDefaultInvocationRequest_InvalidMediaType_Throws()
        {
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                        ItExpr.IsAny <HttpRequestMessage>(),
                                                                        ItExpr.IsAny <CancellationToken>())
            .Callback <HttpRequestMessage, CancellationToken>((request, token) => RequestHandler(request))
            .ReturnsAsync(HttpWorkerTestUtilities.GetHttpResponseMessageWithStringContent());

            _httpClient = new HttpClient(handlerMock.Object);
            _defaultHttpWorkerService = new DefaultHttpWorkerService(_httpClient, new OptionsWrapper <HttpWorkerOptions>(_httpWorkerOptions), _testLogger, _testEnvironment, new OptionsWrapper <ScriptJobHostOptions>(_scriptJobHostOptions));
            var testScriptInvocationContext = HttpWorkerTestUtilities.GetScriptInvocationContext(TestFunctionName, _testInvocationId, _testLogger);
            await _defaultHttpWorkerService.ProcessDefaultInvocationRequest(testScriptInvocationContext);

            await Assert.ThrowsAsync <UnsupportedMediaTypeException>(async() => await testScriptInvocationContext.ResultSource.Task);
        }
Пример #8
0
        public async Task IsWorkerReady_Returns_True()
        {
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                        ItExpr.IsAny <HttpRequestMessage>(),
                                                                        ItExpr.IsAny <CancellationToken>())
            .Callback <HttpRequestMessage, CancellationToken>((request, token) => RequestHandler(request))
            .ReturnsAsync(HttpWorkerTestUtilities.GetValidHttpResponseMessage());

            _httpClient = new HttpClient(handlerMock.Object);
            _defaultHttpWorkerService = new DefaultHttpWorkerService(_httpClient, new OptionsWrapper <HttpWorkerOptions>(_httpWorkerOptions), _testLogger, _testEnvironment, new OptionsWrapper <ScriptJobHostOptions>(_scriptJobHostOptions));

            bool workerReady = await _defaultHttpWorkerService.IsWorkerReady(CancellationToken.None);

            Assert.True(workerReady);
        }
        public void ToScripInvocationResultTests(bool includeReturnValue)
        {
            var testInvocationContext    = HttpWorkerTestUtilities.GetSimpleHttpTriggerScriptInvocationContext("test", Guid.NewGuid(), new TestLogger("test"));
            var testHttpInvocationResult = HttpWorkerTestUtilities.GetHttpScriptInvocationResultWithJsonRes();

            if (includeReturnValue)
            {
                testHttpInvocationResult.ReturnValue = "Hello return";
            }
            var result = testHttpInvocationResult.ToScriptInvocationResult(testInvocationContext);

            Assert.Null(result.Return);

            var resResult = JObject.Parse((string)result.Outputs["res"]);

            Assert.Equal("my world", resResult["Body"]);
        }
Пример #10
0
        public void ToScripInvocationResultTests(bool includeReturnValue)
        {
            var testInvocationContext    = HttpWorkerTestUtilities.GetSimpleHttpTriggerScriptInvocationContext("test", Guid.NewGuid(), new TestLogger("test"));
            var testHttpInvocationResult = HttpWorkerTestUtilities.GetHttpScriptInvocationResultWithJsonRes();

            if (includeReturnValue)
            {
                testHttpInvocationResult.ReturnValue = "Hello return";
            }
            var result = testHttpInvocationResult.ToScriptInvocationResult(testInvocationContext);

            Assert.Null(result.Return);
            ExpandoObject output = result.Outputs["res"] as ExpandoObject;

            output.TryGetValue <object>("Body", out var resResult, ignoreCase: true);
            Assert.Equal("my world", resResult);
        }
Пример #11
0
        public async Task ProcessDefaultInvocationRequest_OkResponse_InvalidBody_Throws()
        {
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                        ItExpr.IsAny <HttpRequestMessage>(),
                                                                        ItExpr.IsAny <CancellationToken>())
            .Callback <HttpRequestMessage, CancellationToken>((request, token) => RequestHandler(request))
            .ReturnsAsync(HttpWorkerTestUtilities.GetValidHttpResponseMessage_JsonType_InvalidContent());

            _httpClient = new HttpClient(handlerMock.Object);
            _defaultHttpWorkerService = new DefaultHttpWorkerService(_httpClient, new OptionsWrapper <HttpWorkerOptions>(_httpWorkerOptions), _testLogger, _testEnvironment, new OptionsWrapper <ScriptJobHostOptions>(_scriptJobHostOptions));
            var testScriptInvocationContext = HttpWorkerTestUtilities.GetScriptInvocationContext(TestFunctionName, _testInvocationId, _functionLogger);
            await _defaultHttpWorkerService.ProcessDefaultInvocationRequest(testScriptInvocationContext);

            InvalidOperationException recodedEx = await Assert.ThrowsAsync <InvalidOperationException>(async() => await testScriptInvocationContext.ResultSource.Task);

            Assert.Contains("Hello World", recodedEx.Message);
            Assert.Contains("StatusCode: 200", recodedEx.Message);
        }
Пример #12
0
        public async Task ProcessDefaultInvocationRequest_JsonResponse_Succeeds()
        {
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                        ItExpr.IsAny <HttpRequestMessage>(),
                                                                        ItExpr.IsAny <CancellationToken>())
            .Callback <HttpRequestMessage, CancellationToken>((request, token) => RequestHandler(request))
            .ReturnsAsync(HttpWorkerTestUtilities.GetHttpResponseMessageWithJsonContent());

            _httpClient = new HttpClient(handlerMock.Object);
            _defaultHttpWorkerService = new DefaultHttpWorkerService(_httpClient, new OptionsWrapper <HttpWorkerOptions>(_httpWorkerOptions), _testLogger, _testEnvironment, new OptionsWrapper <ScriptJobHostOptions>(_scriptJobHostOptions));
            var testScriptInvocationContext = HttpWorkerTestUtilities.GetScriptInvocationContext(TestFunctionName, _testInvocationId, _functionLogger);
            await _defaultHttpWorkerService.ProcessDefaultInvocationRequest(testScriptInvocationContext);

            var invocationResult = await testScriptInvocationContext.ResultSource.Task;

            Assert.Empty(invocationResult.Outputs);
            Assert.Null(invocationResult.Return);
        }
Пример #13
0
        public async Task ProcessDefaultInvocationRequest_BadRequestResponse_Throws()
        {
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                        ItExpr.IsAny <HttpRequestMessage>(),
                                                                        ItExpr.IsAny <CancellationToken>())
            .Callback <HttpRequestMessage, CancellationToken>((request, token) => RequestHandler(request))
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.BadRequest
            });

            _httpClient = new HttpClient(handlerMock.Object);
            _defaultHttpWorkerService = new DefaultHttpWorkerService(_httpClient, new OptionsWrapper <HttpWorkerOptions>(_httpWorkerOptions), _testLoggerFactory);
            var testScriptInvocationContext = HttpWorkerTestUtilities.GetScriptInvocationContext(TestFunctionName, _testInvocationId, _testLogger);
            await _defaultHttpWorkerService.ProcessDefaultInvocationRequest(testScriptInvocationContext);

            await Assert.ThrowsAsync <HttpRequestException>(async() => await testScriptInvocationContext.ResultSource.Task);
        }
Пример #14
0
        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());
        }
Пример #15
0
        public void ProcessOutputLogs_Succeeds(HttpScriptInvocationResult httpScriptInvocationResult)
        {
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                        ItExpr.IsAny <HttpRequestMessage>(),
                                                                        ItExpr.IsAny <CancellationToken>())
            .Callback <HttpRequestMessage, CancellationToken>((request, token) => RequestHandler(request))
            .ReturnsAsync(HttpWorkerTestUtilities.GetValidHttpResponseMessage());

            _httpClient = new HttpClient(handlerMock.Object);
            _defaultHttpWorkerService = new DefaultHttpWorkerService(_httpClient, new OptionsWrapper <HttpWorkerOptions>(_httpWorkerOptions), _testLogger, _testEnvironment, new OptionsWrapper <ScriptJobHostOptions>(_scriptJobHostOptions));
            _defaultHttpWorkerService.ProcessLogsFromHttpResponse(HttpWorkerTestUtilities.GetScriptInvocationContext(TestFunctionName, _testInvocationId, _functionLogger), httpScriptInvocationResult);
            var testLogs = _functionLogger.GetLogMessages();

            if (httpScriptInvocationResult.Logs != null && httpScriptInvocationResult.Logs.Any())
            {
                Assert.True(testLogs.Count() == httpScriptInvocationResult.Logs?.Count());
                Assert.True(testLogs.All(m => m.FormattedMessage.Contains("test log")));
            }
        }
Пример #16
0
        public async Task ProcessDefaultInvocationRequest_BinaryData_Succeeds()
        {
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                        ItExpr.IsAny <HttpRequestMessage>(),
                                                                        ItExpr.IsAny <CancellationToken>())
            .Callback <HttpRequestMessage, CancellationToken>((request, token) => ValidateDefaultInvocationRequest(request))
            .ReturnsAsync(HttpWorkerTestUtilities.GetValidHttpResponseMessage_Binary_Data());

            _httpClient = new HttpClient(handlerMock.Object);
            _defaultHttpWorkerService = new DefaultHttpWorkerService(_httpClient, new OptionsWrapper <HttpWorkerOptions>(_httpWorkerOptions), _testLogger, _testEnvironment, new OptionsWrapper <ScriptJobHostOptions>(_scriptJobHostOptions));
            var testScriptInvocationContext = HttpWorkerTestUtilities.GetScriptInvocationContext(TestFunctionName, _testInvocationId, _functionLogger);
            await _defaultHttpWorkerService.ProcessDefaultInvocationRequest(testScriptInvocationContext);

            var invocationResult = await testScriptInvocationContext.ResultSource.Task;

            var expectedHttpScriptInvocationResult = HttpWorkerTestUtilities.GetTestHttpScriptInvocationResult_Binary_Outputs();
            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());

            // Verifies decoding for binary data from base64 encoded string
            foreach (var expectedOutput in expectedHttpScriptInvocationResult.Outputs)
            {
                byte[] expectedOutputData   = Convert.FromBase64String((string)expectedOutput.Value);
                string expectedOutputString = Encoding.UTF8.GetString(expectedOutputData);
                string actualOutputString   = Encoding.UTF8.GetString((byte[])invocationResult.Outputs[expectedOutput.Key]);
                Assert.Equal(expectedOutputString, actualOutputString);
            }

            byte[] expectedStringData = Convert.FromBase64String((string)expectedHttpScriptInvocationResult.ReturnValue);
            string expectedString     = Encoding.UTF8.GetString(expectedStringData);
            string actualString       = Encoding.UTF8.GetString((byte[])invocationResult.Return);

            Assert.Equal(expectedString, actualString);
        }
Пример #17
0
        public async Task ProcessSimpleHttpTriggerInvocationRequest_Sets_ExpectedResult()
        {
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                        ItExpr.IsAny <HttpRequestMessage>(),
                                                                        ItExpr.IsAny <CancellationToken>())
            .Callback <HttpRequestMessage, CancellationToken>((request, token) => RequestHandler(request))
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.InternalServerError
            });

            _httpClient = new HttpClient(handlerMock.Object);
            _defaultHttpWorkerService = new DefaultHttpWorkerService(_httpClient, new OptionsWrapper <HttpWorkerOptions>(_httpWorkerOptions), _testLoggerFactory);
            var testScriptInvocationContext = HttpWorkerTestUtilities.GetSimpleHttpTriggerScriptInvocationContext(TestFunctionName, _testInvocationId, _testLogger);
            await _defaultHttpWorkerService.ProcessHttpInAndOutInvocationRequest(testScriptInvocationContext);

            var invocationResult            = await testScriptInvocationContext.ResultSource.Task;
            var expectedHttpResponseMessage = HttpWorkerTestUtilities.GetValidSimpleHttpResponseMessage();
            var expectedResponseContent     = await expectedHttpResponseMessage.Content.ReadAsStringAsync();

            var testLogs = _testLogger.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(HttpStatusCode.InternalServerError, httpOutputResponse.StatusCode);

            var response = invocationResult.Return as HttpResponseMessage;

            Assert.NotNull(response);
            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
        }
Пример #18
0
        public async Task ProcessDefaultInvocationRequest_DataType_Binary_Succeeds()
        {
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                        ItExpr.IsAny <HttpRequestMessage>(),
                                                                        ItExpr.IsAny <CancellationToken>())
            .Callback <HttpRequestMessage, CancellationToken>((request, token) => ValidateDefaultInvocationRequest(request))
            .ReturnsAsync(HttpWorkerTestUtilities.GetValidHttpResponseMessage_DataType_Binary_Data());

            _httpClient = new HttpClient(handlerMock.Object);
            _defaultHttpWorkerService = new DefaultHttpWorkerService(_httpClient, new OptionsWrapper <HttpWorkerOptions>(_httpWorkerOptions), _testLoggerFactory);
            var testScriptInvocationContext = HttpWorkerTestUtilities.GetScriptInvocationContext(TestFunctionName, _testInvocationId, _testLogger, Script.Description.DataType.Binary);
            await _defaultHttpWorkerService.ProcessDefaultInvocationRequest(testScriptInvocationContext);

            var invocationResult = await testScriptInvocationContext.ResultSource.Task;

            var expectedHttpScriptInvocationResult = HttpWorkerTestUtilities.GetTestHttpScriptInvocationResult_DataType_Binary_Outputs();
            var testLogs = _testLogger.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());

            // Verifies output data is not transformed if dataType is set to binary
            foreach (var expectedOutput in expectedHttpScriptInvocationResult.Outputs)
            {
                string expectedOutputString = Encoding.UTF8.GetString((byte[])expectedOutput.Value);
                string actualOutputString   = Encoding.UTF8.GetString((byte[])invocationResult.Outputs[expectedOutput.Key]);
                Assert.Equal(expectedOutputString, actualOutputString);
            }

            string expectedString = Encoding.UTF8.GetString((byte[])expectedHttpScriptInvocationResult.ReturnValue);
            string actualString   = Encoding.UTF8.GetString((byte[])invocationResult.Return);

            Assert.Equal(expectedString, actualString);
        }