Json 获取令牌并将其作为剩余步骤的授权标头的值发送
在我从Post请求中获得令牌后,如下所示:Json 获取令牌并将其作为剩余步骤的授权标头的值发送,json,rest,groovy,soapui,ready-api,Json,Rest,Groovy,Soapui,Ready Api,在我从Post请求中获得令牌后,如下所示: { "access_token": "12345", "expires_in": 3600, "token_type": "Bearer" } 我想在不同的TestSteps头值中使用这个标记 例如,我必须在收到这个令牌后发出GET请求,并且它在头->authentication:Bearer+token\u值中有 那么,我可以写一个GroovyScript或其他东西来自动实现这一点吗?我用的是ReadyApi 问候,, Adrian为收到上述响应
{ "access_token": "12345", "expires_in": 3600, "token_type": "Bearer" }
我想在不同的TestSteps头值中使用这个标记
例如,我必须在收到这个令牌后发出GET请求,并且它在头->authentication:Bearer+token\u值中有
那么,我可以写一个GroovyScript或其他东西来自动实现这一点吗?我用的是ReadyApi
问候,,
Adrian为收到上述响应的同一步骤添加脚本断言:
脚本断言从响应中获取值,创建项目属性并设置检索到的值
//Check if the response is empty or null
assert context.response, "Response is null or empty"
def json = new groovy.json.JsonSlurper().parseText(context.response)
def token = "${json.token_type} ${json.access_token}" as String
log.info "Token will be: ${token}"
//Assing the value at project level property TOKEN
context.testCase.testSuite.project.setPropertyValue('TOKEN', token)
现在需要将该值动态设置为每个传出请求的头。i、 例如,为SOAP或REST请求类型步骤添加授权头及其值。为此,将使用事件功能。
添加SubmitListener.beforeSubmit事件并将以下脚本添加到其中。请跟随在线评论
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep
import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep
//Please edit the header name as you wanted
def headerName = 'Authorization'
//a method which sets the headers
def setHttpHeaders(def headers) {
def step = context.getProperty("wsdlRequest").testStep
if (step instanceof RestTestRequestStep || step instanceof WsdlTestRequestStep) {
def currentRequest = step.httpRequest
def existingHeaders = currentRequest.requestHeaders
headers.each {
existingHeaders[it.key] = it.value
}
currentRequest.requestHeaders = existingHeaders
} else {
log.info 'not adding headers to the current step as it is not request type step'
}
}
//read the token from project properties
def token = context.expand('${#Project#TOKEN}')
//assert the value of token
assert token, "Token is null or empty"
//UPDATE from the comment to add the header to next request
if (token) {
def headerValue = [(token)]
def headers = [(headerName) : (headerValue)]
setHttpHeaders(headers)
}
我必须做很多测试步骤,我不想在他的头中复制粘贴每个测试步骤的标记。你真的尝试过编写GroovyScript来实现你想要的吗?如果包含您尝试的脚本,但该脚本不起作用,您会得到更好的响应。您能显示包含令牌的原始响应吗?对于Post令牌->响应:{访问令牌:12345,过期令牌:3600,令牌类型:承载者}我不知道如何为此编写GroovyScript…所以我从Get.authentication TestStep创建了一个脚本断言,它可以正常工作。我创建SubmitListener.beforeSubmit事件,我将此代码放在那里。我不必更改标题名,因为在我的步骤GetABC、PutABC等中它是相同的,但它不起作用。我必须再做一次修改吗?例如,我必须在:setHttpHeadersnStepName,headers中介绍我要添加此标题的步骤的名称?我不必更改wsdlRequest?我已将setHttpHeadersnStepName、headers修复为setHttpHeadersheaders。不需要步骤名称,因为每个步骤都需要它。请从上面的答案中选择更新后的脚本。顺便说一句,在给你之前我无法尝试。这是一种使用令牌进行单独步骤的方法吗?为什么,因为在同一个测试套件中,我有其他授权访问的请求……更清楚地说:在同一个测试套件中,我有GetAuthorization令牌,我在3个测试步骤头中使用了这个令牌;在同一个测试套件中,我有另一个请求,来自另一个api的另一个授权。很抱歉,没有收到您的请求。你试过更新答案吗?它至少对预期的步骤有效吗?
import groovy.json.JsonSlurper
import groovy.json.*
def tokens=testRunner.runStepByname("Token")
def response = context.expand( '${Token#Response}' )
def JsonSlurperjsonSlurper = newJsonSlurper()
def Objectresult = jsonSlurper.parseText(response)
def access_token= result.access_token
def authorization = "Bearer "+access_token
testRunner.testCase.setPropertyValue("access_token", authorization)