コード例 #1
0
        public async Task DoNotReadEntireResponseBody()
        {
            // Arrange
            var command = new CheckHttpEndpoint(
                HttpMonitorCheckId.Create(),
                HttpMonitorId.Create(),
                new HttpRequest(HttpMethod.Get, _httpServer.BaseAddress));

            _httpServer.Handler = async ctx =>
            {
                ctx.Response.StatusCode = (int)HttpStatusCode.Accepted;

                // write infinitely to the response stream
                while (true)
                {
                    await ctx.Response.WriteAsync(Guid.NewGuid().ToString());
                }
                // ReSharper disable once FunctionNeverReturns
            };

            // Act
            var @event = await _executor.CheckHttpEndpointAsync(command);

            // Assert
            Assert.Null(@event.ErrorMessage);
            Assert.Equal(HttpStatusCode.Accepted, @event.Response.StatusCode);
        }
コード例 #2
0
        public async Task RequestHttpMethodAndPathCalled(string httpMethod)
        {
            // Arrange
            var command = new CheckHttpEndpoint(
                HttpMonitorCheckId.Create(),
                HttpMonitorId.Create(),
                new HttpRequest(new HttpMethod(httpMethod),
                                new Uri(_httpServer.BaseAddress,
                                        $"/api/{DateTime.UtcNow.Ticks}/index.html?q={DateTime.UtcNow.Ticks}")));

            string actualHttpMethod   = null;
            string actualRelativePath = null;
            string actualQueryString  = null;

            _httpServer.Handler = ctx =>
            {
                // capture request data
                actualHttpMethod   = ctx.Request.Method;
                actualRelativePath = ctx.Request.Path.Value;
                actualQueryString  = ctx.Request.QueryString.Value;

                return(Task.CompletedTask);
            };

            // Act
            await _executor.CheckHttpEndpointAsync(command);

            // Assert
            Assert.Equal(command.Request.Method.Method, actualHttpMethod, StringComparer.InvariantCultureIgnoreCase);
            Assert.Equal(command.Request.Url.AbsolutePath, actualRelativePath);
            Assert.Equal(command.Request.Url.Query, actualQueryString);
        }
コード例 #3
0
        private Message ToMessage(CheckHttpEndpoint command)
        {
            var message = _converter.Convert(command);

            message.MessageId = $"{DateTime.UtcNow:yyyyMMddhhmm}-{command.HttpMonitorId}";

            return(message);
        }
コード例 #4
0
        public async Task EndpointUnavailableReturnsError()
        {
            // Arrange
            var command = new CheckHttpEndpoint(
                HttpMonitorCheckId.Create(),
                HttpMonitorId.Create(),
                new HttpRequest(HttpMethod.Get, new Uri("http://localhost:9485/")));// nothing should be open on port

            // Act
            var @event = await _executor.CheckHttpEndpointAsync(command);

            // Assert
            Assert.Null(@event.Response);
            Assert.Equal("A connection with the server could not be established", @event.ErrorMessage);
        }
コード例 #5
0
        public async Task <HttpMonitorCheck> CheckHttpEndpointAsync(CheckHttpEndpoint command)
        {
            HttpResponse response = null;
            DateTime     startTime;
            DateTime     endTime;
            string       errorMessage = null;

            using (var requestMessage = BuildRequestMessage(command))
            {
                startTime = DateTime.UtcNow;

                try
                {
                    using (var responseMessage = await SendMessageAsync(requestMessage))
                    {
                        response = HttpResponse.Create(responseMessage);
                    }
                }
                catch (HttpRequestException ex)
                {
                    if (ex.InnerException is Win32Exception win32Exception)
                    {
                        // A connection with the server could not be established
                        errorMessage = win32Exception.Message;
                    }
                    else
                    {
                        errorMessage = ex.Message;
                    }
                }
                catch (TaskCanceledException)
                {
                    errorMessage = "Request timed out";
                }
                finally
                {
                    endTime = DateTime.UtcNow;
                }
            }

            return(command.CreateHttpMonitorCheck(new HttpRequestTiming(startTime, endTime), response, errorMessage));
        }
コード例 #6
0
        public async Task ForceCloseConnectionReturnsError()
        {
            // Arrange
            var command = new CheckHttpEndpoint(
                HttpMonitorCheckId.Create(),
                HttpMonitorId.Create(),
                new HttpRequest(HttpMethod.Get, _httpServer.BaseAddress));

            _httpServer.Handler = async ctx =>
            {
                await _httpServer.Host.StopAsync();
            };

            // Act
            var @event = await _executor.CheckHttpEndpointAsync(command);

            // Assert
            Assert.Null(@event.Response);
            Assert.Equal("The server returned an invalid or unrecognized response", @event.ErrorMessage);
        }
コード例 #7
0
        public async Task ResponseCatpured(HttpStatusCode statusCode)
        {
            // Arrange
            var command = new CheckHttpEndpoint(
                HttpMonitorCheckId.Create(),
                HttpMonitorId.Create(),
                new HttpRequest(HttpMethod.Get, _httpServer.BaseAddress));

            _httpServer.Handler = ctx =>
            {
                // set response status code
                ctx.Response.StatusCode = (int)statusCode;

                return(Task.CompletedTask);
            };

            // Act
            var @event = await _executor.CheckHttpEndpointAsync(command);

            // Assert
            Assert.Equal(statusCode, @event.Response.StatusCode);
        }
コード例 #8
0
        public async Task RequestTiming(int millisecondsDelay)
        {
            // Arrange
            var command = new CheckHttpEndpoint(
                HttpMonitorCheckId.Create(),
                HttpMonitorId.Create(),
                new HttpRequest(HttpMethod.Get, _httpServer.BaseAddress));

            _httpServer.Handler = async ctx =>
            {
                await Task.Delay(millisecondsDelay);
            };

            // Act
            var expectedStartTime = DateTime.UtcNow;
            var @event            = await _executor.CheckHttpEndpointAsync(command);

            var expectedEndTime = DateTime.UtcNow;

            // Assert
            AssertDateTime.Equal(expectedStartTime, @event.RequestTiming.StartTime, TimeSpanComparer.DefaultTolerance);
            AssertDateTime.Equal(expectedEndTime, @event.RequestTiming.EndTime, TimeSpanComparer.DefaultTolerance);
        }
コード例 #9
0
        public async Task HangRequestReturnsTimeoutError()
        {
            // Arrange
            var command = new CheckHttpEndpoint(
                HttpMonitorCheckId.Create(),
                HttpMonitorId.Create(),
                new HttpRequest(HttpMethod.Get, _httpServer.BaseAddress));

            // set timeout on client level
            _httpClient.Timeout = TimeSpan.FromMilliseconds(100);

            _httpServer.Handler = async ctx =>
            {
                await Task.Delay(TimeSpan.FromMilliseconds(1000));
            };

            // Act
            var @event = await _executor.CheckHttpEndpointAsync(command);

            // Assert
            Assert.Null(@event.Response);
            Assert.Equal("Request timed out", @event.ErrorMessage);
        }
コード例 #10
0
 private HttpRequestMessage BuildRequestMessage(CheckHttpEndpoint command)
 {
     return(new HttpRequestMessage(command.Request.Method, command.Request.Url));
 }
コード例 #11
0
        private CloudQueueMessage ToMessage(CheckHttpEndpoint command)
        {
            var message = _converter.Convert(command);

            return(message);
        }