Ask a Question

Using Groovy to set a HTTP header value

iangfox1965
Occasional Contributor

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
9 REPLIES 9
M_McDonald
Super Contributor

Re: Using Groovy to set a HTTP header value

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)
"Ignorance more frequently begets confidence than does knowledge"
iangfox1965
Occasional Contributor

Re: Using Groovy to set a HTTP header value

M McDonald,

Thank you, thank you, thank you.

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

Regards

Ian
M_McDonald
Super Contributor

Re: Using Groovy to set a HTTP header value

First time I've noticed it. You can look at http://www.soapui.org/apidocs/com/eviware/soapui/support/types/StringToStringMap.html

Has lots of extra methods beyond a regular HashMap.
"Ignorance more frequently begets confidence than does knowledge"
Sam
Not applicable

Re: Using Groovy to set a HTTP header value

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!
kamahade
Regular Contributor

Re: Using Groovy to set a HTTP header value

I have also same problem.. any solutions sam?
FelixNawroth
New Contributor

Re: Using Groovy to set a HTTP header value

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.
renatoathaydes
Contributor

Re: Using Groovy to set a HTTP header value

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
}
}
}
NadiaLinares
Regular Visitor

Re: Using Groovy to set a HTTP header value

Thank you so so much! This is great info!

anand7892
Occasional Contributor

Re: Using Groovy to set a HTTP header value

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)
   }
}
}
}

 

cancel
Showing results for 
Search instead for 
Did you mean: