public Task <HttpMonitorCheck> GetAsync(HttpMonitorCheckId id)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            var uri     = _configs.DocumentCollectionUri;
            var options = new FeedOptions()
            {
                MaxItemCount = 1
            };

            var querySpec = new SqlQuerySpec
            {
                QueryText  = "select * from root r where (r.id = @id and r._type = @type)",
                Parameters = new SqlParameterCollection
                {
                    new SqlParameter("@id", id.ToString()),
                    new SqlParameter("@type", DocumentType)
                }
            };

            return(_client.CreateDocumentQuery <HttpMonitorCheck>(uri, querySpec, options)
                   .AsDocumentQuery()
                   .FirstOrDefaultAsync());
        }
        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);
        }
        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);
        }
        public async Task PublishAsync()
        {
            var httpMonitors = await _repository.GetAsync();

            var commands = httpMonitors.Select(x => x.CreateCheckHttpEndpoint(HttpMonitorCheckId.Create()));

            await _publisher.PublishAsync(commands);
        }
 private HttpMonitorChecked GenerateHttpMonitorChecked(HttpMonitor httpMonitor)
 {
     return(httpMonitor
            .CreateCheckHttpEndpoint(HttpMonitorCheckId.Create())
            .CreateHttpMonitorCheck(
                new HttpRequestTiming(DateTime.UtcNow, DateTime.UtcNow.AddSeconds(1)),
                new HttpResponse(HttpStatusCode.OK))
            .CreateHttpMonitorChecked());
 }
 private HttpMonitorCheck GenerateHttpMonitorCheck(HttpMonitorCheckId id)
 {
     return(new HttpMonitorCheck(
                id,
                HttpMonitorId.Create(),
                new HttpRequest(HttpMethod.Delete, new Uri("http://yahoo.com")),
                new HttpRequestTiming(DateTime.UtcNow, DateTime.UtcNow.AddSeconds(1)),
                new HttpResponse(HttpStatusCode.Accepted)));
 }
 public CheckHttpEndpoint(
     HttpMonitorCheckId httpMonitorCheckId,
     HttpMonitorId httpMonitorId,
     HttpRequest request)
 {
     HttpMonitorCheckId = httpMonitorCheckId ?? throw new ArgumentNullException(nameof(httpMonitorCheckId));
     HttpMonitorId      = httpMonitorId ?? throw new ArgumentNullException(nameof(httpMonitorId));
     Request            = request ?? throw new ArgumentNullException(nameof(request));
 }
 private IEnumerable <CheckHttpEndpoint> GenerateCheckHttpEndpoint(int count)
 {
     for (var i = 0; i < count; i++)
     {
         yield return(new CheckHttpEndpoint(
                          HttpMonitorCheckId.Create(),
                          HttpMonitorId.Create(),
                          new HttpRequest(HttpMethod.Get, new Uri("http://example.com/asdfasdfasdfasdfasdf/asdfasdfasdf/asdfasdfasdf/asdfasdf/sadf"))));
     }
 }
        public async Task <HttpMonitorCheck> ExecuteAsync(HttpMonitorId httpMonitorId)
        {
            var httpMonitor = await _repository.GetByIdAsync(httpMonitorId);

            if (httpMonitor == null)
            {
                throw new EntityNotFoundException(httpMonitorId);
            }

            var cmd = httpMonitor.CreateCheckHttpEndpoint(HttpMonitorCheckId.Create());

            return(await _executor.CheckHttpEndpointAsync(cmd));
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
 private HttpMonitorCheck GenerateHttpMonitorCheck()
 {
     return(GenerateHttpMonitorCheck(HttpMonitorCheckId.Create()));
 }