Forum Discussion

chandanakki's avatar
chandanakki
Occasional Contributor
12 years ago

Automating the cookie in Soap UI

I am testing a Rest API (Post request) using Soap UI 4.6.1 Free Version

When i hit my application url https://qa-application-server.com on the browser,it redirects to this url https://sso-server.com (because of security) where the login page displays, Once i login using the user credentials (Username & Password) it again takes me back to the https://qa-application-server.com

Now in Soap UI tool when i send a POST Request ( This is my 1st request) for this URL https://qa-application-server.com its not giving me proper response but if i update the Cookie manually in Header section of Soap UI i am getting the proper response.I have 2 queries on this

1. Instead of updating the Cookie manually is there any wayout to automate it?

2. Apart from updating the Cookie is there any other wayout to grab the proper response?

Please do the needful.

Thanks,
Chandan

2 Replies

  • stateng's avatar
    stateng
    Occasional Visitor
    I automated this last week for a SOAP API so you can probably adapt what I did. This is my first attempt at programing in java so it was interesting. This is what I used to make it all work:

    To keep track of current credentials, tokens, state and customer I setup "Custom Properties" at the project level. By using the next three commands I could set and read these proeprties:

    def project = com.eviware.soapui.model.support.ModelSupport.getModelItemProject( request )
    String access_token = project.getPropertyValue( "access_token" );
    project.setPropertyValue("token_birth",String.valueOf(token_birth));

    In order to retrieve the token I used the HttpURLConnection library. The following worked for me (I am not a java programmer so if you have constructive criticism please share):

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setDoOutput(true);
    connection.setInstanceFollowRedirects(false);
    // connection.connect();

    OutputStreamWriter out = new OutputStreamWriter( connection.getOutputStream());

    out.write("client_id=" + client_id + "&client_secret=" + client_secret + "&grant_type=client_credentials");
    out.close();

    BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine = "";

    while ((inputLine = input.readLine()) != null) {

    // retrieve access_token - not ideal but this works, look for XML parsing library
    String token_raw = inputLine.split("access_token")[1];
    access_token = token_raw.split("\"")[2];
    log.info("New access_token=" + project.getPropertyValue( "client_name" ) + ":" + access_token);
    project.setPropertyValue("access_token",access_token);

    // retrieve expires_in - not ideal but this works, look for XML parsing library
    String expires_raw = inputLine.split("expires_in")[1];
    String expires_str1 = expires_raw.split(":")[1];
    String expires_str2 = expires_str1.split(",")[0];
    expires_in = Integer.parseInt(expires_str2);
    project.setPropertyValue("expires_in",expires_str2);

    // Save values
    token_birth = now.getTime()/1000;
    project.setPropertyValue("token_birth",String.valueOf(token_birth));
    project.setPropertyValue("last_client_name",project.getPropertyValue( "client_name" ));
    }
    input.close();

    Now it is just a matter of saving the token and placing the value in the header. I found you can place the same value in the header many times so you need to check if it exists first:
    // Add the Authorization header
    def headers = request.requestHeaders
    x = headers.get("Authorization");
    if ( x ) {
    headers.remove("Authorization")
    }
    headers.put( "Authorization", "Bearer " + access_token );
    request.requestHeaders = headers;


    Hope this helps.

    Mike.
  • chandanakki's avatar
    chandanakki
    Occasional Contributor
    Hi Stateng,

    Thanks for your quick reply. I am just a beginner in Soap UI & Java programming.

    I want to run your code using Soap Ui 4.6.1 Free tool. Can you please guide me step by step.

    Thanks In Advance,

    Best Regards,
    Chandan