/// <summary> /// Helper method to execute an HTTP request-response /// </summary> /// /// <param name="url">The HTTP Url</param> /// <param name="payload">Byte array conatining the request data</param> /// <param name="requestTimeout">The request timeout value</param> /// <param name="context">The BizUnit context object which holds state and is passed between test steps</param> /// <returns>response MemoryStream</returns> public static MemoryStream SendRequestData(String url, byte[] payload, int requestTimeout, Context context) { WebResponse result = null; MemoryStream response = new MemoryStream(); Stream responseStream = null; Stream requestStream = null; try { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; req.Timeout = requestTimeout; req.ContentType = "text/xml; charset=\"utf-8\""; req.ContentLength = payload.Length; requestStream = req.GetRequestStream(); requestStream.Write( payload, 0, payload.Length ); result = req.GetResponse(); responseStream = result.GetResponseStream(); const int bufferSize = 4096; byte[] buffer = new byte[bufferSize]; int count = responseStream.Read( buffer, 0, bufferSize ); while (count > 0) { response.Write(buffer, 0, count); count = responseStream.Read(buffer, 0, bufferSize ); } response.Seek(0, SeekOrigin.Begin); } catch(Exception e) { context.LogException( e ); throw; } finally { if ( null != result ) { result.Close(); } if ( null != responseStream ) { responseStream.Close(); } if ( null != requestStream ) { requestStream.Close(); } } return response; }
/// <summary> /// ITestStep.Execute() implementation /// </summary> /// <param name='testConfig'>The Xml fragment containing the configuration for this test step</param> /// <param name='context'>The context for the test, this holds state that is passed beteen tests</param> public void Execute(System.Xml.XmlNode testConfig , Context context) { // Using Policy Tester // Retrieve Rule-Set from Policy file string RuleStoreName = context.ReadConfigAsString(testConfig, "RuleStoreName"); string RuleSetInfoCollectionName =context.ReadConfigAsString(testConfig, "RuleSetInfoCollectionName"); string DebugTracking = context.ReadConfigAsString(testConfig, "DebugTracking"); string SampleXML = context.ReadConfigAsString(testConfig, "SampleXML"); string XSD = context.ReadConfigAsString(testConfig, "XSD"); string ResultFile = context.ReadConfigAsString(testConfig, "ResultFile"); RuleStore ruleStore = new FileRuleStore(RuleStoreName); RuleSetInfoCollection rsInfo = ruleStore.GetRuleSets(RuleSetInfoCollectionName, RuleStore.Filter.Latest); if (rsInfo.Count != 1) { // oops ... error throw new ApplicationException(); } RuleSet ruleset = ruleStore.GetRuleSet(rsInfo[0]); // Create an instance of the DebugTrackingInterceptor DebugTrackingInterceptor dti = new DebugTrackingInterceptor(DebugTracking); // Create an instance of the Policy Tester class PolicyTester policyTester = new PolicyTester(ruleset); XmlDocument xd1 = new XmlDocument(); xd1.Load(SampleXML); TypedXmlDocument doc1 = new TypedXmlDocument(XSD, xd1); // Execute Policy Tester try { policyTester.Execute(doc1, dti); } catch (Exception e) { context.LogException(e); throw; } FileInfo f = new FileInfo(ResultFile); StreamWriter w = f.CreateText(); w.Write(doc1.Document.OuterXml); w.Close(); }
/// <summary> /// TestStepBase.Execute() implementation /// </summary> /// <param name='context'>The context for the test, this holds state that is passed beteen tests</param> public override void Execute(Context context) { Thread.Sleep(Timeout); // Get the list of files in the directory string [] filelist = Directory.GetFiles( DirectoryPath, SearchPattern ); if ( filelist.Length == 0) { // Expecting more than one file throw new ApplicationException( String.Format( "Directory contains no files matching the pattern!" ) ); } // For each file in the file list foreach (string filePath in filelist) { context.LogInfo("FileReadMultipleStep validating file: {0}", filePath); Stream fileData = StreamHelper.LoadFileToStream(filePath, Timeout); context.LogData("File: " + filePath, fileData); fileData.Seek(0, SeekOrigin.Begin); // Check it against the validate steps to see if it matches one of them foreach(var subStep in _subSteps) { try { // Try the validation and catch the exception fileData = subStep.Execute(fileData, context); } catch (Exception ex) { context.LogException(ex); throw; } } if(DeleteFiles) { File.Delete(filePath); } } }
private XmlDocument ValidateXmlInstance(Stream data, Context context) { try { var settings = new XmlReaderSettings(); foreach (var xmlSchema in _xmlSchemas) { settings.Schemas.Add(xmlSchema.XmlSchemaNameSpace, xmlSchema.XmlSchemaPath); } settings.ValidationType = ValidationType.Schema; XmlReader reader = XmlReader.Create(data, settings); var document = new XmlDocument(); document.Load(reader); var eventHandler = new ValidationEventHandler(ValidationEventHandler); document.Validate(eventHandler); return document; } catch (Exception ex) { context.LogException(ex); throw; } }
private Stream CallWebMethod( Stream requestData, string serviceUrl, string action, string username, string password, Context ctx ) { try { Stream responseData; BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly); binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; binding.UseDefaultWebProxy = true; EndpointAddress epa = new EndpointAddress(new Uri(serviceUrl)); ChannelFactory<genericContract> cf = null; genericContract channel; Message request; Message response; string responseString; try { cf = new ChannelFactory<genericContract>(binding, epa); cf.Credentials.UserName.UserName = username; cf.Credentials.UserName.Password = password; cf.Open(); channel = cf.CreateChannel(); using (new OperationContextScope((IContextChannel)channel)) { XmlReader r = new XmlTextReader(requestData); request = Message.CreateMessage(MessageVersion.Soap11, action, r); foreach (var header in _soapHeaders) { MessageHeader messageHeader = MessageHeader.CreateHeader(header.HeaderName, header.HeaderNameSpace, header.HeaderInstance); OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader); } response = channel.Invoke(request); string responseStr = response.GetReaderAtBodyContents().ReadOuterXml(); ctx.LogXmlData("Response", responseStr); responseData = StreamHelper.LoadMemoryStream(responseStr); } request.Close(); response.Close(); cf.Close(); } catch (CommunicationException ce) { ctx.LogException(ce); if (cf != null) { cf.Abort(); } throw; } catch (TimeoutException te) { ctx.LogException(te); if (cf != null) { cf.Abort(); } throw; } catch (Exception e) { ctx.LogException(e); if (cf != null) { cf.Abort(); } throw; } return responseData; } catch (Exception ex) { ctx.LogException(ex); throw; } }