分享

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

 daijingwen0 2017-02-17

本篇主要介绍在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.")
  //注意这里,设置提交内容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./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请求的测试。我们从官方下载的包里有非常多的例子适合各种场景,我们在实际使用中一般会对其进行拷贝,然后修改成自己需要的,所以测试过程中大家也不要有什么负担。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多