Using Groovy to set a HTTP header value
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2010
05:45 AM
05-07-2010
05:45 AM
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
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2010
06:11 AM
05-07-2010
06:11 AM
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"
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2010
06:46 AM
05-07-2010
06:46 AM
M McDonald,
Thank you, thank you, thank you.
Did exactly what I wanted. What is the purpose for the StringToStringMap object?
Regards
Ian
Thank you, thank you, thank you.
Did exactly what I wanted. What is the purpose for the StringToStringMap object?
Regards
Ian
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2010
06:52 AM
05-07-2010
06:52 AM
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.
Has lots of extra methods beyond a regular HashMap.
"Ignorance more frequently begets confidence than does knowledge"
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2010
02:40 PM
09-17-2010
02:40 PM
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 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!
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2010
06:47 AM
11-10-2010
06:47 AM
I have also same problem.. any solutions sam?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2011
05:35 AM
06-01-2011
05:35 AM
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:
Just set the properties ExampleHeaderName and ExampleHeaderValue for your TestSuite and use the script as "Setup Script" of the TestSuite.
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2013
04:11 AM
07-24-2013
04:11 AM
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
}
}
}
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2016
08:00 AM
06-01-2016
08:00 AM
Thank you so so much! This is great info!
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2017
11:34 AM
08-18-2017
11:34 AM
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) } } } }
