-1

I have requests that require a token. Its passed in the header for the request as a key value pair with the key being access_token and the value being its value. Now different formats of the access token needs to be supported. eg: AccessToken,Access-Token,Access_Token. Is there any way I can verify it?As shown in the attachment, I need to write a test case to check whether all these different variations are supported one by one.

Rohith R Nair
  • 43
  • 1
  • 10

1 Answers1

1

First set up two arrays as environment variable :

pm.environment.set("header",["access","acces2","access3","aaccess4"])

pm.environment.set("header_temp",["access","acces2","access3","aaccess4"])

After that delete the above lines from script and below line to your pre-request script:

replace pass_2 with the request name you are trying

pm.environment.get("header_temp").length === pm.environment.get("header").length ? pm.environment.set("header", pm.environment.get("header_temp")) : null

let header = pm.environment.get("header")
let header_temp = pm.environment.get("header_temp")

if (header.length !== 1) {
    console.log(header.length)
    pm.environment.set("token", header.pop())
    pm.environment.set("header", header)
    postman.setNextRequest("pass_2")
    console.log("pass_2")
} else {
    
     pm.environment.set("token", header.pop())
     pm.environment.set("header", header_temp)
    postman.setNextRequest(null)
    console.log("null")
}

Set header as a variable token:

enter image description here

Now open collection runner and run it , you can see that single request was send 4 times with those 4 headers

You can try below collection that I created:

https://www.getpostman.com/collections/7288ea50c0eca2ebf115

PDHide
  • 18,113
  • 2
  • 31
  • 46