Rester

Build Status codecov Twitter: @_sa_s

Rester 是一个命令行工具,用于测试 HTTP API。它将声明式编写的请求描述作为测试脚本处理,执行并验证指定的请求。

例如,以下描述

# basic.yml
requests:
  basic:
    url: https://httpbin.org/anything
    validation:
      status: 200

的处理方式如下

asciicast

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

结果

asciicast

更多例子

examples 目录展示了 rester 的更多用法。 您还可以找到这些示例在 test snapshot 目录中生成的输出。

以下是一些值得注意的常见任务示例

运行 rester

运行 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 参数的说明

请注意,如果要测试在 localhost 上运行的 API,还需要添加

到您的 docker run 命令。 这使容器可以在主机网络上运行,并允许它将主机视为 localhost

安装 rester

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