Exemplo n.º 1
0
        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());
            }
        }
Exemplo n.º 2
0
        private static void AssertJsonSchema(IMeasuredResponse response, Uri schemaUri)
        {
            var client         = new RestClient();
            var schemaResponse = client.Execute(new RestRequest(schemaUri));

            AssertJsonSchema(response, schemaResponse.Content);
        }
Exemplo n.º 3
0
 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());
     }
 }
Exemplo n.º 4
0
        private static void AssertXmlSchema(IMeasuredResponse response, string schema)
        {
            var schemaSet = new XmlSchemaSet();

            schemaSet.Add(string.Empty, XmlReader.Create(new StringReader(schema)));

            AssertXmlSchema(response, schemaSet);
        }
Exemplo n.º 5
0
 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());
     }
 }
Exemplo n.º 6
0
        private static void AssertXmlSchema(IMeasuredResponse response, Uri schemaUri)
        {
            var schemaSet = new XmlSchemaSet();

            schemaSet.Add(string.Empty, schemaUri.ToString());

            AssertXmlSchema(response, schemaSet);
        }
Exemplo n.º 7
0
 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());
     }
 }
Exemplo n.º 8
0
 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());
     }
 }
Exemplo n.º 9
0
 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());
     }
 }
Exemplo n.º 10
0
 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());
     }
 }
Exemplo n.º 11
0
 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());
     }
 }
Exemplo n.º 12
0
        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);
        }
Exemplo n.º 13
0
        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());
            }
        }
Exemplo n.º 14
0
 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);
     }
 }
Exemplo n.º 15
0
        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);
        }
Exemplo n.º 16
0
        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);
            }
        }
Exemplo n.º 17
0
        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);
        }
Exemplo n.º 18
0
        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);
        }
Exemplo n.º 19
0
        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);
        }
Exemplo n.º 21
0
        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);
        }
Exemplo n.º 22
0
        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);
            }
        }
Exemplo n.º 24
0
 public ApiAssertEventArgs(IMeasuredResponse measuredResponse) => MeasuredResponse = measuredResponse;
Exemplo n.º 25
0
 public static void AssertContentType(this IMeasuredResponse response, string expectedContentType)
 {
     AssertContentTypeEvent?.Invoke(response, new ApiAssertEventArgs(response, $"{expectedContentType}"));
     response.AssertResponseHeader("Content-Type", expectedContentType);
 }
Exemplo n.º 26
0
 public static void AssertContentEncoding(this IMeasuredResponse response, string expectedContentEncoding)
 {
     AssertContentEncodingEvent?.Invoke(response, new ApiAssertEventArgs(response, $"{expectedContentEncoding}"));
     response.AssertResponseHeader("Content-Encoding", expectedContentEncoding);
 }