Extracting value from redirect

Hi,
I need to extract a value from a redirect URL.
I don’t want to remove the followRedirect for the whole scenario, only for this specifi request.
Is there a solution or should I remove the automatic follow from the whole scenario ?
Emmanuel

Hi Emmanuel,

Nope, you’ll have to disable followRedirect.

Steph

2012/6/20 Emmanuel Hugonnet <emmanuel.hugonnet@gmail.com>

If so, how do I ‘parse’ the redirect URL since regex works on the response body to extract the information ?

You’ll have to write some code (you probably not only have to parse your redirect header, but also decode it, so what you want to do is quite complex and can’t be made a built-in).
You can either write your own verify or a transform function (would be my choice):
https://github.com/excilys/gatling/wiki/Checks#wiki-transforming

Does this help?

Steph

2012/6/20 Emmanuel Hugonnet <emmanuel.hugonnet@gmail.com>

Yep, so I need to tansform my header.
Too bad regex works only on the ody of the response and not on the whole response.
(there is no decoding just pure regex extraction).
Emmanuel

I don’t know if you know some scala, so here’s a little help.
You can write something like this:

val myRegexp = “foo(.*)bar”.r // declare and compute the regexp

val scn = …

check(header(“Redirect-Url”).transform((s: String) => s match {
case foo(capturedValue) => capturedValue // scala regexp have an extractor, so it can be used in pattern matching
case _ => “” // regexp doesn’t match
}.saveAs(“foo”)
)

If you need help, feel free to post a gist.

Cheers,

Stephane

2012/6/20 Emmanuel Hugonnet <emmanuel.hugonnet@gmail.com>

Nope almost no Scala so:

def extractTicket(url: String): String = {
val matcher = Pattern.compile(
“”“https://monserveur/silverpeas/?ticket=(.*)”"").matcher(url)
if (matcher.find())
return new String(matcher.group(matcher.groupCount.min(1)))
else
return new String("")
}

2012/6/20 Emmanuel Hugonnet <emmanuel.hugonnet@gmail.com>

Nope almost no Scala so:

def extractTicket(url: String): String = {
val matcher = Pattern.compile(
“”“https://monserveur/silverpeas/?ticket=(.*)”“”).matcher(url)
if (matcher.find())
return new String(matcher.group(matcher.groupCount.min(1)))
else
return new String(“”)
}

.check(header(LOCATION).transform((s: String) => extractTicket(s)).saveAs(“ticket”),status.is(302)

does the trick (I copied it from your own regex scala class).

This way is probably less effecient than my own as in your case, the regexp will be compile every time extractTicket is called. Also, you’re right, you have to create a new String when the regex matches, but don’t do it for the empty String.

I must admit that having scala in the DSL drives the learning curve higher.

Well, understand that it’s thanks to Scala that you have a hook for writing this kind of custom feature that was not implemented as-is in the engine. :wink:
Hopefully now everyone has to write this kind of stuff…

Regarding regex on headers, we’ll try to implement it.

Cheers,

Steph

Upgraded, this was more a POC that Icould do it :wink:
Also I missed a random generator ‘à la’ scala.util.Random to provides data in forms (who said valid filenames :wink: ) for mass upload testing.
I hope to be able to open the scenario and provides it on our project’s github repo.
Cheers,
Emmanuel

Hi Emmanuel,

Just to let you know, I’ve pushed regex checks on headers in master, so it should be in the next release.

Regarding your random generator, you’ll have to explain what exactly you wanted to do. You probably have to implement your own feeder.

Cheers,

Stéphane

2012/6/20 Emmanuel Hugonnet <emmanuel.hugonnet@gmail.com>

Hi,
Yes that would be the perfect wayto do it.
I need to look into your feeder API.
Cheers,
Emmanuel

We’ll add some samples in the cookbook.
Until then, you can search the mailing for examples.

ex: https://groups.google.com/forum/#!searchin/gatling/feeders/gatling/RPcM7vRHrDA/_Fytc6tYG7sJ
Look at the message from Chris Carrier.

Steph

2012/6/22 Emmanuel Hugonnet <emmanuel.hugonnet@gmail.com>

I did this feeder that can be a nice example I think.
I wanted a feeder for creating accounts where an email and the birthdate were needed.

https://gist.github.com/2972382

Nicolas

Added this to the Feeders chapter:
https://github.com/excilys/gatling/wiki/Feeders#wiki-custom

2012/6/22 Nicolas Rémond <nicolas.remond@gmail.com>

Cool,
I will add some Random operations like those on RandomStringUtils in commons-lang3 : http://commons.apache.org/lang/api-3.0/org/apache/commons/lang3/RandomStringUtils.html