检查 groovy 中的响应 header
Checking Response header in groovy
我正在尝试测试响应 header 是否包含 X-Duration-MS 文本。我很确定如果 X-Duration-MS 不在 header 上,我检查断言的方式缺少主要检查。你能帮我测试一下吗?
这是我的代码:
def httpResponseHeaders = context.testCase.testSteps["Testname"].testRequest.response.responseHeaders
//log.info (httpResponseHeaders)
httpResponseHeaders.each
{
k,v ->
if(k=~"X-Duration-MS"){
assert k == "X-Duration-MS"
}
}
使用 groovy 脚本
您正在检查是否存在一个 X-Duration-MS
响应 header,尽管其值。然后你可以在 httpResponseHeaders
上使用 keySet()
函数来获取所有 http header 名称,然后使用 contains()
检查你想要的 header 是否在列表中.所以你可以试试:
def httpResponseHeaders = context.testCase.testSteps["Testname"].testRequest.response.responseHeaders
assert httpResponseHeaders.keySet().contains("X-Duration-MS")
使用脚本断言
您还可以在请求中使用 Script assertion
而不是 Groovy
testStep 来实现您的目标。在您的请求 UI 的左下角有一个 Assertion(0)
文本,单击它然后使用断言面板中的 +
按钮添加一个 Script Assertion
。然后在这个脚本中你可以做你在 Groovy testStep 中做的同样的事情,而不需要找到 testStep 来获得响应,因为在 Script Assertion
中是上下文中的 messageExchange
object,您可以使用它来获取当前请求、响应、http headers 的详细信息...所以您可以简单地使用:
def httpResponseHeaders = messageExchange.getResponseHeaders()
assert httpResponseHeaders.keySet().contains("X-Duration-MS")
我正在尝试测试响应 header 是否包含 X-Duration-MS 文本。我很确定如果 X-Duration-MS 不在 header 上,我检查断言的方式缺少主要检查。你能帮我测试一下吗?
这是我的代码:
def httpResponseHeaders = context.testCase.testSteps["Testname"].testRequest.response.responseHeaders
//log.info (httpResponseHeaders)
httpResponseHeaders.each
{
k,v ->
if(k=~"X-Duration-MS"){
assert k == "X-Duration-MS"
}
}
使用 groovy 脚本
您正在检查是否存在一个 X-Duration-MS
响应 header,尽管其值。然后你可以在 httpResponseHeaders
上使用 keySet()
函数来获取所有 http header 名称,然后使用 contains()
检查你想要的 header 是否在列表中.所以你可以试试:
def httpResponseHeaders = context.testCase.testSteps["Testname"].testRequest.response.responseHeaders
assert httpResponseHeaders.keySet().contains("X-Duration-MS")
使用脚本断言
您还可以在请求中使用 Script assertion
而不是 Groovy
testStep 来实现您的目标。在您的请求 UI 的左下角有一个 Assertion(0)
文本,单击它然后使用断言面板中的 +
按钮添加一个 Script Assertion
。然后在这个脚本中你可以做你在 Groovy testStep 中做的同样的事情,而不需要找到 testStep 来获得响应,因为在 Script Assertion
中是上下文中的 messageExchange
object,您可以使用它来获取当前请求、响应、http headers 的详细信息...所以您可以简单地使用:
def httpResponseHeaders = messageExchange.getResponseHeaders()
assert httpResponseHeaders.keySet().contains("X-Duration-MS")