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

时间:2016-10-15 12:59:38

标签: groovy soapui jsessionid

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

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

之后,我有一个URL请求,只有登录成功才能访问该URL。 虽然我已经设置了“维护HTTP会话”#39;如果在TestCase选项中为true,则JSESSIONID cookie不会传递给我的HTTP请求。 HTTP请求在没有JSESSIONID的情况下执行,因此响应不是请求的URL而是登录页面。我想这是因为登录过程是SOAP请求而不是HTTP。

我试图用一个groovy脚本处理这个问题:我能够从SOAP响应中捕获JSESSIONID并将其设置为

Cookie  |  JSESSIONID=85fc792a71f8eb1e2f0e9c63339e

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

1 个答案:

答案 0 :(得分:1)

假设测试用例有两个以下名称的测试步骤:

  • step1(SOAP请求测试步骤)
  • step2(HTTP请求测试步骤)

class MyCustomAttribute : Attribute { private int[] _values; public MyCustomAttribute(params int[] values) { _values = values; } } [MyCustomAttribute(3, 4, 5)] class MyClass { } 响应在响应标头中包含step1。并且Set-Cookie需要在step2之上发送请求标题。

以下Cookie Script Assertion确实将step1设置为Cookie。请按照在线评论。

脚本断言:

step2

更新1

以下是改进版/** * 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