Forum Discussion

iangfox1965's avatar
iangfox1965
Occasional Contributor
15 years ago

Using Groovy to set a HTTP header value

Hi,

I have several test cases in a test suite. Each test case consists of at least one Groovy test step followed by a REST test step for which I need to manually set a http header value.

I am using Test Suite Properties  to store the values that I want to insert into a header.

What I want to know is the Groovy code I need to create a custom Header field and value in my REST test step please. A nice simple example would help me considerably.

Many thanks

Ian
  • Please allow me to refactor the above script and extend it to work with REST TestSteps as well:

    import com.eviware.soapui.impl.wsdl.teststeps.*

    def valueOf = { key -> testSuite.getPropertyValue( key ) }

    testSuite.testCaseList.each { testCase ->
    testCase.testStepList.each { testStep ->
    if( testStep instanceof WsdlTestRequestStep || testStep instanceof RestTestRequestStep ) {
    def headers = [ (valueOf( "ExampleHeaderName" )) : [ valueOf( "ExampleHeaderValue" ) ] ]
    log.info("Setting HTTP headers ${headers} in test case ${testCase.label}")
    testStep.testRequest.requestHeaders = headers
    }
    }
    }
    • anand7892's avatar
      anand7892
      Occasional Contributor

      Here is more precise solution to this:

      Problem: I wanted to add a header with name "Authorization" and value "${#project#token}"(stored at project level)'

      Solution:

       

      Run Groovy script and it will solve your problem.

       

      i

      import com.eviware.soapui.impl.wsdl.teststeps.*
      import com.eviware.soapui.support.types.StringToStringMap 
      
      def project = context.testCase.testSuite.project
        
      def token = "\${"+"#"+"Project"+"#"+"token}"
      
      ${#project#token}
      log.info(token)
      def valueOf = { key -> testSuite.getPropertyValue( key ) }
      project.testSuiteList.each{ testSuite ->
      testSuite.testCaseList.each { testCase ->
         testCase.testStepList.each { testStep ->
            if( testStep instanceof WsdlTestRequestStep || testStep instanceof RestTestRequestStep  ) 
            {
            	def headers = new StringToStringMap()
      		headers.put("Authorization",token)
               log.info("Setting HTTP headers ${headers} in test case ${testCase.label}")
               testStep.testRequest.setRequestHeaders(headers)
         }
      }
      }
      }

       

  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    Like this?

    import com.eviware.soapui.support.types.StringToStringMap 

    def headers = new StringToStringMap()
    headers.put("param","value")
    testRunner.testCase.getTestStepByName("REST Test Request").testRequest.setRequestHeaders(headers)
  • iangfox1965's avatar
    iangfox1965
    Occasional Contributor
    M McDonald,

    Thank you, thank you, thank you.

    Did exactly what I wanted. What is the purpose for the StringToStringMap object?

    Regards

    Ian
  • Hello,

    I recently downloaded SoapUI 3.6. I was using SoapUI 3.5.1. When running my groovy script I am getting an error.
    ERROR:groovy.lang.MissingMethodException: No signature of method: com.eviware.soapui.impl.wsdl.teststeps.RestTestRequest.setRequestHeaders() is applicable for argument types: (com.eviware.soapui.support.types.StringToStringMap) values:
    Possible solutions: setRequestHeaders(com.eviware.soapui.support.types.StringToStringsMap), getRequestHeaders()
    I am able to run the same groovy script in 3.5.1 and it works fine. Did I miss something?

    testRunner.testCase.getTestStepByName("testCase1").testRequest.setRequestHeaders(header)

    Any ideas?

    Thanks!
  • I ran into the same problem. The solution is simple: There is both StringToStringMap and StringToStringsMap in com.eviware.soapui.support.types - the method expects the plural variant.

    Here's an adaption of M McDonald's script (combined with code from https://thewonggei.wordpress.com/2010/08/05/configure-http-basic-auth-once-for-soapui-test-suties/) to write TestSuite properties into the headers of all called TestRequests:

    import com.eviware.soapui.impl.wsdl.teststeps.*
    import com.eviware.soapui.support.types.StringToStringsMap

    for( testCase in testSuite.getTestCaseList() ) {
    for( testStep in testCase.getTestStepList() ) {
    if( testStep instanceof WsdlTestRequestStep ) {
    def headers = new StringToStringsMap()
    headers.put(testSuite.getPropertyValue("ExampleHeaderName"),testSuite.getPropertyValue("ExampleHeaderValue"))

    log.info("Setting HTTP headers ${headers} in test case ${testCase.getLabel()}")
    testStep.getTestRequest().setRequestHeaders(headers)
    }
    }
    }


    Just set the properties ExampleHeaderName and ExampleHeaderValue for your TestSuite and use the script as "Setup Script" of the TestSuite.