Hi,
This new thing I'm testing has some new OAuth security and the first thing I need to do is grab some cookies from the request.
I've never done this before - I know - weird - right?
Anyway - I have a blank payload but plenty of headers
There are 4 headers entitled Set-Cookie and each of these 4 'Set-Cookie' headers have different values - please see screenshot below
Typically if I need to capture header values for later use - I have a bit of groovy to do this (as follows:)
//Takes one of the elements of the response Header def value = testRunner.testCase.testSteps["REST Request)"].testRequest.response.responseHeaders["headername"] //Read this value into a parameter - writes the header value into the Properties test step def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ) groovyUtils.setPropertyValue("Properties", "headername",value[0])
HOWEVER - the above is never going to work - I need to capture each of the 4 'Set-Cookie' values and they each have the same name?!??!
Can anyone please advise how I can grab these cookies?
Many thanks!
richie
zx
Solved! Go to Solution.
That was just for my testing -- you would just replace that with:
testRunner.testCase.testSteps["GET Cookies"].testRequest.response.responseHeaders["Set-Cookie"]
Hi,
My bad - I found a post by @nmrao from way back answering this (link) - I'm trying to tailor the script accordingly to exactly what I need - so please don't anyone waste any time looking at this - I "might" be able to sort myself out - I'll come back with either questions or to close off the ticket.
ta,
richie
Thanks for sharing the link, @richie !
We are looking forward to your updates.
You may have already figured it out, but it looks like each value has a unique identifier you could use. If you need to reference each one by name, you could do something like this:
def headers = testRunner.testCase.testSteps["REST Request)"].testRequest.response.responseHeaders
headers.each {
if (it.getKey() == 'Set-Cookie') { // assuming this is a map
def valueParts = it.getValue().split('=') // Split the value on the equals
testRunner.testCase.setPropertyValue(valueParts[0], it.getValue())
}
}
// This would get you the following test case properties:
testCase.getPropertyValue('returnurl') // returnurl=9d97d277ed656a2...
testCase.getPropertyValue('authtype') // authtype=72b76f4f5cf...
testCase.getPropertyValue('state') // state=81ef1f0ff48fcd...
testCase.getPropertyValue('nonce') // nonce=7511a89aaacda...
Or, if you just need them all and don't need to reference each one by name, you could just throw them in a list:
import groovy.json.*
def headers = testRunner.testCase.testSteps["REST Request)"].testRequest.response.responseHeaders def cookieList = [] headers.each { if (it.getKey() == 'Set-Cookie') { // assuming this is a map cookieList.add(it.getValue()) } }
// This would get you: ["returnurl=9d97d277ed656a2", "authtype=72b76f4f5cf", "state=81ef1f0ff48fcd", "nonce=7511a89aaacda"]
// Store as test case property -- can only store String, that's why we serialize the List to JSON
testRunner.testCase.setPropertyValue("cookieList", new JsonBuilder(cookieList).toString())
// To use in another Groovy script
String cookieListStr = testRunner.testCase.getPropertyValue("cookieList")
def cookieList = new JsonSlurper().parseText(cookieListStr) // Deserialize JSON back into a List
Hey @JustinM89
I got stuck on something else - but I'm circling back to this tomorrow - so I'll be looking at it then - thanks so much for your help - its far more than I would've expected!
cheers,
richie
Hi,
I haven't forgotten about this - I'm still working on this and other things, so cant close off the ticket as yet,
cheers,
richie
Hi,
I tried all the scripts provided but I found a couple of problems with a couple of them - I managed to fix all but the last one - but the I'm a bit stuck on the code that writes out the cookies to the Properties step
the script for saving the 'Set-Cookie' values to the Properties step is as below:
import groovy.json.* def headers = testRunner.testCase.testSteps["REST Request"].testRequest.response.responseHeaders headers.each { if (it.getKey() == 'Set-Cookie') { // assuming this is a map def valueParts = it.getValue().split('=') // Split the value on the equals testRunner.testCase.setPropertyValue(valueParts[0], it.getValue()) } } // This would get you the following test case properties: testCase.getPropertyValue('returnurl') // returnurl=9d97d277ed656a2... testCase.getPropertyValue('authtype') // authtype=72b76f4f5cf... testCase.getPropertyValue('state') // state=81ef1f0ff48fcd... testCase.getPropertyValue('nonce') // nonce=7511a89aaacda...
HOWEVER - this returned the following error
groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.split() is applicable for argument types: (java.lang.String) values: [=] Possible solutions: split(groovy.lang.Closure), split(groovy.lang.Closure), split(groovy.lang.Closure), wait(), init(), init() error at line: 5
so I think there's either a missing closing bracket (even though it looks fine to me - or perhaps its the wrong type (string to list???)
I'm sorry - I just have no idea how to handle this - can anyone advise?
I did get the other scripts working including @nmrao's to grab the cookies and pass directly onto another step - but I've been told I might need to save the cookies off for other use - so I really need to park the cookies into a Properties step if possible?
Grateful for all and any help as always! 🙂
Cheers,
richie
as
in this line:
def valueParts = it.getValue().split('=') // Split the value on the equals
I assumed that it.getValue() would return a String, but it appears to be returning an ArrayList. I would just log that thing (log.info it.getValue()) to see exactly what's in there
Hey @JustinM89
I tried as you said - i.e. tried logging the it.getValue() - but the runtime array warning launched before the logging executed - I commented out the one line and tried as below
headers.each { if (it.getKey() == 'Set-Cookie') { // assuming this is a map // def valueParts = it.getValue().split('=') // Split the value on the equals log.info (it.getValue()) testRunner.testCase.setPropertyValue(valueParts[0], it.getValue()) } }
and the returned value were the 4 setcookie values in a comma separated Arraylist as follows:
Tue Jun 18 22:32:20 BST 2019: INFO: [returnurl=41c9c733b4b7ae87cebea0077307969d0b2b6dbe00e140f51d72451b6128c9eb2bc9cefbd881a83d9784c9fe99345c0875a521b73e23c8739ee54ccfa84265c4f0fdaac6b6b55cfc944efd1c07f615d4-870dc80d4da43c6f; Max-Age=3600; Expires=Tue, 18-Jun-2019 22:27:50 GMT; Path=/; Secure; HttpOnly, authtype=61da3a97b24f98368adaef05552f37c3304467f5d2c02d740b244e0d8623d0a1-67c8bfd955a10235; Max-Age=3600; Expires=Tue, 18-Jun-2019 22:27:50 GMT; Path=/; Secure; HttpOnly, state=4128c393917003b9f7a110723aa1a436bfcb4b60f3af5ead5dec55da0cfa5005f9236299dc7294de335ce5906c30805c7f5d236a58899559d67e82262981348a-dfb8e5192d506a03; Max-Age=3600; Expires=Tue, 18-Jun-2019 22:27:50 GMT; Path=/; Secure; HttpOnly, nonce=80989400c4004d4975a29077c7914e75b9ccc2d65408b8aae968f87bce4faf4b-3fbe6f333efd76e3; Max-Age=3600; Expires=Tue, 18-Jun-2019 22:27:50 GMT; Path=/; Secure; HttpOnly]
I sorry - I dont know how to proceed to change the type from list to string - I'm guessing you might throw in a .toString() operator....but that's all my tiny brain can manage..... 🙂
I did try altering the code to
def headers = testRunner.testCase.testSteps["GET Cookies"].testRequest.response.responseHeaders headers.each { if (it.getKey() == 'Set-Cookie') { // assuming this is a map // def valueParts = it.getValue().split('=') // Split the value on the equals
def valueParts = (it.getValue()).toString().split('=') //I commented out the above line and replaced it with this one
log.info (valueParts) testRunner.testCase.setPropertyValue(valueParts[0], it.getValue()) } } //def (name, value) = cookies.toString().split('=',2) // This would get you the following test case properties: testCase.getPropertyValue('returnurl') // returnurl=9d97d277ed656a2... testCase.getPropertyValue('authtype') // authtype=72b76f4f5cf... testCase.getPropertyValue('state') // state=81ef1f0ff48fcd... testCase.getPropertyValue('nonce') // nonce=7511a89aaacda...
but I got the following response:
groovy.lang.MissingMethodException: No signature of method: com.eviware.soapui.impl.wsdl.WsdlTestCasePro.setPropertyValue() is applicable for argument types: (java.lang.String, java.util.ArrayList) values: [[returnurl, [returnurl=881d9208ca983dcf9a2cc1971d02a8465eefd47cccdeafb751826e947204b8626f505fa6cf1e06dd9f9ceed91a8f0390dfbdf4077b308be6eb91306b6620ad2fd92d06c68387cddcc87a19c9909218dc-87b5428103f85588; Max-Age=3600; Expires=Tue, 18-Jun-2019 22:58:30 GMT; Path=/; Secure; HttpOnly, ...]] Possible solutions: setPropertyValue(java.lang.String, java.lang.String), getPropertyValue(java.lang.String) error at line: 5
indicating quite conclusively that Im not a groovy developer 🙂
As always - any help is mightily appreciated
Thanks guys,
rich
Hi,
As everyone can tell I'm not a groovy coder - but if altering the script from my previous messages on this topic appears to be a problem - I have an idea to get around it - i dont know if its possible or not?
the following script is one that I typically use to grab header values.........in this instance, it grabs the first 'Set-Cookie' value (out of the 4 'Set-Cookie' headers) and stores it in the Properties step.
//Takes one of the elements of the response Header def value = testRunner.testCase.testSteps["GET Cookies"].testRequest.response.responseHeaders["Set-Cookie"] //Read this value into a parameter - writes the header value into the Properties test step def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ) groovyUtils.setPropertyValue("Properties", "Set-Cookie",value[0])
Is there any way to alter the above code so rather than using the 'it' method and some other methods I dont really know how to use - would it be easier to have 4 instances of the 'takes the element from header' and 4 instances of the 'read this value into parameter', but altering the value capture by getting it to search the 'Set-Cookie' value for a specific string and writing out that value to a separate property?
What I mean by this (and this isn't even pseudo code) is something like the following:
//Takes the returnurl 'Set-Cookie' value of the response Header def returnurlvalue = testRunner.testCase.testSteps["Request"].testRequest.response.responseHeaders["Set-Cookie"] //and some code here equating to a filter where value begins 'returnurl='
//Read this value into a parameter - writes the returnurl header value into the Properties test step
def groovyUtils1 = new com.eviware.soapui.support.GroovyUtils( context )
groovyUtils1.setPropertyValue("Properties", "Set-Cookie_returnurl",returnurlvalue[0])
//Takes the authtype'Set-Cookie' value of the response Header
def authtypevalue = testRunner.testCase.testSteps["Request"].testRequest.response.responseHeaders["Set-Cookie"] //and code equating to a filter where value begins 'authtype='
//Read this value into a parameter - writes the authtype header value into the Properties test step
def groovyUtils2 = new com.eviware.soapui.support.GroovyUtils( context )
groovyUtils2.setPropertyValue("Properties", "Set-Cookie_authtype", authtypevalue[0])
//Takes the state 'Set-Cookie' value of the response Header
def statevalue = testRunner.testCase.testSteps["Request"].testRequest.response.responseHeaders["Set-Cookie"] //and code equating to a filter where value begins 'state='
//Read this value into a parameter - writes the state header value into the Properties test step
def groovyUtils3 = new com.eviware.soapui.support.GroovyUtils( context )
groovyUtils3.setPropertyValue("Properties", "Set-Cookie_state", statevalue[0])
//Takes the nonce 'Set-Cookie' value of the response Header
def noncevalue = testRunner.testCase.testSteps["Request"].testRequest.response.responseHeaders["Set-Cookie"] //and code equating to a filter where value begins 'nonce='
//Read this value into a parameter - writes the nonce header value into the Properties test step
def groovyUtils4 = new com.eviware.soapui.support.GroovyUtils( context )
groovyUtils4.setPropertyValue("Properties", "Set-Cookie_nonce",noncevalue[0])
I know it's not pretty or efficient but would it be possible and perhaps easier?
Would anyone know how to add a filter so that when the header attribute value is grabbed - rather than just grabbing the specific header value, the search is filtered (like I've tried to represent above) so that 'Set-Cookie' is captured where value begins 'whatever='?
I know I'm just pulling this out of nowhere here, trying to think of anything that will handle this - out of 500 people that work here - no one knows groovy so they cant help me!
Thanks again,
richie