Gatling教程系列一实现POST请求测试(四)


本篇主要介绍在http请求post传参的场景,有了之前GET相关的介绍,相信POST的使用大家也能快速的掌握。POST的请求处理和GET请求最大的区别无非是参数的传递方式,所以在Gatling脚本里主要是去设置http body Content-Type。


本篇主要介绍json和form表单两种参数提交方式的处理:
JSON方式

import io.gatling.core.Predef._
import io.gatling.core.scenario.Simulation
import io.gatling.http.Predef._
class JsonSimulation extends Simulation {
  val httpConf = http.baseURL("http://127.0.0.1:7001/tst")
  //注意这里,设置提交内容type
  val headers_json = Map("Content-Type" -> "application/json")
  val scn = scenario("json scenario")
    .exec(http("test_json")   //http 请求name
      .post("/order/get")     //post url
      .headers(headers_json)  //设置body数据格式
      //将json参数用StringBody包起,并作为参数传递给function body()
      .body(StringBody("{\"orderNo\":201519828113}")).asJSON)
  setUp(scn.inject(atOnceUsers(10))).protocols(httpConf)
}

Form方式

import io.gatling.core.Predef._
import io.gatling.http.Predef._
class FormSimulation extends Simulation {
  val httpConf = http
    .baseURL("http://computer-database.gatling.io")
  //注意这里,设置提交内容type
  val contentType = Map("Content-Type" -> "application/x-www-form-urlencoded")
  //声明scenario
  val scn = scenario("form Scenario")
    .exec(http("form_test") //http 请求name
      .post("/computers") //post地址, 真正发起的地址会拼上上面的baseUrl http://computer-database.gatling.io/computers
      .headers(contentType)
      .formParam("name", "Beautiful Computer") //form 表单的property name = name, value=Beautiful Computer
      .formParam("introduced", "2012-05-30")
      .formParam("discontinued", "")
      .formParam("company", "37"))
  setUp(scn.inject(atOnceUsers(1)).protocols(httpConf))
}

如果需要动态参数,可参考GET请求动态参数的处理。


到这里Gatling的介绍基本就结束了,Gatling的测试脚本本身就不复杂,主要面向http请求的测试。我们从官方下载的包里有非常多的例子适合各种场景,我们在实际使用中一般会对其进行拷贝,然后修改成自己需要的,所以测试过程中大家也不要有什么负担。

clipboard.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值