示例#1
0
        public async Task TryInvokeAsync_ThrowsInvocationExceptionIfHttpResponseHas500StatusCode()
        {
            // Arrange
            var dummyInvocationRequest = new InvocationRequest(ModuleSourceType.Cache, "dummyModuleSource");
            Mock <HttpContent>         mockRequestHttpContent = _mockRepository.Create <HttpContent>(); // HttpContent is an abstract class
            Mock <IHttpContentFactory> mockHttpContentFactory = _mockRepository.Create <IHttpContentFactory>();

            mockHttpContentFactory.Setup(h => h.Create(dummyInvocationRequest)).Returns(mockRequestHttpContent.Object);
            var dummyHttpResponseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError)
            {
                Content = new StreamContent(new MemoryStream())
            };
            Mock <IHttpClientService> mockHttpClientService = _mockRepository.Create <IHttpClientService>();

            mockHttpClientService.Setup(h => h.SendAsync(It.Is <HttpRequestMessage>(hr => ReferenceEquals(hr.Content, mockRequestHttpContent.Object)),
                                                         HttpCompletionOption.ResponseHeadersRead,
                                                         CancellationToken.None)).
            ReturnsAsync(dummyHttpResponseMessage);
            var dummyInvocationError            = new InvocationError("dummyErrorMessage", "dummyErrorStack");
            Mock <IJsonService> mockJsonService = _mockRepository.Create <IJsonService>();

            mockJsonService.Setup(j => j.DeserializeAsync <InvocationError>(It.IsAny <Stream>(), CancellationToken.None)).ReturnsAsync(dummyInvocationError);
#pragma warning disable IDE0067
            ExposedHttpNodeJSService testSubject = CreateHttpNodeJSService(httpContentFactory: mockHttpContentFactory.Object,
                                                                           httpClientService: mockHttpClientService.Object,
                                                                           jsonService: mockJsonService.Object);
#pragma warning disable IDE0067

            // Act and assert
            InvocationException result = await Assert.ThrowsAsync <InvocationException>(() => testSubject.ExposedTryInvokeAsync <string>(dummyInvocationRequest, CancellationToken.None)).ConfigureAwait(false);

            _mockRepository.VerifyAll();
            Assert.Equal(dummyInvocationError.ErrorMessage + Environment.NewLine + dummyInvocationError.ErrorStack, result.Message, ignoreLineEndingDifferences: true);
        }
        public async void AllInvokeMethods_HandleHttpClientErrorsSuchAsMalformedRequests()
        {
            // Arrange
            HttpNodeJSService testSubject = CreateHttpNodeJSService();
            await testSubject.InvokeFromStringAsync("module.exports = callback => callback();").ConfigureAwait(false); // Starts the Node.js process

            Uri dummyEndpoint    = testSubject.Endpoint;
            var dummyJsonService = new JsonService();

            // Act
            using (var dummyHttpClient = new HttpClient())
                // Send a request with an invalid HTTP method. NodeJS drops the connection halfway through and fires the clientError event - https://nodejs.org/api/http.html#http_event_clienterror
                using (var dummyHttpRequestMessage = new HttpRequestMessage(new HttpMethod("INVALID"), dummyEndpoint))
                    using (HttpResponseMessage dummyHttpResponseMessage = await dummyHttpClient.SendAsync(dummyHttpRequestMessage).ConfigureAwait(false))
                        using (Stream dummyStream = await dummyHttpResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false))
                        {
                            InvocationError result = await dummyJsonService.DeserializeAsync <InvocationError>(dummyStream).ConfigureAwait(false);

                            // Assert
                            Assert.Equal(HttpStatusCode.InternalServerError, dummyHttpResponseMessage.StatusCode);
                            Assert.False(string.IsNullOrWhiteSpace(result.ErrorMessage));
                            Assert.False(string.IsNullOrWhiteSpace(result.ErrorStack));
                        }
        }