【问题标题】:Passing JSESSIONID from a SOAP response to a HTTP request in SOAP UI将 SOAP 响应中的 JSESSIONID 传递给 SOAP UI 中的 HTTP 请求
【发布时间】:2017-02-24 20:07:38
【问题描述】:

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

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

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

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

Cookie  |  JSESSIONID=85fc792a71f8eb1e2f0e9c63339e

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

【问题讨论】:

    标签: groovy soapui jsessionid


    【解决方案1】:

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

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

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

    step1 的以下Script Assertion 确实将Cookie 设置为step2。请关注在线 cmets。

    脚本断言:

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

    更新 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
    }
    

    【讨论】:

    • 非常感谢您的回答,明天我会检查您的解决方案并给您反馈。
    • 我试过你的脚本断言解决方案。它成功地将标头从步骤 1(登录 SOAP 响应)设置为步骤 2(HTTP 请求),但似乎 HTTP 请求忽略了它:响应仍然是登录页面而不是请求的页面。 HTTP 响应还有一个 Set-Cookie 标头,其中包含一个 JSESSIONID 但与请求的集合不同。我想应该是一样的。我错了吗?此外,HTTP 响应还有一个名为“Expires”的标头,其值为 1995 年的日期。cookie 在使用之前是否可能已经过期?
    • 清除第 2 步的标题,尤其是 Cookie(如果有)。您尝试了上述哪个脚本?您可以通过这种方式快速检查:首先只运行 step1,然后查看 step2 的 header 中的值是什么。
    • 也许你可以取消选中Maintain HTTP Session并尝试。
    • @TamásG.,你可以看看上面的 cmets 并在这里更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    相关资源
    最近更新 更多