cancel
Showing results for 
Search instead for 
Did you mean: 

[TechCorner Challenge #8] How to Clear Cookies in API Request

SOLVED
Community Hero

Re: [TechCorner Challenge #8] How to Clear Cookies in API Request

This the script for SubmitListener.beforeSubmit

 

To make the script more dynamic, using project level custom properties to avoid hard coded header and test step names in the script. i.e., user can add the comma separated values to each custom property.

 

For example,

1. add REMOVE_COOKIE_FOR_STEPS property and values as requested, Login Server 2, Get Info Server 2

2. add HEADERS_TO_REMOVE property and value as requested COOKIE

 

Here goes the script:

 

 

//Closure to find if current running step is REST request type
def isRestTestStep = {    
    (it.getProperty('wsdlRequest')?.parent instanceof com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep) ? true : false    
}

//Closure to get the project property value
def getProjectProperty = { context.testCase.testSuite.project.getPropertyValue(it) ?: '' }

//Get the test step names for which defined headers to be removed    
def names = getProjectProperty('REMOVE_COOKIE_FOR_STEPS')?.split(',')*.trim()

//Actual business logic
if ( isRestTestStep(context) && (context.getProperty('wsdlRequest').testStep.name in names)) {
    log.info 'Headers to be removed for this step'
    def eHeaders = submit.request.requestHeaders
    getProjectProperty('HEADERS_TO_REMOVE')?.split(',')*.trim().each {
        eHeaders.remove(it)
    }
    submit.request.requestHeaders = eHeaders
}

 

 



Regards,
Rao.
Community Hero

Re: [TechCorner Challenge #8] How to Clear Cookies in API Request

Hahahaha! And i thought i could do this? Rao and Himanshu proved i wasn't even close!

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Community Hero

Re: [TechCorner Challenge #8] How to Clear Cookies in API Request

@richie, you are modest in saying so. But you do better in lot in your own way.

May be you can try helping by testing the solution.


Regards,
Rao.
Community Hero

Re: [TechCorner Challenge #8] How to Clear Cookies in API Request

Hey @nmrao,

Actually that's a pretty good idea....i can read and understand what most of your code is actually doing, but testing will no doubt help my education as i start getting a little lost when you guys concatenate methods together like the following:

def names = getProjectProperty('REMOVE_COOKIE_FOR_STEPS')?.split(',')*.trim()

I can see what this is doing for the most (i can see why youre using split() amd trim()), but i don't understand what function the '?' or the '*' characters are actually doing so playing around with it will help!

Appreciate the help/advice guys!

Cheers!

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Community Manager

Re: [TechCorner Challenge #8] How to Clear Cookies in API Request

Thank you for the script @nmrao! It is really well-written – Kudos from the SmartBear team!

 

My only comment is similar to the ones I gave in this thread before – looks like it works only for manually-created Headers (because the request.requestHeaders property is used in your script, too).

 

To improve this, you can refer to this article - https://community.smartbear.com/t5/SoapUI-Pro/How-to-get-cookies-in-Groovy-scripts/td-p/171332

Also, here’s the class that can be used to access cookies - https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/CookieStore... 

 

@msiadak @HimanshuTayal you might find the above links useful too🙂


Sonya Mihaljova
Community and Education Specialist

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Community Hero

How to Clear Cookies in API Request

Task: create a Groovy script that will clear the cookies that are sent with the request.

 

This is a solution created for [TechCorner Challenge #8]

 

Thank you, @sonya_m. Those articles provided context that I did not have / understand, and from there I was able to make this work. This script needs to be set as an event script for "RequestFilter.filterRequest" and the target needs to be set as the steps that need to be filtered. Per the original request, an event would need to be created whose target is one of the test steps that needs the cookies cleared. Once that is in place, this script will clear the cookies.

 

 

 

 

 

import org.apache.http.protocol.HttpContext
import com.eviware.soapui.model.iface.SubmitContext
import org.apache.http.impl.client.BasicCookieStore
import org.apache.http.client.protocol.HttpClientContext

HttpContext httpContext = context.getProperty(SubmitContext.HTTP_STATE_PROPERTY);
BasicCookieStore cookieStore = httpContext.getAttribute(HttpClientContext.COOKIE_STORE)

cookieStore.clear();

 

 

 

 

 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!

View solution in original post

Tags (1)
Community Hero

Re: [TechCorner Challenge #8] How to Clear Cookies in API Request

Task: create a Groovy script that will clear the cookies that are sent with the request.

 

This is a solution created for [TechCorner Challenge #8]

 

@sonya_m Thanks for the feedback.

 

Completely ignored about automatic Cookie's earlier.

 

Here is updated one which covers both automatic and manual set cookies

 

This the script for SubmitListener.beforeSubmit

 

To make the script more dynamic, using project level custom properties to avoid hard coded header and test step names in the script. i.e., user can add the comma separated values to each custom property.

 

For example,

1. add REMOVE_COOKIE_FOR_STEPS property and values as requested, Login Server 2, Get Info Server 2

2. add HEADERS_TO_REMOVE property and value as requested COOKIE

 

UPDATE: made few changes

 

 

 

//Closure to get the project property value
def getProjectProperty = { context.testCase.testSuite.project.getPropertyValue(it) ?: '' }
 
//Get the test step names for which defined headers to be removed
def names = getProjectProperty('REMOVE_COOKIE_FOR_STEPS')?.split(',')*.trim()

//Actual business logic
if ( context.getProperty('wsdlRequest').testStep.name in names) {
    //Removes automatic Cookie's
    context.'#HTTP_STATE'.getAttribute('http.cookie-store').clear()

    //Removes manual COOKIE
    def eHeaders = submit.request.requestHeaders
    getProjectProperty('HEADERS_TO_REMOVE')?.split(',')*.trim().each {
        eHeaders.remove(it)
    }
    submit.request.requestHeaders = eHeaders
}

 

 

NOTE: the same is having issues with Pro for SubmitListener.beforeSubmit event.

Works in free edition (tested in 5.4v)with soapUIExtensions

 

Script for RequestFilter.filterRequest

 

 

//Closure to get the project property value
def getProjectProperty = { context.testCase.testSuite.project.getPropertyValue(it) ?: '' }
 
//Get the test step names for which defined headers to be removed
def names = getProjectProperty('REMOVE_COOKIE_FOR_STEPS')?.split(',')*.trim()

//Actual business logic
if ((context.getProperty('wsdlRequest').testStep.name in names)) {
    
    //Removes automatic Cookie's
    context.'#HTTP_STATE'.getAttribute('http.cookie-store').clear()

    def eHeaders = request.requestHeaders
    getProjectProperty('HEADERS_TO_REMOVE')?.split(',')*.trim().each {
        eHeaders.remove(it)
    }
    request.requestHeaders = eHeaders
}

 

 



Regards,
Rao.

View solution in original post

Tags (1)
Community Hero

Re: [TechCorner Challenge #8] How to Clear Cookies in API Request

Simplifying to one-liner

To remove automatic cookies, below script can be used either in RequestFilter.filterRequest or SubmitListener.beforeSubmit events with Target step names as required

 

 

context.'#HTTP_STATE'.getAttribute('http.cookie-store').clear()

 

NOTE: the same is having issues with Pro for SubmitListener.beforeSubmit event.

Works in free edtion (tested in 5.4v)with soapUIExtensions



Regards,
Rao.
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors