public TestCommander(TestWorker worker, TestWebClient webClient) { if (worker == null) { throw new ArgumentNullException("worker"); } if (webClient == null) { throw new ArgumentNullException("webClient"); } Worker = worker; WebClient = webClient; }
private void Work() { IsRunning = true; StopRequested = false; Stopwatch swHttp = new Stopwatch(), swIteration = new Stopwatch(); TestRequestResult result; DateTime start; string response, scriptBeforeResult, error; WebExceptionStatus errorCode; while (!StopRequested && Iterations < MaxIterations) { Iterations++; swIteration.Start(); using (TestWebClient webClient = new TestWebClient() { Timeout = TestWorker.RequestsTimeout }) { TestCommander commander = new TestCommander(this, webClient); CommandContainerCollection ccc = new CommandContainerCollection() { new CommandContainer(commander, "Commander"), new CommandContainer(this, "Worker"), new CommandContainer(webClient, "WebClient"), new CommandContainer(webClient.CookieContainer, "Cookies"), new CommandContainer(webClient.Headers, "Headers"), }; ccc.SelectContainer("Commander"); foreach (TestRequestInfo request in this.testRequests) { if (StopRequested) { break; } errorCode = WebExceptionStatus.Success; error = null; start = DateTime.Now; ccc.Add(new CommandContainer(request, "Request")); commander.Request = request; try { scriptBeforeResult = request.ScriptBefore == null ? null : (ccc.ExecuteCommand(request.ScriptBefore) ?? "").ToString(); } catch (Exception e) { scriptBeforeResult = e.Message; } IsWaiting = true; try { if (request.PostData == null) { webClient.Headers.Remove(HttpRequestHeader.ContentType); if (commander.Burning) { response = null; error = "Burning"; swHttp.Start(); webClient.DownloadStringAsync(new Uri(request.Url)); webClient.CancelAsync(); } else { swHttp.Start(); response = webClient.DownloadString(request.Url); } } else { webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; if (commander.Burning) { response = null; error = "Burning"; swHttp.Start(); webClient.UploadStringAsync(new Uri(request.Url), request.PostData); webClient.CancelAsync(); } else { swHttp.Start(); response = webClient.UploadString(request.Url, request.PostData); } } } catch (WebException we) { response = null; errorCode = we.Status; error = we.Message; } catch(Exception e) { response = null; errorCode = WebExceptionStatus.UnknownError; error = e.Message; } swHttp.Stop(); IsWaiting = false; result = new TestRequestResult(request) { Duration = swHttp.Elapsed, Start = start, WorkerId = Id, Status = errorCode, Valid = request.ResultValidation == null || response == null ? default(Nullable<bool>) : new Regex(request.ResultValidation, RegexOptions.Singleline).Match(response).Success, Length = response == null ? default(Nullable<int>) : response.Length, Iteration = Iterations, ScriptBeforeResult = scriptBeforeResult, Error = error, }; if (request.ResultDataExtract != null && response != null) { StringBuilder data = new StringBuilder(); Match match = new Regex(request.ResultDataExtract, RegexOptions.Singleline).Match(response); while (match.Success) { data.Append(match.Value); match = match.NextMatch(); if (match.Success) { data.Append(request.ResultDataSeparator); } } result.DataExtracted = data.ToString(); } ccc.StoredValues["Response"] = response; commander.ResponseString = response; ccc.Remove("Result"); ccc.Add(new CommandContainer(result, "Result")); commander.Result = result; try { result.ScriptAfterResult = request.ScriptAfter == null ? null : (ccc.ExecuteCommand(request.ScriptAfter) ?? "").ToString(); } catch (Exception e) { result.ScriptAfterResult = e.Message; } ccc.Remove("Request"); swHttp.Reset(); this.testResults.Add(result); Requests++; } } swIteration.Stop(); LastIterationRunTime = swIteration.Elapsed; swIteration.Reset(); } IsRunning = false; Done = Iterations == MaxIterations; }