Skip to content

Instantly share code, notes, and snippets.

@meteorfox
Created February 12, 2015 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meteorfox/29b5cf43ab395089de42 to your computer and use it in GitHub Desktop.
Save meteorfox/29b5cf43ab395089de42 to your computer and use it in GitHub Desktop.
Gatling Template Based Requests
package qeperf
import io.gatling.core.Predef._
import io.gatling.http.Predef._
class IdentitySimulation extends Simulation {
val httpConf = http
.baseURL("http://identity.openstack.com")
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
.acceptLanguageHeader("en-US,en;q=0.5")
.acceptEncodingHeader("gzip, deflate")
.userAgentHeader("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0")
val scn = scenario("Auth")
.exec(
session =>
session
.set("identityURL", "")
.set("userID", 1)
.set("username", "carl")
.set("password", "qwerty"))
.exec(
Tokens.authenticate(
session => Tokens.buildAuthRequest(session)))
setUp(
scn.inject(atOnceUsers(1))
).protocols(httpConf)
}
package qeperf
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.core.session.Expression
object Tokens {
object AuthenticateBody {
case class Params(userID: Int,
username: String,
password: Option[String],
apiKey: Option[String])
private[this] def passwordTemplate(pass: String) = s""""password": "$pass""""
private[this] def apiKeyTemplate(apiKey: String) = s""""apiKey": "$apiKey""""
def passwordOrKey(params: Params): String =
params.password match {
case Some(password) => passwordTemplate(password)
case None => params.apiKey match {
case Some(apiKey) => apiKeyTemplate(apiKey)
case None => ""
}
}
def template(params: Params): String =
s"""{
| "auth": {
| "passwordCredentials": {
| "username": "${params.username}",
| ${passwordOrKey(params)}
| }
|}""".stripMargin
}
val buildAuthRequest: Expression[AuthenticateBody.Params] = (session: Session) =>
AuthenticateBody.Params(
session("userID").as[Int],
session("username").as[String],
password = session("password").asOption[String],
apiKey = session("apiKey").asOption[String]
)
def authenticate(params: Expression[AuthenticateBody.Params]) =
http("POST /tokens")
.post("${identityURL}/tokens")
.body(StringBody(session => AuthenticateBody.template(params(session).get)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment