Reputation: 737
I have a test case which performs a login via a SOAP request and the response includes this header:
Set-Cookie | JSESSIONID=85fc792a71f8eb1e2f0e9c63339e; Path=/somepath; HttpOnly
After that I have a HTTP request to an URL which only can be accessed if login was succesful. Although I have set the 'Maintain HTTP session' to true in TestCase Options, the JSESSIONID cookie is not passed to my HTTP request. The HTTP request is performed without a JSESSIONID therefore the response is not the requested URL but the login page. I guess it is because the login process is a SOAP request not HTTP.
I tried to handle the issue with a groovy script: I was able to capture the JSESSIONID from the SOAP response and set it as
Cookie | JSESSIONID=85fc792a71f8eb1e2f0e9c63339e
to my HTTP request but the response is again the login page not the requested page. Any idea how to resolve this issue? SOAP UI version is 5.2.1
Upvotes: 1
Views: 2978
Reputation: 21379
Assuming that the test case has two test steps with below names:
step1
response contains Set-Cookie
in response header. And the step2
needs to send above Cookie
as part of request headers.
The below Script Assertion
for step1
does set Cookie
to step2
. Please follow in-line comments.
Script Assertion:
/**
* This script assertion reads the http response,
* collect the cookies for the next http request
* Provide the next step name where you want to set the Cookie to the request
**/
//Change the name of the test step2 below as per your test
def nextStepName = 'step2'
//Assert if the step1 response has headers
assert messageExchange.responseHeaders, "Response does not have headers"
//Get the next request using test step name
def nextRequest = context.testCase.testSteps[nextStepName].httpRequest
//Get the existing headers
def headers = nextRequest.requestHeaders
//Get Cookie from step1 response and create headers
if (messageExchange.responseHeaders.containsKey('Set-Cookie')) {
log.info "Found Cookie in the response headers"
def cookiez = messageExchange.responseHeaders['Set-Cookie'].value
def list = []
cookiez.each { cookies ->
list.add(cookies.toString())
}
headers['Cookie'] = list
} else {
log.warn "Not Found Cookie in the response headers"
}
//Set the headers for step2
nextRequest.requestHeaders = headers
Update 1
Here is improved Script Assertion
which allows you to be able to extend very easily:
/**
* This is the Script Assertion
* which sets headers to the requested targeted steps
* by extracting header from current step response
**/
//Assert if response has headers
assert messageExchange.responseHeaders, "Response does not have any headers"
//Specify all the headers to retrieve from current test step response as keys, target step request headers as values
//key - current response header name
//value - target request header name
//Add more key, values into map if you need to extract and set more headers
def headerMap = ['Set-Cookie' : 'Cookie']
//Specify the test step name for which headers to be set. Change step name as needed.
//Add call to setHttpHeaders with different test step names as needed to apply for more steps
setHttpHeaders('step2', headerMap)
/**
* method sets headers to targeted step
* step is the step name for which headers to be set
* header map consists key, header name in the current step and value, header name to appear in the
* targeted step
*
**/
def setHttpHeaders(def step, def headerMap) {
def nextRequest = context.testCase.testSteps[step]?.httpRequest
def existingHeaders = nextRequest?.requestHeaders
headerMap.each {
existingHeaders[it.value] = getHttpHeaderValue(it.key)
}
nextRequest?.requestHeaders = existingHeaders
}
/**
* method to retrieve the value of the specified header
**/
def getHttpHeaderValue(def headerToLookup) {
if (messageExchange.responseHeaders.containsKey(headerToLookup)) {
log.info "Found ${headerToLookup} in the response headers"
return messageExchange.responseHeaders[headerToLookup]
} else {
log.warn "${headerToLookup} is not found in the response headers"
}
null
}
Upvotes: 1