Forum Discussion

TDYSmartBear's avatar
TDYSmartBear
Occasional Contributor
8 years ago

Use testCase custom property to create dynamic request header in event

I created an event to use RequestFilter.filterRequest to populate REST call headers. Most of the testCases use "Accept, application/json" as header while some special testCases require "Accept, application/octet-stream". I include a custom property "Accept" in the special testCases and hope when event gets executed the Accept header can be populated dynamically. But the actual execution completely skip the if-else section. May I know did I miss anything?

 

 

import com.eviware.soapui.support.types.StringToStringMap

 

def headers = new StringToStringMap()

headers.put("Authorization", "Basic abcxyz123456....")
headers.put("Cache-Control", "no-cache")
headers.put("Pragma", " no-cache")
headers.put("Expires", " Sat, 01 Jan 2000 00:00:00 GMT")
headers.put("Content-Type", "application/json")

 

if(testRunner.testCase.hasProperty("Accept")) {
  headers.remove("Accept")
  headers.put("Accept", "application/octet-stream")
} else {
  headers.put("Accept", "application/json")
}

request.setRequestHeaders(headers)

2 Replies

  • avidCoder's avatar
    avidCoder
    Super Contributor

    I think you should try below code:-

     

    import com.eviware.soapui.support.types.StringToStringMap
    
    def headers = new StringToStringMap()
    
    headers.put("Authorization", "Basic abcxyz123456....")
    headers.put("Cache-Control", "no-cache")
    headers.put("Pragma", " no-cache")
    headers.put("Expires", " Sat, 01 Jan 2000 00:00:00 GMT")
    headers.put("Content-Type", "application/json")
    
    boolean str = testRunner.testCase.testSteps["Your Request"].testRequest.requestHeaders["Accept"]
    log.info str
    if(str) {
    	log.info "123"
      headers.remove("Accept")
      headers.put("Accept", "application/octet-stream")
    } else {
      headers.put("Accept", "application/json")
    }

    It will go to if loop now.

    • TDYSmartBear's avatar
      TDYSmartBear
      Occasional Contributor

      Thank you for the help! By using Swagger and that solved the problem. :)