How to compare responses from http calls in gatling?

女生的网名这么多〃 提交于 2020-02-29 07:20:11

问题


I have 2 http calls in 2 different function def and saving json keys from response body in gatling session. How can I match them?

def getAppData():HttpRequestBuilder = {
      http("get application resource")
        .get("host/app")
        .header("Authorization", "Bearer "+ token)
        .check(status.is(200))
        .check(jsonPath("$..${app_info}").saveAs("app_Response"))

}
def getUserData():HttpRequestBuilder = {
      http("get user data ")
        .get("host/user/data")
        .header("Authorization", "Bearer "+ token)
        .check(status.is(200))
        .check(jsonPath("$..${user_info}").saveAs("userdata_Response"))

}

How do I compare or verify that the json values of app_info and user_info matches ie;

app_Response and userdata_Response

The values of both of these are arrays . For instance, in this format:

"app_info":
    [
         "name",
         "address"
    ]

same for user_info. I gave a try to use in-built methods of jsonPath().equals() but I believe that's not appropriate way for comparing. If not a way using gatling specific methods then perhaps will find how to perform using scala?

Kindly help.


回答1:


Basically of you use json-spray you should be able to compare both using == operator like described in this other answer here.

Compare json equality in Scala

[Edit] Doing something like this I could compare 2 Json's using spray:

package example

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import spray.json._
import DefaultJsonProtocol._

class MainSimulation extends Simulation {
  val baseUrl = "http://localhost:8080"
  val httpProtocol = http
    .baseUrl(baseUrl)
    .userAgentHeader("Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)")
  val header = Map("Content-Type" -> "application/json”,”Accept-Charset" -> "utf-8")

  val scn = scenario("Scenario")
    .exec(http("Get Hello Json")
      .get("/hello/Alessandro")
      .check(status.is(200))
      .check(jsonPath("$").saveAs("activities-1")))
    .exec(http("Get Hello Json")
      .get("/hello/Ronaldo")
      .check(status.is(200))
      .check(jsonPath("$").saveAs("activities-2")))
    .exec(session => {
      println("=======================================================")
      val activities_1 = session("activities-1").as[String]
      val activities_2 = session("activities-2").as[String]
      println(s"Activities 1: ${activities_1.parseJson}")
      println(s"Activities 2: ${activities_2.parseJson}")
      println(s"Are they equal?: ${activities_1.parseJson == activities_2.parseJson}")
      println("=======================================================")
      session
    })

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

and I can see this in the output:

=======================================================
Activities 1: {"activities":["swimming","soccer"],"name":"Alessandro"}
Activities 2: {"activities":["swimming","soccer"],"name":"Alessandro"}
Are they equal?: true
=======================================================


来源:https://stackoverflow.com/questions/56911085/how-to-compare-responses-from-http-calls-in-gatling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!