Rester 是一个命令行工具,用于测试 HTTP API。它将声明式编写的请求描述作为测试脚本处理,执行并验证指定的请求。
例如,以下描述
# basic.yml
requests:
basic:
url: https://httpbin.org/anything
validation:
status: 200
的处理方式如下
Rester 目前支持
有关未来版本计划的功能列表,请参阅 即将推出的功能。
一个例子胜过千言万语 - 这就是您如何使用 rester
来检测 API 调用
# github.yml
variables:
BASE_URL: https://api.github.com/repos/finestructure/Rester
requests:
releases:
url: ${BASE_URL}/releases
validation:
status: 200
json:
# validate the first id in the list (latest release)
# this also captures it as a variable
0:
id: .regex(\d+)
log:
# log the id to the console
- json[0].id
latest_release:
# use the release id to request release details
url: ${BASE_URL}/releases/${releases[0].id}
validation:
status: 200
log:
# log the latest release tag to the console
- json.tag_name
结果
examples 目录展示了 rester 的更多用法。 您还可以找到这些示例在 test snapshot 目录中生成的输出。
以下是一些值得注意的常见任务示例
github.yml
:使用 Github API 获取并打印 rester
的最新发行版本。 演示了响应变量的使用以及如何索引到响应数组和对象中,以及如何记录响应。multipart.yml
:执行图像的 multipart 上传。运行 rester
的最简单方法是通过 docker
docker run --rm -t -v $PWD:/host -w /host finestructure/rester examples/github.yml
最好定义一个别名
alias rester="docker run --rm -t -v $PWD:/host -w /host finestructure/rester"
rester examples/basic.yml
关于 docker 参数的说明
--rm
在运行后清理容器-t
附加一个 tty (这有助于刷新和 unicode 表示)-v $PWD:/host
将当前目录映射到容器内的 /host
-w /host
将 /host
目录设置为容器内的工作目录。 这样您就可以简单地引用相对于工作目录的文件,并且它们将在容器内的相同路径中找到。请注意,如果要测试在 localhost
上运行的 API,还需要添加
--network host
到您的 docker run
命令。 这使容器可以在主机网络上运行,并允许它将主机视为 localhost
。
rester
需要 Swift 5。您可以通过 Swift Package Manager 从源代码构建和安装它
git clone https://github.com/finestructure/rester
cd rester
swift build -c release
在 macOS 上,安装和更新 rester
的最简单方法可能是 Mint
brew install mint
mint install finestructure/rester