Forum Discussion

StevenC's avatar
StevenC
Contributor
10 years ago

Clearing request headers

I am testing a system where we have multiple client configurations for the web services, some of which require authentication others do not. I have my framework set up so that whether security is enabled or disabled is configurable with a single switch and that is all working.

The problem I am having is when I switch from a security on to a security off configuration I want to be able to remove the authentication headers from the soap calls automatically in groovy (otherwise the request fails for a bad session token) .

I have tried several incarnations of setting them to null or to an empty StringToStringMap/StringToStringsMap and they all failed as well as the following...

pfdRequest.setRequestHeaders(pfdRequest.getRequestHeaders().clear())


which failed because it could not determine whether the input was a StringToStringsMap or a StringToStringMap

So how do I remove all the headers from a soap call using groovy?

3 Replies

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hello,

    Moving away from groovy for a sec, have you looked into using the environments functionality, this will allow you to run the same tests with different configurations (like different security settings) and does not require any scripting. Please take a look at the link below and let me know what you think:

    http://www.soapui.org/Working-with-Proj ... oapui.html

    Regards,
    Temil
  • That in no way shape or form helps me add or remove the authentication token to/from the soap request dynamically at runtime.

    The calls are not using authentication in the soap calls, there is an explicit open session soap call that returns an authentication cookie that needs to be attached to the headers of every subsequent soap call. Environments will not dynamically determine whether to run openSession, nor will they extract the returned token and attach it to every subsequent soap call.

    To give you an idea what I am talking about here is the code that attaches the headers...

      def step = testRunner.runTestStepByName( "openSession")

    def authorizationHeader = new StringBuilder()
    def credentials = new StringBuilder()

    def cookieValue = testRunner.testCase.testSteps["openSession"].testRequest.response.responseHeaders["Set-Cookie"][0]
    authHeaders.put("Cookie", cookieValue.toString())

    authorizationHeader.append("License")
    authorizationHeader.append(" ")

    credentials.append(testRunner.testCase.testSuite.project.getPropertyValue( "validLicensedUserLogin" ))
    credentials.append(":")
    credentials.append(testRunner.testCase.testSuite.project.getPropertyValue( "validCustomerLicenseGuid" ))

    byte[] bytes = Base64.encodeBase64(credentials.toString().getBytes())
    authorizationHeader.append(new String(bytes))

    authHeaders.put("Authorization", authorizationHeader.toString())


    So how do I remove them dynamically? I can't set them to Null, and I can't set them to an empty stringtostrings object so what can I do?
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,
    You can use this code in a RequestFilter.filterRequest event handler which will be triggered when the a request is run, or incorporate it in your Groovy script:

    import com.eviware.soapui.support.types.StringToStringMap
    def headers = new StringToStringMap()
    request.requestHeaders = headers


    To add custom headers, please refer to this tip: http://www.soapui.org/Scripting-Properties/tips-a-tricks.html#12-add-custom-http-header-to-requests

    Regards,

    Giscard
    SmartBear Support