将SOAP响应中的JSESSIONID传递给SOAP UI中的HTTP请求 [英] Passing JSESSIONID from a SOAP response to a HTTP request in SOAP UI
问题描述
我有一个通过SOAP请求执行登录的测试用例,响应中包含这个头文件:
Set-Cookie | JSESSIONID = 85fc792a71f8eb1e2f0e9c63339e;路径= / somepath; HttpOnly
之后,我有一个HTTP请求到一个只有登录成功才能被访问的URL。
虽然我在TestCase Options中将维护HTTP会话设置为true,但JSESSIONID cookie不会传递给我的HTTP请求。 HTTP请求在没有JSESSIONID的情况下执行,因此响应不是请求的URL,而是登录页面。我想这是因为登录过程是一个SOAP请求而不是HTTP。
我试图用groovy脚本来处理这个问题:我能够从SOAP响应并将其设置为
Cookie |对我的HTTP请求JSESSIONID = 85fc792a71f8eb1e2f0e9c63339e
,但是响应再次是登录页面而不是请求的页面。任何想法如何解决这个问题?
SOAP UI版本为5.2.1假设测试用例有两个测试步骤,其名称如下: / p>
step1
response包含 Set-Cookie
在回应标题。 step2
需要发送上面的 Cookie
作为请求标题的一部分。
下面的 脚本断言: 更新1 这里改进了 I have a test case which performs a login via a SOAP request and the response includes this header: 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 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 Assuming that the test case has two test steps with below names: The below Script Assertion: Update 1 Here is improved 脚本断言用于
step1
不会设置 Cookie
到 step2
。请按照在线评论。
/ **
*此脚本声明读取http响应,
*收集下一个http请求的cookie
*提供您要设置的下一步名称对请求的Cookie
** /
//根据您的测试更改以下测试步骤2的名称
def nextStepName ='step2'
//判断step1响应是否有头文件
assert messageExchange.responseHeaders,Response没有头文件
//使用测试步骤名称获取下一个请求
def nextRequest = context.testCase.testSteps [nextStepName] .httpRequest
//获取现有标题
def headers = nextRequest.requestHeaders
//从第1步获取Cookie响应并创建头文件
if(messageExchange.responseHeaders.containsKey('Set-Cookie')){
log.info在响应头文件中找到Cookie
def cookiez = messageExchange.re sponseHeaders ['Set-Cookie']。value
def list = []
cookiez.each {cookies - >
list.add(cookies.toString())
}
headers ['Cookie'] = list
} else {
log.warnNot Found Cookie in响应标题
//设置步骤2的标题
nextRequest.requestHeaders =标题
Script Assertion
,它可以让你很容易地扩展:
/ b>
/ **
*这是脚本断言
*,它通过从当前步骤响应中提取标题将标题设置为请求的目标步骤
*
** /
//断言响应是否有头文件
断言messageExchange.responseHeaders,响应没有任何头文件
//指定从当前测试步骤响应中检索的所有头文件e作为键,将步骤请求标题定位为值
//键 - 当前响应标题名称
//值 - 目标请求标题名称
//如果需要,可将更多键值添加到映射中提取并设置更多标题
def headerMap = ['Set-Cookie':'Cookie']
//指定要设置标题的测试步骤名称。根据需要更改步骤名称。
//根据需要添加对具有不同测试步骤名称的setHttpHeaders的调用以申请更多步骤
setHttpHeaders('step2',headerMap)
/ **
*方法将标题设置为目标步骤
*步骤是要设置标题的步骤名称
*标题映射包含键,当前步骤中的标题名称和值,要显示的标题名称
*目标步骤
*
** /
def setHttpHeaders(def step,def headerMap){
def nextRequest = context.testCase.testSteps [step]? .httpRequest $ b $ def def existingHeaders = nextRequest?.requestHeaders
headerMap.each {
existingHeaders [it.value] = getHttpHeaderValue(it.key)
}
nextRequest ?. requestHeaders = existingHeaders
}
/ **
*方法检索指定头文件的值
** /
def getHttpHeaderValue(def headerToLookup) {
if(messageExchange.responseHeaders.containsKey(headerToLookup)){
log.info在响应头文件中找到$ {headerToLookup}
return messageExchange.responseHeaders [headerToLookup]
} else {
log.warn$ {headerToLookup}在响应中找不到头文件
null
}
Set-Cookie | JSESSIONID=85fc792a71f8eb1e2f0e9c63339e; Path=/somepath; HttpOnly
Cookie | JSESSIONID=85fc792a71f8eb1e2f0e9c63339e
step1
response contains Set-Cookie
in response header. And the step2
needs to send above Cookie
as part of request headers.Script Assertion
for step1
does set Cookie
to step2
. Please follow in-line comments./**
* 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
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
}
这篇关于将SOAP响应中的JSESSIONID传递给SOAP UI中的HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!