Forum Discussion

kristoffer's avatar
kristoffer
New Contributor
12 years ago

preserving cookies

I'm trying to read cookies.
Most pages i find when searching seems to link to http://siking.wordpress.com/2013/07/25/ ... anagement/ so thats what im trying to use.

I have created a test case with the following steps:
1) REST request to post data to /A_Page
2) Groovy script

The groovy script contains:
import com.eviware.soapui.impl.wsdl.support.http.HttpClientSupport
import org.apache.http.impl.cookie.BasicClientCookie

// Get cookie store and cookies
def myCookieStore = HttpClientSupport.getHttpClient().getCookieStore();
def myCookies = myCookieStore.getCookies();

log.info("parse cookies " + myCookies.size());
myCookies.each {
log.info(it.name);
}

assert sessionCookie;


The problem is that the collection returned from getCookies() contains 0 items.
If i check the HTTP log window i can see that the response includes Set-Cookie headers; so items should be returned.
I can also see the Set-Cookie headers in the headers-tab if i execute the rest-step by itself.

Do i need to activate some sort of cookie setting somewhere for the getCookies() to return the cookies; which are obviously sent?


/Kristoffer

2 Replies

  • SiKing's avatar
    SiKing
    Community Expert
    You need to have "Maintain HTTP session" turned on in the TestCase Options.
    Also, check the Raw Request tab, you should see something like
    Cookie: JSESSIONID=96dff7752ed75070b38317ce4936

    HTH.
  • kristoffer's avatar
    kristoffer
    New Contributor
    Yea, i just figured that out too. I was looking for a checkbox called "cookie"-something.
    I guess if i use cookies to identify sessions this option will maintain them. (In my case I do, so this works for me)

    Just to clarify; does "Maintain HTTP session" do anything else than preserving cookies?
    Or could the name of this option really be "Maintain HTTP cookies"?

    And one more note. Even though i enable this option i still cannot iterate the cookies using the above code. Since the time will come when i will need to get cookie-values i dug into this problem. It seems that while the "Maintain HTTP session" DO keep track of the cookies, it doesn't do it using the cookieStore returned by getHttpClient().getCookieStore().

    The following code works for me in the end. Do note that this still requires the "Maintain HTTP session" option to be on.
    // Define variable to hold sessionCookie value if we find it.
    def sessionCookie;

    final httpStatePropertyName = com.eviware.soapui.model.testsuite.TestRunContext.HTTP_STATE_PROPERTY;
    final httpContext = context.getProperty(httpStatePropertyName);
    assert httpContext;

    // Get cookie store
    final cookieStore = httpContext.getAttribute("http.cookie-store");
    log.info("cookieStore: " + cookieStore);

    // Get cookies from store
    def cookies = cookieStore.getCookies();
    cookies.each {
    log.info(it.name);
    if (it.name == "ASP.NET_SessionId"){
    sessionCookie = it.value;
    }
    }

    assert sessionCookie;
    log.info(sessionCookie)