/// <summary> /// Adds a retry policy to the <see cref="WebTestRequest"/>. /// </summary> /// <param name="retryTestType">The <see cref="RetryTestType"/>.</param> /// <param name="value">The value to be used in the retry test.</param> /// <param name="maxRetries">The max number of retries.</param> /// <param name="interval">The interval in milliseconds between each retry.</param> /// <param name="webApiRequest">The <see cref="WebTestRequest"/> action.</param> /// <param name="asserts">The <see cref="ValidationRule"/> actions.</param> /// <returns>Returns the current instance of <see cref="TestRequests"/>.</returns> public TestRequests Retry(RetryTestType retryTestType, string value, int maxRetries, int interval, Func<WebTestRequest> webApiRequest, params Func<ValidationRule>[] asserts) { Contract.Requires(maxRetries >= 0, "The max retries cannot be less than 0."); Contract.Requires(interval >= 0, "The retry interval cannot be less than 0."); Contract.Requires(webApiRequest != null, "The web api request cannot be null."); var testRequest = new TestRequest { Request = webApiRequest, Asserts = asserts, RetryTestType = retryTestType, RetryValue = value, MaxRetries = maxRetries, Interval = interval }; Requests.Add(testRequest); return this; }
/// <summary> /// Main Enumerator for the <see cref="WebTest"/>. /// </summary> /// <returns></returns> public override IEnumerator<WebTestRequest> GetRequestEnumerator() { WebTestRequests(); foreach (var testRequest in testRequests.Requests) { currentTestRequest = testRequest; currentRequest = testRequest.Request(); currentWebTestOutcome = Outcome; currentRequest.ValidationRuleReferences.Clear(); foreach (var assert in testRequest.Asserts) { currentRequest.ValidateResponse += assert().Validate; } this.AddCommentToResult("Blah"); if (testRequest.MaxRetries == 0) { if (testRequest.WaitPeriod > 0) { Thread.Sleep(testRequest.WaitPeriod); } yield return currentRequest; } else { var retryValue = testRequest.RetryValue; if (testRequest.RetryValueDelegate != null) { retryValue = testRequest.RetryValueDelegate.Invoke(); } currentRequest.PostRequest += CurrentRequestOnPostRequest; for (var i = 0; i <= testRequest.MaxRetries; i++) { //TestValidateRetry(testRequest, i); currentRequest.ValidateResponse += new AssertRetryValidationRule(testRequest.RetryTestType, retryValue).Validate; yield return currentRequest; if (currentTestRequest.RetryTestType == RetryTestType.StatusCodeEquals && (int)LastResponse.StatusCode == int.Parse(retryValue)) { break; } if (currentTestRequest.RetryTestType == RetryTestType.BodyEquals && LastResponse.BodyString == retryValue) { break; } if (currentTestRequest.RetryTestType == RetryTestType.BodyIncludes && LastResponse.BodyString.IndexOf(retryValue, StringComparison.Ordinal) > -1) { break; } if (currentTestRequest.RetryTestType == RetryTestType.BodyDoesNotInclude && LastResponse.BodyString.IndexOf(retryValue, StringComparison.Ordinal) == -1) { break; } Thread.Sleep(testRequest.Interval); } } } AddFinalOutput(); }
private void TestValidateRetry(TestRequest testRequest, int i) { if (i == 0) { currentRequest.Url = currentRequest.Url + "/askj"; } if (i == testRequest.MaxRetries - 1) { currentRequest.Url = currentRequest.Url.Replace("/askj", ""); } }
/// <summary> /// Forces the main thread to wait before running the <see cref="WebTestRequest"/>. /// </summary> /// <param name="milliseconds">The number of milliseconds to wait before proceeding with the <see cref="WebTestRequest"/>.</param> /// <param name="webApiRequest">The <see cref="WebTestRequest"/> action.</param> /// <param name="asserts">The <see cref="ValidationRule"/> actions.</param> /// <returns>Returns the current instance of <see cref="TestRequests"/>.</returns> public TestRequests Wait(int milliseconds, Func<WebTestRequest> webApiRequest, params Func<ValidationRule>[] asserts) { Contract.Requires(milliseconds != 0); Contract.Requires(webApiRequest != null, "The web api request cannot be null."); var testRequest = new TestRequest {Request = webApiRequest, Asserts = asserts, WaitPeriod = milliseconds}; Requests.Add(testRequest); return this; }