Exemple #1
0
        public void ParametersAreAppendedCorrectly(string initial, string expected)
        {
            var actual = UrlHelper.AppendParameter(initial, "q", "42");

            Assert.Equal(expected, actual);
        }
        public async Task <HealthCheckResult> CheckNow(CancellationToken cancel)
        {
            string outcome;

            Exception exception      = null;
            int?      statusCode     = null;
            string    contentType    = null;
            long?     contentLength  = null;
            string    initialContent = null;
            JToken    data           = null;

            var probeId   = Nonce.Generate(12);
            var probedUrl = _bypassHttpCaching ?
                            UrlHelper.AppendParameter(_targetUrl, ProbeIdParameterName, probeId) :
                            _targetUrl;

            var utcTimestamp = DateTime.UtcNow;
            var sw           = Stopwatch.StartNew();

            try
            {
                var request = new HttpRequestMessage(HttpMethod.Get, probedUrl);
                request.Headers.Add("X-Correlation-ID", probeId);

                if (_bypassHttpCaching)
                {
                    request.Headers.CacheControl = new CacheControlHeaderValue {
                        NoStore = true
                    }
                }
                ;

                var response = await _httpClient.SendAsync(request, cancel);

                statusCode    = (int)response.StatusCode;
                contentType   = response.Content.Headers.ContentType?.ToString();
                contentLength = response.Content.Headers.ContentLength;

                var content = await response.Content.ReadAsStreamAsync();

                (initialContent, data) = await DownloadContent(content, contentType, contentLength);

                outcome = response.IsSuccessStatusCode ? OutcomeSucceeded : OutcomeFailed;
            }
            catch (Exception ex)
            {
                outcome   = OutcomeFailed;
                exception = ex;
            }

            sw.Stop();

            var level = outcome == OutcomeFailed ? "Error" :
                        data == null && _extractor != null ? "Warning" :
                        null;

            return(new HealthCheckResult(
                       utcTimestamp,
                       _title,
                       "GET",
                       _targetUrl,
                       outcome,
                       probeId,
                       level,
                       sw.Elapsed.TotalMilliseconds,
                       statusCode,
                       contentType,
                       contentLength,
                       initialContent,
                       exception,
                       data,
                       _targetUrl == probedUrl ? null : probedUrl));
        }