private static void AssertXmlSchema(IMeasuredResponse response, XmlSchemaSet xmlSchemaSet) { _xmlSchemaValidationErrors = new List <string>(); var trimmedContent = response.Content.TrimStart(); var xml = new XmlDocument(); xml.LoadXml(trimmedContent); xml.Schemas.Add(xmlSchemaSet); xml.Validate(ValidationCallBack); if (_xmlSchemaValidationErrors.Any()) { var sb = new StringBuilder(); sb.AppendLine("XML Schema is not valid. Error Messages:"); foreach (var errorMessage in _xmlSchemaValidationErrors) { sb.AppendLine(errorMessage); } throw new ApiAssertException(sb.ToString()); } }
private static void AssertJsonSchema(IMeasuredResponse response, Uri schemaUri) { var client = new RestClient(); var schemaResponse = client.Execute(new RestRequest(schemaUri)); AssertJsonSchema(response, schemaResponse.Content); }
public static void AssertContentNotEquals(this IMeasuredResponse response, string content) { AssertContentNotEqualsEvent?.Invoke(response, new ApiAssertEventArgs(response, content)); if (response.Content.Equals(content)) { throw new ApiAssertException($"Request's Content was equal to {content}.", response.ResponseUri.ToString()); } }
private static void AssertXmlSchema(IMeasuredResponse response, string schema) { var schemaSet = new XmlSchemaSet(); schemaSet.Add(string.Empty, XmlReader.Create(new StringReader(schema))); AssertXmlSchema(response, schemaSet); }
public static void AssertExecutionTimeUnder(this IMeasuredResponse response, int seconds) { AssertExecutionTimeUnderEvent?.Invoke(response, new ApiAssertEventArgs(response, seconds.ToString())); if (response.ExecutionTime.TotalSeconds > seconds) { throw new ApiAssertException($"Request's execution time {response.ExecutionTime.TotalSeconds} was over {seconds}.", response.ResponseUri.ToString()); } }
private static void AssertXmlSchema(IMeasuredResponse response, Uri schemaUri) { var schemaSet = new XmlSchemaSet(); schemaSet.Add(string.Empty, schemaUri.ToString()); AssertXmlSchema(response, schemaSet); }
public static void AssertContentNotContains(this IMeasuredResponse response, string contentPart) { AssertContentNotContainsEvent?.Invoke(response, new ApiAssertEventArgs(response, contentPart)); if (response.Content.Contains(contentPart)) { throw new ApiAssertException($"Request's Content contained {contentPart}.", response.ResponseUri.ToString()); } }
public static void AssertCookieExists(this IMeasuredResponse response, string cookieName) { AssertCookieExistsEvent?.Invoke(response, new ApiAssertEventArgs(response, $"{cookieName}")); if (!response.Cookies.Any(x => x.Name.Equals(cookieName))) { throw new ApiAssertException($"Response's cookie with name {cookieName} was not present.", response.ResponseUri.ToString()); } }
public static void AssertStatusCode(this IMeasuredResponse response, HttpStatusCode statusCode) { AssertStatusCodeEvent?.Invoke(response, new ApiAssertEventArgs(response, statusCode.ToString())); if (response.StatusCode != statusCode) { throw new ApiAssertException($"Request's status code {response.StatusCode} was not equal to {statusCode}.", response.ResponseUri.ToString()); } }
public static void AssertSuccessStatusCode(this IMeasuredResponse response) { AssertSuccessStatusCodeEvent?.Invoke(response, new ApiAssertEventArgs(response, "2**")); if ((int)response.StatusCode <= 200 && (int)response.StatusCode >= 299) { throw new ApiAssertException($"Request's status code was not successful - {response.StatusCode}.", response.ResponseUri.ToString()); } }
public static void AssertResultEquals <TResultType>(this IMeasuredResponse <TResultType> response, TResultType result) where TResultType : IEquatable <TResultType>, new() { AssertResultEqualsEvent?.Invoke(response, new ApiAssertEventArgs(response, result.ToString())); if (!response.Data.Equals(result)) { throw new ApiAssertException($"Request's Data was not equal to {result}.", response.ResponseUri.ToString()); } }
public override List <ResponseAssertionResults> Execute(HttpRequestDto httpRequestDto, IMeasuredResponse response) { ResponseAssertionResultsCollection.Clear(); if (httpRequestDto.ResponseAssertions.Count > 0) { foreach (var responseAssertion in httpRequestDto.ResponseAssertions) { var responseAssertionResults = new ResponseAssertionResults { AssertionType = responseAssertion.ToString() }; var htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(response.Content); if (_loadTestLocators.Any(x => x.LocatorType.Equals(responseAssertion.Locator))) { var currentLocator = _loadTestLocators.First(x => x.LocatorType.Equals(responseAssertion.Locator)); try { LoadTestElement htmlElement = currentLocator.LocateElement(htmlDocument, responseAssertion.LocatorValue); if (_loadTestEnsureHandler.Any(x => x.EnsureType.Equals(responseAssertion.AssertionType))) { var currentEnsureHandler = _loadTestEnsureHandler.First(x => x.EnsureType.Equals(responseAssertion.AssertionType)); responseAssertionResults = currentEnsureHandler.Execute(htmlElement, responseAssertion.ExpectedValue); } else { responseAssertionResults.FailedMessage = $"AssertionType {responseAssertion.AssertionType} is not supported."; responseAssertionResults.Passed = false; } } catch (Exception ex) { responseAssertionResults.FailedMessage = ex.Message; responseAssertionResults.Passed = false; } } else { responseAssertionResults.FailedMessage = $"Locator {responseAssertion.Locator} is not supported."; responseAssertionResults.Passed = false; } ResponseAssertionResultsCollection.Add(responseAssertionResults); } } return(ResponseAssertionResultsCollection); }
public static void AssertCookie(this IMeasuredResponse response, string cookieName, string cookieValue) { AssertCookieEvent?.Invoke(response, new ApiAssertEventArgs(response, $"{cookieName}={cookieValue}")); response.AssertCookieExists(cookieName); var cookie = response.Cookies.FirstOrDefault(x => x.Name.Equals(cookieName)); if (!cookie.Value.Equals(cookieValue)) { throw new ApiAssertException($"Response's cookie with name {cookieName}={cookie.Value} was not equal to {cookieName}={cookieValue}.", response.ResponseUri.ToString()); } }
public static void AssertSchema(this IMeasuredResponse response, Uri schemaUri) { AssertSchemaEvent?.Invoke(response, new ApiAssertEventArgs(response, string.Empty)); if (response.Request.RequestFormat == DataFormat.Json) { AssertJsonSchema(response, schemaUri); } else { AssertXmlSchema(response, schemaUri); } }
public override void TestsAct() { var getResponse = _apiClientService.Get <Genres>(_getRequest); getResponse.Data.Name = "Unique Title"; _putRequest.AddJsonBody(getResponse.Data); _apiClientService.Put <Genres>(_putRequest); _putResponse = _apiClientService.Get <Genres>(_getRequest); }
public static void AssertSchema(this IMeasuredResponse response, Uri schemaUri) { AssertSchemaEvent?.Invoke(response, new ApiAssertEventArgs(response, string.Empty)); #pragma warning disable CS0618 // Type or member is obsolete if (response.Request.RequestFormat == DataFormat.Json) #pragma warning restore CS0618 // Type or member is obsolete { AssertJsonSchema(response, schemaUri); } else { AssertXmlSchema(response, schemaUri); } }
public static dynamic AsDynamicContent(this IMeasuredResponse response) { if (response.Request.RequestFormat == DataFormat.Xml) { var doc = XDocument.Parse(response.Content); var contentToBeConverted = JsonConvert.SerializeXNode(doc); return(contentToBeConverted); } dynamic contentAsDynamic = JObject.Parse(response.Content); return(contentAsDynamic); }
private static void AssertJsonSchema(IMeasuredResponse response, string schemaContent) { JSchema jsonSchema; try { jsonSchema = JSchema.Parse(schemaContent); } catch (Exception ex) { throw new ArgumentException("Schema is not valid schema", ex); } AssertJsonSchema(response, jsonSchema); }
public static void AssertResponseHeader(this IMeasuredResponse response, string headerName, string headerExpectedValue) { AssertResponseHeaderEvent?.Invoke(response, new ApiAssertEventArgs(response, $"{headerName}")); var headerParameter = response.Headers.FirstOrDefault(x => x.Name.ToLower().Equals(headerName.ToLower())); if (headerParameter == null) { throw new ArgumentNullException($"No header was present with name {headerName}"); } if (headerParameter.Value.ToString() != headerExpectedValue) { throw new ApiAssertException($"Response's header {headerName} with value {headerParameter.Value} was not equal to {headerExpectedValue}.", response.ResponseUri.ToString()); } }
public static dynamic AsDynamicContent(this IMeasuredResponse response) { #pragma warning disable CS0618 // Type or member is obsolete if (response.Request.RequestFormat == DataFormat.Xml) #pragma warning restore CS0618 // Type or member is obsolete { var doc = XDocument.Parse(response.Content); var contentToBeConverted = JsonConvert.SerializeXNode(doc); return(contentToBeConverted); } dynamic contentAsDynamic = JObject.Parse(response.Content); return(contentAsDynamic); }
public override List <ResponseAssertionResults> Execute(HttpRequestDto httpRequestDto, IMeasuredResponse response) { ResponseAssertionResultsCollection.Clear(); var responseAssertionResults = new ResponseAssertionResults { AssertionType = "StatusCode is SUCCESS", Passed = true }; if ((int)response.StatusCode <= 200 && (int)response.StatusCode >= 299) { responseAssertionResults.FailedMessage = $"Request's status code was not successful - {response.StatusCode} {response.ResponseUri}."; } ResponseAssertionResultsCollection.Add(responseAssertionResults); return(ResponseAssertionResultsCollection); }
private static void AssertJsonSchema(IMeasuredResponse response, JSchema jsonSchema) { IList <string> messages; var trimmedContent = response.Content.TrimStart(); bool isSchemaValid = trimmedContent.StartsWith("{", StringComparison.Ordinal) ? JObject.Parse(response.Content).IsValid(jsonSchema, out messages) : JArray.Parse(response.Content).IsValid(jsonSchema, out messages); if (!isSchemaValid) { var sb = new StringBuilder(); sb.AppendLine("JSON Schema is not valid. Error Messages:"); foreach (var errorMessage in messages) { sb.AppendLine(errorMessage); } throw new ApiAssertException(sb.ToString()); } }
private void UpdateCookiesCollection(IMeasuredResponse response) { foreach (var setCookieHeader in response.Headers.Where(x => x.Name.Equals("Set-Cookie"))) { if (!string.IsNullOrEmpty(setCookieHeader.Value?.ToString())) { string setCookieValue = setCookieHeader.Value.ToString(); string cookieSetValuePart = setCookieValue.Split(';').First().Trim(); string cookieName = cookieSetValuePart.Split('=').First(); string cookieNewValue = cookieSetValuePart.Split('=').Last(); string cookieExpiresPart = setCookieValue.Split(';').FirstOrDefault(x => x.Contains("expires"))?.Trim(); if (!string.IsNullOrEmpty(cookieExpiresPart)) { var cookieExpirationDate = DateTime.Parse(cookieExpiresPart.Split('=').Last()); if (cookieExpirationDate > DateTime.Now) { CreateOrUpdateCookieInLocalCollection(cookieName, cookieNewValue); } else if (_scenarioCookies.ContainsKey(cookieName)) { _scenarioCookies.Remove(cookieName); } } else { CreateOrUpdateCookieInLocalCollection(cookieName, cookieNewValue); } } } foreach (var responseCookie in response.Cookies) { CreateOrUpdateCookieInLocalCollection(responseCookie.Name, responseCookie.Value); } }
public ApiAssertEventArgs(IMeasuredResponse measuredResponse) => MeasuredResponse = measuredResponse;
public static void AssertContentType(this IMeasuredResponse response, string expectedContentType) { AssertContentTypeEvent?.Invoke(response, new ApiAssertEventArgs(response, $"{expectedContentType}")); response.AssertResponseHeader("Content-Type", expectedContentType); }
public static void AssertContentEncoding(this IMeasuredResponse response, string expectedContentEncoding) { AssertContentEncodingEvent?.Invoke(response, new ApiAssertEventArgs(response, $"{expectedContentEncoding}")); response.AssertResponseHeader("Content-Encoding", expectedContentEncoding); }