Forum Discussion

rka's avatar
rka
Occasional Contributor
9 years ago
Solved

Maintain HTTP Session: Should pass cookies to following request

Hi, I was reading this community forum for a long time about maintaining https session over several requests.

But I'm not able to run it sucessfully.

 

I'm on Ready!API 1.5.0  (SoapUI NG Pro) Build Date: 20151106-1037

 

All I want to do is

1.) Request A contains in its response 3 cookies (within the http header)

2.) I would like to have this 3 cookies transfered in Request B (also in the http header)

 

It's as simple, but it doesn't work.

I tried:  to pass the cookie around, go to the testcase options and turn on "maintain HTTP session".

Simply enable this option to true does not work

I also tried to use a property transfer, but how can I easily pass all cookies?

I don't want to write groovie code. I think such basic requirement should be handled by the tool itself.

 

Maybe someone has an idea?

 

Thanks

Regards, Reto

  • In your REST request, you should add those 3 header fields (you don't need to supply a value):

    1.JPG

     

    Then in the actual test step, double click the value field of each, right-click, then use 'Get Data' to get each header from the response of a previous request:

    2.png

  • rka's avatar
    rka
    9 years ago

    Hi Justin

    Thanks for that hint.

    But it doesn't work in my case.

     

    a) my first request contains in it's response:

    Set-Cookie: JSESSIONID=xxxxxxxxxxxx; Path=/yyyyyyyyyyyyyyyy; Secure
    Set-Cookie: WAF-XSRF-TOKEN=zzzzzzzzzzzzzzzz; Path=/
    Set-Cookie: XSRF-TOKEN=aaaaaaaaaaaaaaaaa; Path=/
    Access-Control-Allow-Headers: origin, content-type, accept, X-XSRF-TOKEN

     

    b) I have to transfer those cookies to the next request

     

    Now I try with a groovy script

    Actually I think it would work, but it douplicates all cookies in the header, each time I start the script ??

     

    def header = testRunner.testCase.getTestStepByName("firstLoginCookie").httpRequest.response.responseHeaders["Set-Cookie"]
     header=header.toString()

    log.info(" Current XSRF-TOKEN header: " + header)
     start=header.indexOf(" XSRF-TOKEN=")
    log.info(" Current XSRF-TOKEN start: " + start+1)

     end=header.indexOf("; Path=/]")
    log.info(" Current XSRF-TOKENend: " + end)

     testCaseProperty= header.substring(start+1,end)
     vXsrf=testCaseProperty
    log.info("Current xsrf: " + vXsrf) 

    ////////////////

    log.info(" Current WAF-XSRF-TOKEN header: " + header)
     start=header.indexOf(" WAF-XSRF-TOKEN=")
    log.info(" Current WAF-XSRF-TOKEN start: " + start+1)

     end=header.indexOf("; Path=/, ")
    log.info(" Current WAF-XSRF-TOKEN end: " + end)

     testCaseProperty= header.substring(start+1,end)
     vWafXsrf=testCaseProperty
    log.info("Current wafxsrf: " + vWafXsrf) 

    ////////////////

    log.info(" Current JSESSIONID header: " + header)
     start=header.indexOf("JSESSIONID=")
    log.info(" Current JSESSIONID start: " + start)

     end=header.indexOf("; Path=/com.avaloq")
    log.info(" Current JSESSIONID end: " + end)

     testCaseProperty= header.substring(start,end)
     vJsessionId=testCaseProperty
    log.info("Current vJsessionId: " + vJsessionId) 


    // Iterate through all test steps and add the session cookie to the headers.
    for ( tstep in context.testCase.testSteps ) {
     testStepName = ( tstep.key )
      log.info("Current testStepName: " + testStepName);

     if ( testStepName != 'firstLoginCookie' && testStepName != 'SetXSRFheadersForAll') {
      log.info("Cookie set for " + testStepName)
     def headers=testRunner.testCase.testSteps[testStepName].testRequest.requestHeaders 
     headers.put('Cookie', vXsrf) 
     headers.put('Cookie', vWafXsrf)
     headers.put('Cookie', vJsessionId +'; Path=/com.avaloq.afs.rest.services; Secure')
     //testRunner.testCase.testSteps[testStepName].testRequest.requestHeaders=headers
     testRunner.testCase.testSteps[testStepName].testRequest.requestHeaders=headers

     }
    }

     

    Thanks

    Regards, Reto

     

8 Replies

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi Reto,

     

    You should see your cookies in Get Data (right-click the mouse in the request editor and select Get Data from the context menu). Did you try it?

    • rka's avatar
      rka
      Occasional Contributor

      Hi Tanya

      Where exactly do you mean to use the "get Data" for the http header?

       

      I have a REST request.

      On the Request Window:

      a) I can manually add headers on the "Headers" tab - but this is a dialog, no "get Data".

      b) In the outline section, there is the request body, not the header, "get Data" would work there, but makes no sense because I would like to set http headers.

       

      Thanks

      Regards, Reto

      • JustinM89's avatar
        JustinM89
        Contributor

        In your REST request, you should add those 3 header fields (you don't need to supply a value):

        1.JPG

         

        Then in the actual test step, double click the value field of each, right-click, then use 'Get Data' to get each header from the response of a previous request:

        2.png

    • JustinM89's avatar
      JustinM89
      Contributor

      Also, do you have 'Maintain HTTP session' enabled for your test case? I believe that will handle passing your session amongst the test steps.

       

      Nevermind, I see you've already done that

      • rka's avatar
        rka
        Occasional Contributor

        Thanks to all of you

        Adding all this informations together, I could solve my problem.

        Now I use  the groovy script to extract the cookie values and a project property to write it into.

        In the following request I just need to read out this project property in the header values XSRF-TOKEN=${#Project#XSRF-TOKEN_PROPERTY}

         

        Thanks

        Regards, Reto