肥皂用户界面。使用 groovy 添加多部分 body 部分

SoapUI. Add multipart body part using groovy

我需要使用 groovy.

将 json 作为多部分 body 部分添加到 "multipart/form-data" 请求

我可以使用文件附件来做到这一点:

testRunner.testCase
          .testSteps["/job/result"]
          .getHttpRequest()
          .attachBinaryData(json.toString().getBytes(), "application/json").contentID = "info"

我的问题是 "attachBinaryData" 为每个请求创建一个临时文件。它不利于负载测试:)

是否有其他可能添加 body 部分,没有文件附件?

类似于:

testRunner.testCase
              .testSteps["/job/result"]
              .getHttpRequest()
              .addBodyPart("application/json", json.toString())

P.S。它必须是 "add",因为请求也有一个静态附件。

如果您想使用 groovy script testStep 将 json 作为 multipart/form-data 添加到您的请求中,您可以使用以下代码:

def jsonStr = "{'id':'test','someValue':'3'}"

def testStep = context.testCase.testSteps["/job/result"]
// set the content for the request
testStep.getHttpRequest().setRequestContent(jsonStr)
// and set the media type 
testStep.testRequest.setMediaType('application/json')
// if you want to send as a multipart/form-data then use follow line instead
// testStep.testRequest.setMediaType('multipart/form-data')

这导致请求配置如下:

希望对您有所帮助,

已找到使用 https://github.com/jgritman/httpbuilder

的解决方案
def http = new HTTPBuilder(serviceEndPoint)
def scanResultFile = new File(testRunner.testCase.getPropertyValue("ScanResultFile"))

http.request( POST ){ req ->

    headers.'Connection' = 'Keep-Alive'
    headers.'User-Agent' = 'SoapUI 4.5.1'
    requestContentType = 'multipart/form-data'

    ByteArrayBody bin = new ByteArrayBody(scanResultFile.readBytes(), "application/octet-stream", "jobResult");
    StringBody info = new StringBody(testRunner.testCase.getPropertyValue("JsonScanResult"), "application/json", java.nio.charset.StandardCharsets.UTF_8);

    MultipartEntity entity = new MultipartEntity()
    entity.addPart("info", info);
    entity.addPart("jobResult", bin)
    req.entity = entity
}