Adrian
Adrian

Reputation: 13

Get the token and send it as value for Authorization header for the rest of the steps

After I get a token from a Post request as shown below:

{ "access_token": "12345", "expires_in": 3600, "token_type": "Bearer" } 

I want to use this token in different TestSteps Headers values.

For example I have to make a GET request after I received this token and it have in the header -> Authentification : Bearer + token_value.

So can I write a GroovyScript or something to make this automatically? I'm using ReadyApi.

Regards, Adrian

Upvotes: 1

Views: 6772

Answers (2)

SQA
SQA

Reputation: 47

 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)

Upvotes: 0

Rao
Rao

Reputation: 21379

Add Script Assertion for the same step where you receive the mentioned response:

Script Assertion this fetches the values from response and creates a project property and set the retrieved value.

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

Now the value needs to be set as header to each outgoing request dynamically. i.e., Add Authorization header and its value for the SOAP or REST request type steps. For this, Events feature is going to be used. Add a SubmitListener.beforeSubmit event and add the below script into it. Please follow the comments inline.

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)
}

Upvotes: 2

Related Questions