如何在加特林比较来自http调用的响应? [英] How to compare responses from http calls in gatling?
问题描述
我在2个不同的函数def中有2个http调用,并在加特林会话中保存了响应正文中的json键.我该如何搭配?
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"))
}
我如何比较或验证app_info和user_info的json值是否匹配,即;
How do I compare or verify that the json values of app_info and user_info matches ie;
app_Response
和userdata_Response
这两个值都是arrays.例如,采用以下格式:
The values of both of these are arrays . For instance, in this format:
"app_info":
[
"name",
"address"
]
与user_info相同.我尝试使用jsonPath().equals()的内置方法,但我认为这不是比较合适的方法.如果不是使用特定方法的方法,那么也许会发现如何使用Scala执行?
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?
请帮助.
推荐答案
基本上,您使用json-spray都应该能够使用==运算符(如此处其他答案所述)进行比较.
Basically of you use json-spray you should be able to compare both using == operator like described in this other answer here.
做这样的事情,我可以比较使用喷雾剂的2个Json:
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
=======================================================
这篇关于如何在加特林比较来自http调用的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!