private HttpWebRequest SendWebRequest(HTTPTestCase httpTestCase, string RequestMethod, NameValueCollection collHeader) { HttpWebRequest webrequest = null; HTTPBaseClass BaseHttp = null; BaseHttp = new HTTPBaseClass(httpTestCase.Username, httpTestCase.Password, httpTestCase.ProxyServer , httpTestCase.Target, RequestMethod, collHeader); bool allowRedirect = (!string.IsNullOrEmpty(httpTestCase.RedirectLocation)) ? true : false; bool isNetworkCred = (!string.IsNullOrEmpty(httpTestCase.Username) && !string.IsNullOrEmpty(httpTestCase.Password)) ? true : false; return(webrequest = BaseHttp.CreateWebRequest(isNetworkCred, allowRedirect));//false indicates: Http non-secure request }
//Replace curl with C# default httpWebRequest API // todo: async task //private bool RunTestCase(BaseTestCase testCase) //{ // // for now, keep it simple // // we'll use more advanced stuff later // // probably will need to make a runner class still // // for now jam it inline // Console.WriteLine("Running test case {0}", testCase.TestName); // // is a ref... right? // if(testCase is CommandTestCase) // { // //TODO: redirect output? // Console.WriteLine("Command test case"); // CommandTestCase commandTestCase = testCase as CommandTestCase; // Process testCaseProcess = Process.Start(commandTestCase.TestCommand, commandTestCase.Params); // if(testCaseProcess.WaitForExit(60000)) // { // commandTestCase.ActualReturnCode = testCaseProcess.ExitCode; // // TODO: do not assume that it actually finished // commandTestCase.CaseStatus = BaseTestCase.TestCaseStatus.FINISHED; // } // } // else if(testCase is HTTPTestCase) // { // Console.WriteLine("Network (HTTP) test case"); // HTTPTestCase httpTestCase = testCase as HTTPTestCase; // // craft the curl request // // TODO: modular so we may use restsdk? // // first pass: just get requests, no proxy, etc. // ProcessStartInfo startInfo = new ProcessStartInfo(); // //TODO: fix code path // startInfo.FileName = PathToCurl; // startInfo.Arguments = string.Format("-i {0} {1}", httpTestCase.Target, httpTestCase.FollowRedirect ? "-L": ""); // startInfo.RedirectStandardError = true; // startInfo.RedirectStandardOutput = true; // startInfo.UseShellExecute = false; // startInfo.CreateNoWindow = true; // Process process = Process.Start(startInfo); // string output = process.StandardOutput.ReadToEnd(); // // to determine: where to look // string errorOutput = process.StandardOutput.ReadToEnd(); // // TODO: output command line to logs // string combinedOutput = httpTestCase.Target + "\r\n\r\n" + // "Curl arguments:" + startInfo.Arguments + "\r\n\r\n" + // output + "\r\n\r\n" + // errorOutput + "\r\n\r\n"; // OnTestCaseOutputEventHandler(new TestCaseOutputEventArgs(combinedOutput)); // if(process.WaitForExit(60000)) // { // // for now, just output // Console.WriteLine(output); // Console.WriteLine("-------"); // Console.WriteLine(errorOutput); // httpTestCase.CaseStatus = BaseTestCase.TestCaseStatus.FINISHED; // // TEST TEST // // TODO: how to pull out http code // // TODO: if redirect that triggers more https that's not in scope. need to think about this // httpTestCase.ActualResponseCode = CustomStringUtils.GetHTTPCodeFromCurlOutput(output, httpTestCase.FollowRedirect); // httpTestCase.Location = CustomStringUtils.GetLocation(output, httpTestCase.FollowRedirect); // OnTestCaseOutputEventHandler(new TestCaseOutputEventArgs(httpTestCase.Target, // httpTestCase.DidTestCasePass())); // } // // TODO: if psexec how to capture output? // // Another option - just run the whole thing as system... // // worry later // } // else // { // Console.WriteLine("Test case type not yet supported."); // } // return true; //} // todo: async #endregion #region Newly added : HTTPClient patch private bool RunTestCase(BaseTestCase testCase) { string Cookie = string.Empty; string ReUri = string.Empty; string combinedOutput = string.Empty; NameValueCollection collHeader = new NameValueCollection(); HttpWebResponse webresponse = null; if (testCase is CommandTestCase) { CommandTestCase commandTestCase = testCase as CommandTestCase; int status = 0; Dictionary <string, object> result = new Dictionary <string, object>(); switch (commandTestCase.Purpose) { case "InstallExe": status = RunProcessAsAdmin(commandTestCase.Target, commandTestCase.Params); if (status == 1) { combinedOutput = "Logs has been created at C:\\ path"; } break; case "ServiceStatus": ServiceControllerStatus serviceControllerStatus = GetServiceStatus(commandTestCase.Target); if (serviceControllerStatus == ServiceControllerStatus.Running) { status = 1; } else { status = -1; } combinedOutput = string.Format("Service status : {0} ", serviceControllerStatus); break; case "RegistryStatus": result = CheckRegistryExists(commandTestCase.Target); if (result.Count > 0) { status = 1; combinedOutput = "Registry contains following entries :"; foreach (var item in result) { combinedOutput += string.Format("(Key- {0}: Value- {1}) ", item.Key, item.Value); } } else { status = -1; } break; case "ProcessStatus": status = GetProcessStatus(commandTestCase.Target); break; default: break; } commandTestCase.ActualReturnCode = status; commandTestCase.CaseStatus = BaseTestCase.TestCaseStatus.FINISHED; commandTestCase.Description = combinedOutput; SetResponseCode(commandTestCase); //Console ouput combinedOutput = string.Format("Running test case {0}\nResponse StatusCode : {1}\n\tConnection result to {2} : {3}", commandTestCase.TestName, commandTestCase.ActualReturnCode, commandTestCase.Target, (commandTestCase.ExpectedResponseCode == commandTestCase.ActualReturnCode) ? "Pass" : "Fail"); Console.WriteLine(combinedOutput); } else if (testCase is HTTPTestCase) { HTTPTestCase httpTestCase = testCase as HTTPTestCase; try { webresponse = (HttpWebResponse)SendWebRequest(httpTestCase, "GET", collHeader).GetResponse(); int temp = (int)webresponse.StatusCode; //Check if there is any redirected URI if (!string.IsNullOrEmpty(httpTestCase.RedirectLocation)) { ReUri = httpTestCase.RedirectLocation; ReUri = ReUri.Trim(); HTTPBaseClass BaseHttp = new HTTPBaseClass(httpTestCase.Username, httpTestCase.Password, httpTestCase.ProxyServer , ReUri, "POST", collHeader); webresponse = BaseHttp.GetFinalResponse(Cookie, false, true);//set auto redirect flag=true httpTestCase.Location = ReUri; } return(true); } catch (WebException e) { webresponse = (HttpWebResponse)e.Response; if (webresponse != null) { httpTestCase.ActualResponseCode = (int)webresponse.StatusCode; } Console.WriteLine(e.Message); httpTestCase.Description = "Exception message :\n" + e.Message; // Obtain the 'Proxy' mentioned in the LAN settings string defaultProxy = WebRequest.DefaultWebProxy. GetProxy(new Uri(httpTestCase.Target)).Authority; if (!string.IsNullOrEmpty(defaultProxy)) { try { webresponse = (HttpWebResponse)SendWebRequest(httpTestCase, "GET", collHeader).GetResponse(); return(true); } catch (WebException ex) { webresponse = (HttpWebResponse)ex.Response; Console.WriteLine("Default proxy block :" + ex.Message); httpTestCase.Description = ex.Message; } } // Obtain the 'Proxy' mentioned in the Default browser string staticProxy = WebRequest.GetSystemWebProxy(). GetProxy(new Uri(httpTestCase.Target)).Authority; if (!string.IsNullOrEmpty(staticProxy)) { try { webresponse = (HttpWebResponse)SendWebRequest(httpTestCase, "GET", collHeader).GetResponse(); return(true); } catch (WebException exc) { webresponse = (HttpWebResponse)exc.Response; Console.WriteLine("Static proxy block :" + exc.Message); httpTestCase.Description = exc.Message; } } } finally { //Record Actual Status code int resposeCode = 0; if (webresponse != null) { resposeCode = (int)webresponse.StatusCode; httpTestCase.ActualResponseCode = resposeCode; webresponse.Close(); } //Console ouput combinedOutput = string.Format("Running test case {0}\nResponse StatusCode : {1}\n\tConnection result to {2} : {3}", httpTestCase.TestName, httpTestCase.ActualResponseCode, httpTestCase.Target, (httpTestCase.ExpectedResponseCode == httpTestCase.ActualResponseCode) ? "Pass" : "Fail"); Console.WriteLine(combinedOutput); SetResponseCode(httpTestCase); } } return(true); }