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