cancel
Showing results for 
Search instead for 
Did you mean: 

Add HTTP headerfield to Requests

SOLVED
Highlighted
Contributor

Add HTTP headerfield to Requests

Hi All,

 

I'm using Ready API 1.5.0 for testing REST API calls. Came across this Groovy-script that seems to be useful to add a HTTP-header field to all teststeps. I'm executing it on Project-level fot all TestSuites and TestSteps in that area. The remark that it only should work in 3.5.1 sounds a bit odd to me; executing the script results in the error attached to this message. How can I get this script to work properly in SOAP UI NG?

 

// Add HTTP headers to Requests
// NOTE: will work only in 3.5.1 version

import com.eviware.soapui.impl.wsdl.teststeps.*
import com.eviware.soapui.support.types.StringToStringMap;

def tc = testRunner.testCase.testSuite.getTestCaseByName("<TESTCASE_NAME>")
def ts = tc.getTestStepByName("<TESTSTEP_NAME>")

def headers = new StringToStringMap()

headers.putIfMissing("PmAuthenticationToken","<TOKEN>")
log.info ts.getHttpRequest().setRequestHeaders(headers)

 

1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: Add HTTP headerfield to Requests

def token = new File('C:/temp/SOAPUI/PmAuthenticationToken.txt').text
if (token) {
   request.requestHeaders['PmAuthenticationToken'] = [token]
} else {
   log.error("Token is empty or null")
}


Regards,
Rao.
9 REPLIES 9
Community Hero

Re: Add HTTP headerfield to Requests

You mentioned header needs to be added for the test steps which is not possible if the test step type is jdbc/groovy etc.
So, the question is that please specify if test step is SOAP? or REST or HTTP Request or something else?


Regards,
Rao.
Contributor

Re: Add HTTP headerfield to Requests

I mentioned in my introduction "..testing REST API calls.." and thats still the case. ;-)

 

To be more specific: they are REST Request Test Steps. And because I've about 200 different REST Request Test Steps divided into several (functional) blocks/projects. Also useful, because we're working with more people on these sources.

Community Hero

Re: Add HTTP headerfield to Requests

Ok. If that is the case, then you can use Events feature to handle your case like RequestFilter, add headers before run for each rest so that you do not have to really worry about the number of test cases that it needs to be updated.


Regards,
Rao.
Contributor

Re: Add HTTP headerfield to Requests

This script does the job (temporarily) for me as RequestFilter.filterRequest-event:

 

// Add HTTP headers to all Requests
def headers = request.requestHeaders

headers.put( "AuthenticationToken", "abc123" )
log.info headers
request.requestHeaders = headers 

 

Below the situation of my project. With the "RequestFilter.filterRequest-event" I'm able to add a fixed token-value to the header of TestStep 2. The token is read in TestStep 1 and needed in header of TestStep 2:

 

Project_A

  TestSuite_B

    TestCase_C

        1. DataSourceReadToken (read txt-file)

        2. REST_Request_GET

 

I would expect that "RequestFilter.filterRequest-event" is executed on the level of TestStep 2, so that it can use the propterty (eg. outputvalue)of TestStep 1.

 

As mentioned before: I need this solution for a situation where I have about 50 TestSteps in a few TestCases. So manually changing them is not an option (for now ;-).

 

Any suggestions how to solve this issue?

Community Hero

Re: Add HTTP headerfield to Requests

Can you just clarify below?

  1. Are all 50 test cases have the same two steps?
  2. Is the token different for each test case? or common to all 50 test cases?


Regards,
Rao.
Contributor

Re: Add HTTP headerfield to Requests

Answers:

1. No that is the wrong assumption: "...where I have about 50 TestSteps in a few TestCases."

2. No. The token needs to be generated once and is common for all TestSteps in the TestCases. After generation, token is written to a textfile for futher processing.

 

So I have a separate Step & Case for generating and saving a token. At the moment I have a RequestFilter.filterRequest-event that can add a fixed HEADER field to all the TestSteps. Want to modify this so it will read the txt-file with the token in it and add it to all the TestSteps. This will be more Groovy-script than SoapUI issue. ;-)

 

Contributor

Re: Add HTTP headerfield to Requests

The script that I'm now successfully(!) using as a RequestFilter.filterRequest-event is this one:

 

//reading all txt file at once
File file = new File("C://temp/SOAPUI/PmAuthenticationToken.txt")
fileContent = file.getText()

log.info ("Content = " + fileContent)

// Add HTTP headers to all Requests
def headers = request.requestHeaders

log.info ("Headers = " + headers)

headers.put("PmAuthenticationToken", fileContent)
request.requestHeaders = headers

When executing the script, the header tag and value are added to the REST request. But when I rerun a test, a new headerline PmAuthenticationToken is added (see attachement). Is there any Groovy expert here that can suggest how to solve this issue?

 

Community Hero

Re: Add HTTP headerfield to Requests

def token = new File('C:/temp/SOAPUI/PmAuthenticationToken.txt').text
if (token) {
   request.requestHeaders['PmAuthenticationToken'] = [token]
} else {
   log.error("Token is empty or null")
}


Regards,
Rao.
Contributor

Re: Add HTTP headerfield to Requests

Hi all,

 

In the last period I've updated my Groovy-script (executed as RequestFilter.filterRequest-event) to the one below, where my goal is to add a headervalue only to the requestst that have a specific value in the name.

 

// Define input-file
def token = new File('C:/temp/SOAPUI/PmAuthenticationToken.txt').text
def name = context.getCurrentStep().getLabel()
log.info name

if (name.contains("NOTOKEN")){
	//Skip all Steps with tag NOTOKEN
	log.info ("**NOTOKEN**")
}
else
	{
	log.info ("**TOKEN**")
	if (token) {
	def headers = request.requestHeaders
	headers['PmAuthenticationToken'] = [token]
	request.requestHeaders = headers
   		} 
   	else {
	log.error("File 'C:/temp/SOAPUI/PmAuthenticationToken.txt' is not present")
	}
}

From the Ready! API toolset, I can run this event within the SoapUI NG part with no problems on Project-, TestCase- and TestSuit-level.

But when I go to Projects - in the Ready! API toolset - I recieve an error like:

 

Thu Jan 28 15:32:32 CET 2016:ERROR:groovy.lang.MissingMethodException: No signature of method: com.eviware.soapui.impl.wsdl.WsdlSubmitContext.getCurrentStep() is applicable for argument types: () values: []
groovy.lang.MissingMethodException: No signature of method: com.eviware.soapui.impl.wsdl.WsdlSubmitContext.getCurrentStep() is applicable for argument types: () values: []

Is it possible to modify this GROOVY-script in such a way that is it working both under Projects and in SoapUI NG on Project-, TestCase- and TestSuit-level?

New Here?
Join us and watch the welcome video:
Watch the new Interview
Top Kudoed Authors