加特林不会保存访问令牌 [英] Gatling won't save access token

查看:80
本文介绍了加特林不会保存访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,我可以看到令牌的路径是正确的,因为更改令牌时会遇到诸如find.exists之类的错误.一无所获.但是由于某种原因,我无法保存令牌.我无法建立请求:未定义名为令牌"的属性

In this example below, I can see that path to token is correct, because when I change it I get errors such as find.exists. found nothing. Yet for some reason I can't save the token. I get Failed to build request: No attribute named 'Token' is defined

import scala.concurrent.duration._
import io.gatling.jsonpath.JsonPath

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import io.gatling.jsonpath.AST._

class Uus extends Simulation {

    val httpProtocol = http
        .baseUrl("https://testsite.com")
        .inferHtmlResources()
        .userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36")

    val autentimata = Map(
        "Access-Control-Request-Headers" -> "authorization",
        "Access-Control-Request-Method" -> "GET",
        "Origin" -> "https://testsite.com")

    val autentitud = Map(
        "Accept" -> "application/json, text/plain, */*",
        "Origin" -> "https://testsite.com",
        "authorization" -> "Bearer ${Token}")

    val uri2 = "https://testsite.com"

    val scn = scenario("RecordedSimulation")

        .exec(http("savingtoken")
            .options("/token/get?rememberMe=true")
            .headers(autentimata)
            .resources(http("request_2")
            .get("/token/get?rememberMe=true")
           // .check(jsonPath("$.data.accessToken").saveAs("Token"))
            .check(status.is(200), jsonPath("$.data.accessToken").ofType[String].saveAs("Token"))
            .headers(autentimata)
            .basicAuth("11111111111","P2rooliall"),
            http("sisselogitud")
            .options("/users/11111111111")
            .headers(autentimata),
            http("kasutaja lehele")
            .get("/users/11111111111")
            .headers(autentitud)
            //.check(jsonPath("$.data.accessToken").saveAs("token"))
            .check(status.is(200)),
            http("sündmuste lehele")
            .options("/events?page=0&size=25&relation=ASSIGNEE,CREATOR&status=OPEN,REOPEN,FINISHED,ARCHIVED&sort=createdDate,desc")
            .headers(autentimata),
            http("sündmusteleht")
            .get("/events?page=0&size=25&relation=ASSIGNEE,CREATOR&status=OPEN,REOPEN,FINISHED,ARCHIVED&sort=createdDate,desc")
            .headers(autentitud)
            .header("authorization", "Bearer ${Token}")


    setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
}

推荐答案

我认为问题出在这一行:

I think that the problem is in this line :

授权"->承载者$ {Token}"

"authorization" -> "Bearer ${Token}"

从此块:

 val autentitud = Map(
        "Accept" -> "application/json, text/plain, */*",
        "Origin" -> "https://testsite.com",
        "authorization" -> "Bearer ${Token}")

因为未定义名为令牌"的属性,表明您正在尝试使用尚未定义的变量.而且,实际上,您仅在方案执行期间保存 Token .

since No attribute named 'Token' is defined states that you are trying to use a variable not yet defined. And ,indeed, you save Token only during scenario execution.

加特林文档说明 Expession EL :

此表达式语言仅适用于传递给Gatling DSL方法的字符串值.实例化加特林模拟时,此类字符串仅解析一次.

因此,解决方案是重构代码并在标头中传递代码块,即使这意味着代码重复.

So the solution would be to refactor your code and pass the block inside headers, even if it would mean a code duplication.

您可以尝试通过打印出其值来验证您的令牌是否已提取:

And you could try to verify that your token is extracted by printing its value out like this:

.exec{
        session=>{
                println(" Token value" + session("Token").as[String])
                session

      }}

这篇关于加特林不会保存访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆