Forum Discussion

rka's avatar
rka
Occasional Contributor
9 years ago

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) Buil...
  • JustinM89's avatar
    JustinM89
    9 years ago

    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