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); }
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); }
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); }
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); }
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); }
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); }
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); }