[GATLING-2.0.0M3A] Reading and setting Cookies

Hello

I have a web site that set a cookie with a path “/access”. But I would like this cookie to be used on path “/”.

I tried this code, to read the cookie’s value but it doesn’t work. I have an exception.

.exec(http(“request_Login”)
.post(“/access”)
.queryParam(“action”, “login”)
.body(StringBody(“”“{“login”:“adm”,“password”:“adm”}”“”))
.headers(headers_json)
)
.pause(1)
.exec( session => {
import io.gatling.http.cookie._
import java.net.URI

val ck = session(“gatling.http.cookies”).as[CookieJar].get(URI.create(“//bdist”)).find(
_.getName == “session-id”
)
session.set(“test”, ck.getOrElse(null).getValue)
})

HTTP response:
status=
202 Accepted
headers=
content-type: [application/json]
transfer-encoding: [chunked]
date: [Tue, 29 Oct 2013 15:37:27 GMT]
set-cookie: [session-id=a72e12d3-847c-481d-942d-6928c3667c18; HttpOnly]

<<<<<<<<<<<<<<<<<<<<<<<<<
[ERROR] [10/29/2013 15:43:18.885] [GatlingSystem-akka.actor.default-dispatcher-13] [akka://GatlingSystem/user/$i] null
java.lang.NullPointerException
at se.box.browse$$anonfun$1.apply(browse.scala:113)
at se.box.browse$$anonfun$1.apply(browse.scala:106)
at io.gatling.core.action.SessionHook.executeOrFail(SessionHook.scala:35)
at io.gatling.core.action.Failable$class.execute(Actions.scala:82)
at io.gatling.core.action.SessionHook.execute(SessionHook.scala:28)
at io.gatling.core.action.Action$$anonfun$receive$1.applyOrElse(Actions.scala:42)
at scala.PartialFunction$OrElse.applyOrElse(PartialFunction.scala:166)
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:425)
at akka.actor.ActorCell.invoke(ActorCell.scala:386)
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:230)
at akka.dispatch.Mailbox.run(Mailbox.scala:212)
at akka.dispatch.ForkJoinExecutorConfigurator$MailboxExecutionTask.exec(AbstractDispatcher.scala:506)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

15:43:18.926 [ERROR] i.g.c.a.b.SessionHookBuilder$$anonfun$build$1$$anon$1 - Action $anonfun$build$1$$anon$1 crashed, forwarding user to next one
java.lang.NullPointerException: null
at se.box.browse$$anonfun$1.apply(browse.scala:113) ~[na:na]
at se.box.browse$$anonfun$1.apply(browse.scala:106) ~[na:na]
at io.gatling.core.action.SessionHook.executeOrFail(SessionHook.scala:35) ~[gatling-core-2.0.0-M3a.jar:na]
at io.gatling.core.action.Failable$class.execute(Actions.scala:82) ~[gatling-core-2.0.0-M3a.jar:na]
at io.gatling.core.action.SessionHook.execute(SessionHook.scala:28) ~[gatling-core-2.0.0-M3a.jar:na]
at io.gatling.core.action.Action$$anonfun$receive$1.applyOrElse(Actions.scala:42) ~[gatling-core-2.0.0-M3a.jar:na]
at scala.PartialFunction$OrElse.applyOrElse(PartialFunction.scala:166) ~[scala-library-2.10.2.jar:na]
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:425) ~[akka-actor_2.10-2.1.4.jar:na]
at akka.actor.ActorCell.invoke(ActorCell.scala:386) ~[akka-actor_2.10-2.1.4.jar:na]
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:230) ~[akka-actor_2.10-2.1.4.jar:na]
at akka.dispatch.Mailbox.run(Mailbox.scala:212) ~[akka-actor_2.10-2.1.4.jar:na]
at akka.dispatch.ForkJoinExecutorConfigurator$MailboxExecutionTask.exec(AbstractDispatcher.scala:506) [akka-actor_2.10-2.1.4.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.10.2.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.10.2.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.10.2.jar:na]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.10.2.jar:na]
15:43:18.929 [INFO ] i.g.h.a.HttpRequestAction - Sending request ‘request_7’: scenario ‘Scenario Name’, userId #0
15:43:19.156 [TRACE] i.g.h.a.AsyncHandlerActor -

Request:
request_7: OK

Well

Unless there is a mean to change the thread title we may better continue on this one … (sorry for this)

So in the mean time, I found that my domain was wrong: using

val ck = session(“gatling.http.cookies”).as[CookieJar].get(URI.create(“http://bdist/access”)).find(
_.getName == “session-id”
)
session.set(“test”, ck.getOrElse(null).getValue)
worked !

Now I found out about the https://github.com/excilys/gatling/wiki/Gatling-2#wiki-http-misc
But honestly I have no idea how to use this:
exec(addCookies(url: Expression[String], cookie: Cookie, cookies: Cookie*))

To sum up the discussion about Cookie in 2.0.0
First do not use 2.0.0M3a.
Use M4 if available: https://github.com/excilys/gatling/wiki/Downloads
Or this Snapshot: https://oss.sonatype.org/content/repositories/snapshots/io/gatling/highcharts/gatling-charts-highcharts/2.0.0-SNAPSHOT/

You may have to:

  • Add the libraries: logback- classic and core from previous Gatling lib.
  • Remove the examples (compilation errors)

The DSL has changed a bit (simply start with the recorder to get a valid example):

Then the code example:

.exec(http(“request_Login”)
.post("/access")
.queryParam(“action”, “login”)
.body(StringBody("""{“login”:“adm”,“password”:“adm”}"""))
.headers(headers_json)
)
.pause(1)
.exec( session => {
import io.gatling.http.cookie._
import java.net.URI

val ck = session(“gatling.http.cookies”).as[CookieJar].get(URI.create(“http://bdist/access”)).find(
_.getName == “session-id”
)
session.set(“test”, ck.getOrElse(null).getValue)
})
.exec(
addCookie(“session-id”, “${test}”) // Default to path=/
)

Bruno

The missing Logback dependencies in the bundle was a bug, now fixed :slight_smile:
Grab the latest bundle from Sonatype snapshots repository and you’ll get a fully working bundle !

Envoyé de mon iPhone