如何在SOAPUi中动态地将值设置为标头?

时间:2015-06-04 03:22:14

标签: xpath soapui

我是SoapUI的新手。我想知道如何将2个属性值添加到一个Header值中。

例如,我得到了一些像XML格式的回复:

<Response xmlns="Http://SomeUrl">
    <access_token>abc</access_token>
    <scope>scope1</scope>
    <token_type>Bearer</token_type>
</Response>

我想发送access_token和amp;令牌类型为单个标头值,如:

"Authorization":"Bearer abc"

我没有使用属性转移步骤来了解如何执行此操作。

有人可以帮助我吗?

3 个答案:

答案 0 :(得分:0)

您可以使用XPath concat函数在属性传输步骤中连接一个变量中的两个值,在这种情况下,您可以使用以下XPath:

concat(//*:token_type," ",//*:access_token)

concat函数连接两个或多个字符串,//*:token_type获取Bearer值,//*:access_token获取abc

希望这有帮助,

答案 1 :(得分:0)

在返回上述内容的步骤后添加脚本步骤。

def tokenType = context.expand('${STEP RETURNING STUFF#Response#//Response/token_type}');
def token = context.expand('${STEP RETURNING STUFF#Response#//Response/access_token}');

//add header to all steps
for (def stepEntry : testRunner.testCase.testSteps) {
    if (!(stepEntry.value instanceof com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep)) {
        continue;
    }
    def headers = stepEntry.value.httpRequest.requestHeaders;

    headers.remove("Authorization");
    headers.put("Authorization", token_type + " " + token); 
    stepEntry.value.httpRequest.requestHeaders = headers;
}

答案 2 :(得分:0)

以下是不使用其他属性转移步骤的另一种方法,但使用脚本断言

  1. 为请求测试步骤添加脚本断言。
  2. 将以下代码用于该脚本,需要修改元素XPath
  3. def element1Xpath = '//*:token_type'
    def element2Xpath = '//*:access_token'
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    def response = groovyUtils.getXmlHolder(messageExchange.responseContentAsXml)
    def field1 = response.getNodeValue(element1Xpath)
    def field2 = response.getNodeValue(element2Xpath)
    if (!field1) { throw new Error ("${element1Xpath} is either empty or null")  }
    if (!field1) { throw new Error ("${element2Xpath} is either empty or null")  }
    context.testCase.setPropertyValue('TEMP_PROPERTY', "${field1} ${field2}")
    
    1. 现在预期值(合并)在属性'TEMP_PROPERTY'中可用。您可以根据需要在代码的最后一行重命名属性名称。
    2. 您可以在测试用例中的任何地方使用新的。
相关问题