Gatling Template Based Requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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