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 "); } _apiResult.AssertElapsedTime(MaxElapsedTime); }
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."); } } _apiResult.AssertElapsedTime(MaxElapsedTime); return(this); }
public void AssertElapsedTime(Func <IResponse>?apiRequest, ApiResult apiResult, int?maxElapsedTime) { const int retryCount = 3; IResponse RetryApiCall(int i) { var response = apiRequest(); _logWriter.LogMessage($"Retry #{i + 1} Response Time: {response.ElapsedTime?.TotalMilliseconds} (milliseconds)"); _logWriter.BlankLine(); _logWriter.LogLineBreak(); _logWriter.BlankLine(); return(response); } if (apiResult.ExceededElapsedTime(maxElapsedTime)) { if (apiRequest == null) { return; } _logWriter.LogHeaderMessage($"The API response took longer than {maxElapsedTime} milliseconds. ({apiResult.ElapsedTime?.TotalMilliseconds})"); var totalTime = new TimeSpan(); for (var i = 0; i < retryCount; i++) { var response = RetryApiCall(i); totalTime = totalTime.Add(response.ElapsedTime.GetValueOrDefault()); } var averageTime = totalTime.Divide(retryCount); _logWriter.LogMessage($"Average Response Time: {averageTime.TotalMilliseconds} (milliseconds)"); apiResult.AssertElapsedTime(averageTime, maxElapsedTime); } }