首页 > 解决方案 > 需要来自邮递员 curl url 的等效 gatling scala 代码

问题描述

需要来自邮递员 curl url 的等效 gatling scala 代码

您能否在逻辑上为以下身份验证请求编写一个等效的 gatling scala 代码,首先我需要使用 gatling scala 获取身份验证请求及其响应。下面是 PostMan 中的身份验证 curl 请求,它工作正常,我的意思是给201 响应,但我在 Gatling 中使用 git url github得到 400 响应

从 curl 请求生成:

curl 'https://api.platform.com/auth/oauth/token' -H 'Accept: application/json, text/plain, */*' -H 'Referer: https://api-origin.cloud/dev/reservations-web/signin' -H 'Origin: https://api.origin.cloud' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36' -H 'Authorization: Basic aWtyd3VnaDM4NzFnaHc4cmduN3E4M2c2c3I=' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' --data 'grant_type=password&username=abc.xyc1%40abc.com&password=Password1%21' --compressed

下面是我尝试生成但得到 400 响应的 scala 等效代码

package simulations

import baseConfig.BaseSimulation
import io.gatling.core.Predef._
import io.gatling.core.session
import io.gatling.http.Predef._

import scala.concurrent.duration.DurationInt
import scala.util.Random

class ogrebattledriverPostAccessToken extends BaseSimulation {



   exec(
      http("HTTP Request auth")
        .post("https://api.platform.com/auth/oauth/token")
        .header("Content-Type","application/x-www-form-urlencoded")
        .formParam("grant_type","password")
        .formParam("username", "TableTopEng%40xxxs.com")
        .formParam("password", "actual password")
        .check(status is 200)
        .check(jsonPath("$.access_token").saveAs("access_token"))
        .check(jsonPath("$.token_type").saveAs("token_type"))
    )


  //val PostData=jsonFile("input-json.json").circular
  def name() = Random.nextInt(Integer.MAX_VALUE).toString



   //val headers_10 = Map("Content-Type" -> """application/json""","Authorization","${token_type} + ${access_token}" )


  def getSpecificOgreID()={
    repeat(140000){
      exec(flushHttpCache)

        feed(usersDataSource)
        .exec(http("PostRequestPerformanceTestingOgreBattleDriver")
          .post("https://api.tabletop-stage.tiamat-origin.cloud/dev/ogre-battledriver/Organizations")
         // .headers(headers_10)
          .header("Authorization", "${token_type} + ${access_token}")
          .body(StringBody(session =>
            s"""
               |{
               |  "name": "${name()}",
               |  "latitude": 66.256538,
               |  "longitude": -95.934502,
               |  "phoneNumber": "555-555-5555",
               |  "emailAddress": "perftest1@perftest1.com",
               |  "website": "https://perftest1",
               |  "streetLine1": "123 perftest1.",
               |  "streetLine2": "Ste 400",
               |  "city": "Omaha",
               |  "state": "NE",
               |  "zipCode": "98002"
               |}
          """.stripMargin)).asJSON

          .check(status.in(200,201))//checkforaspecificstatus
          .check(jsonPath(path="$.name").saveAs(key="name")))//checkforaspecificstatus
        .exec{session=>println(session);session}//parameterfortheorgIdgoeshere
        .pause(1)

    }
  }

  // add a scenario
  val scn = scenario("Battle Driver Get Orgs ")

    .forever() { // add in the forever() method - users now loop forever
      exec(getSpecificOgreID())
    }

我在 Intellij 编辑器中遇到的错误

20:22:56.563 [ERROR] i.g.h.a.s.HttpRequestAction - 'httpRequest-5' failed to execute: No attribute named 'access_token' is defined

有人可以从上面的邮递员 curl 请求中编写一个合乎逻辑的加特林 scala 代码吗?

标签: scalacurlperformance-testinggatlingscala-gatling

解决方案


推荐阅读