create comma-separated queryParam from List[String] in session

491 views
Skip to first unread message

Stig Brautaset

unread,
May 3, 2013, 11:32:10 AM5/3/13
to gat...@googlegroups.com
From call A I extract a list of IDs (using jsonPath("//id").findAll.saveAs("IDs")). This works well, and I can do a foreach over these values. However, I now also want to do something like:

.queryParam("foo", session.getTypedAttribute[List[String]]("IDs").mkString(","))

I want the resulting param to look like:

/foo/bar?IDs=12,4523,345

but I haven't been able to make that work. Is it actually possible? If so, how?

Stig

Stéphane Landelle

unread,
May 3, 2013, 11:39:43 AM5/3/13
to gat...@googlegroups.com
You almost there, but you have to pass a function (here, you just have an instruction where session is not defined):

.queryParam("foo", (session: Session) => session.getTypedAttribute[List[String]]("IDs").mkString(","))

or (type inferred)
.queryParam("foo", session => session.getTypedAttribute[List[String]]("IDs").mkString(","))

or (wild card)
.queryParam("foo", _.getTypedAttribute[List[String]]("IDs").mkString(","))

Cheers,

Stéphane


2013/5/3 Stig Brautaset <sbrau...@gmail.com>

Stig

--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Stig Brautaset

unread,
May 3, 2013, 1:03:17 PM5/3/13
to gat...@googlegroups.com
Thanks! I forgot to mention that I'm using Gatling2, so the method has changed a bit. But this works:

.queryParam("foo", session => session.get[List[String]]("IDs").get.mkString(","))

Stig

Stéphane Landelle

unread,
May 3, 2013, 1:55:44 PM5/3/13
to gat...@googlegroups.com

But this works:

.queryParam("foo", session => session.get[List[String]]("IDs").get.mkString(","))

Or simply .queryParam("foo",_.get[List[String]]("IDs").get.mkString(","))
 

Stéphane Landelle

unread,
May 3, 2013, 2:09:53 PM5/3/13
to gat...@googlegroups.com
My bad, actually, you didn't write it the proper way.

I mean, the syntax is correct and it will do the job, but not efficiently: it will generate an Exception if the attribute is absent from the Session (generally meaning that the request that was supposed to store it failed).

In Gatling 2, in order to handle such cases properly, we introduced Validations: https://github.com/excilys/gatling/wiki/Gatling-2#wiki-validation

The proper way is :

queryParam("foo", _.getV[List[String]]("IDs").map(_.mkString(","))

  • getV returns a Validation that contains either the attribute, or an error message (not an exception) if the attribute was absent, or if it wasn't of the expected type
  • map converts the content if the Validation was a Success
  • the signature of the second param is no longer Session => String like in Gatling 1, but Session => Validation[String] (we alias this as Expression[String])
Get it?

PS: What you wrote compiled because your String was implicitly converted into a Validation[String]



2013/5/3 Stéphane Landelle <slan...@excilys.com>
Reply all
Reply to author
Forward
0 new messages