Example #1
0
        public void StatusCodeShouldBe(HttpStatusCode httpStatusCode)
        {
            if (_httpResponse == null)
            {
                throw new BardException($"{nameof(_httpResponse)} property has not been set.");
            }

            var statusCode = _httpResponse.StatusCode;

            if (Log)
            {
                var headerMessage =
                    new StringBuilder($"THEN THE RESPONSE SHOULD BE HTTP {(int) httpStatusCode} {httpStatusCode}");

                if (statusCode != httpStatusCode)
                {
                    headerMessage.Append($" BUT WAS HTTP {(int) statusCode} {statusCode}");
                }

                _logWriter.LogHeaderMessage(headerMessage.ToString());

                LogResponse();
            }

            if (statusCode != httpStatusCode)
            {
                throw new BardException(
                          $"Invalid HTTP Status Code Received \n Expected: {(int) httpStatusCode} {httpStatusCode} \n Actual: {(int) statusCode} {statusCode} \n ");
            }

            _performanceMonitor.AssertElapsedTime(_apiRequest, _apiResult, MaxElapsedTime);
        }
Example #2
0
        public IHeaders ShouldInclude(string headerName, string?headerValue = null)
        {
            var headerMessage =
                new StringBuilder($"THEN THE RESPONSE SHOULD INCLUDE THE HTTP HEADER '{headerName}'");

            if (headerValue != null)
            {
                headerMessage.Append($":{headerValue}");
            }

            _logWriter.LogHeaderMessage(headerMessage.ToString());

            _logWriter.WriteHttpResponseToConsole(_apiResult);

            var contentHeaders = _responseMessage.Content.Headers.Select(pair => pair);
            var headers        = _responseMessage.Headers.Select(pair => pair);

            var allHeaders = contentHeaders.Concat(headers).ToList();

            var(headerKey, headerValues) = allHeaders.FirstOrDefault(pair =>
                                                                     string.Equals(pair.Key, headerName, StringComparison.CurrentCultureIgnoreCase));

            if (headerKey == null)
            {
                throw new BardException($"Header '{headerName} not present.");
            }

            if (headerValue != null)
            {
                var lowerCase = headerValue.ToLower();

                if (headerValues.Select(hv => hv.ToLower()).Contains(lowerCase) == false)
                {
                    throw new BardException($"Header Value'{headerValue} not present.");
                }
            }

            _performanceMonitor.AssertElapsedTime(_apiRequest, _apiResult, MaxElapsedTime);

            return(this);
        }