[TechCorner Challenge #8] How to Clear Cookies in API Request
- 5 years ago
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();
- 5 years ago
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 }