简体   繁体   English

在空手道中使用 cURL 实现 API 自动化

[英]Using cURL for API automation in Karate

I'm new to Karate我是空手道新手

I'm automating an API test where I need to upload a large file >50MB When I do so with Karate I get an error "Broken Pipe" and according to this question Broken pipe (Write failed) when testing > max allowed Content-Length I could use "cURL" for this request.我正在自动化 API 测试,我需要上传大于 50MB 的大文件当我使用空手道这样做时,我收到错误“Broken Pipe”,根据这个问题Broken pipe (Write failed) when testing > max allowed Content-Length我可以对这个请求使用“cURL”。

It's working fine as follows (with hardcoded data):它工作正常如下(使用硬编码数据):

* def result = karate.exec('curl -L -X POST "URL" -H "Authorization: Bearer MYTOKEN" -F "file=@"PATH""')

However, I'm having issues with the syntax when passing variables I need to pass the URL, token, and path as variables and not hardcoded text since I will be reusing this test for multiple landscapes.但是,我在传递变量时遇到语法问题,我需要将 URL、令牌和路径作为变量而不是硬编码文本传递,因为我将在多个环境中重复使用此测试。

How would I go about it?我怎么会 go 呢? Thanks,谢谢,

Think of the Karate syntax as very close to JavaScript.认为空手道语法非常接近 JavaScript。 So, string concatenation works.因此,字符串连接有效。 For example:例如:

* def myUrl = 'https://httpbin.org/anything'
* def result = karate.exec('curl ' + myUrl)

And a nice thing is that JavaScript Template Literals work:一件好事是 JavaScript 模板文字工作:

* def myUrl = 'https://httpbin.org/anything'
* def result = karate.exec(`curl ${myUrl}`)

Also note that the karate.exec() API takes an array of command-line arguments.另请注意, karate.exec() API 采用命令行 arguments 数组。 This can make some things easier like not having to put quotes around arguments with white-space included etc.这可以使一些事情变得更容易,例如不必在 arguments 周围加上引号,包括空格等。

* def myUrl = 'https://httpbin.org/anything'
* def result = karate.exec({ args: [ 'curl', myUrl ] })  

You can build the arguments array as a second step for convenience:为方便起见,您可以构建 arguments 阵列作为第二步:

* def myUrl = 'https://httpbin.org/anything'
* def args = ['curl']
* args.push(myUrl)
* def result = karate.exec({ args: args }) 

Note that conditional logic and even an if statement is possible in Karate: https://stackoverflow.com/a/50350442/143475请注意,在空手道中可以使用条件逻辑甚至if语句: https://stackoverflow.com/a/50350442/143475

Also see: https://stackoverflow.com/a/62911366/143475另请参阅: https://stackoverflow.com/a/62911366/143475

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM