将 JSESSIONID 从 SOAP 响应传递到 SOAP UI 中的 HTTP 请求

2023-12-01

我有一个通过 SOAP 请求执行登录的测试用例,响应包含以下标头:

Set-Cookie  |   JSESSIONID=85fc792a71f8eb1e2f0e9c63339e; Path=/somepath; HttpOnly

之后,我向 URL 发出 HTTP 请求,只有登录成功才能访问该 URL。 尽管我已在 TestCase 选项中将“维护 HTTP 会话”设置为 true,但 JSESSIONID cookie 未传递到我的 HTTP 请求。 HTTP 请求是在没有 JSESSIONID 的情况下执行的,因此响应不是请求的 URL,而是登录页面。我猜这是因为登录过程是 SOAP 请求而不是 HTTP。

我尝试使用常规脚本来处理该问题:我能够从 SOAP 响应中捕获 JSESSIONID 并将其设置为

Cookie  |  JSESSIONID=85fc792a71f8eb1e2f0e9c63339e

我的 HTTP 请求,但响应又是登录页面而不是请求的页面。知道如何解决这个问题吗? SOAP UI 版本是 5.2.1


假设测试用例有两个测试步骤,名称如下:

  • 步骤 1(SOAP 请求测试步骤)
  • 步骤 2(HTTP 请求测试步骤)

step1响应包含Set-Cookie在响应头中。还有step2需要发送上面Cookie作为请求标头的一部分。

下面Script Assertion for step1确实设置了Cookie to step2。请关注内嵌评论。

脚本断言:

/**
* 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

这里有改进Script Assertion这使您能够非常轻松地扩展:

  • 到当前阶跃响应中任意数量的标头
  • 根据需要进行任意数量的测试步骤
/**
 * 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
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 JSESSIONID 从 SOAP 响应传递到 SOAP UI 中的 HTTP 请求 的相关文章

随机推荐