[译]Bruno - 自由强大安全开源的 API 测试神器(文本管理/git友好/离线优先)- Bru 语言

361 阅读3分钟

天下苦 postman 久矣!


原文链接:
Bru Markup Language – Bruno Docs
Samples – Bruno Docs
Language Design – Bruno Docs
Bru Tag Reference – Bruno Docs
Syntax Highlighting Support – Bruno Docs


Bru 标记语言

Bru 是一种简单的标记语言,它利用纯文本文件来记录和组织 API 请求的信息。

集合中的 API 请求使用此语言存储为纯文本文件。

这允许您将 API 集合保存在代码存储库中的文件夹中,并使用您首选的版本控制系统来管理它们并与您的团队共享。可以通过拉取请求对 API 集合进行协作,因为人类可读的文件格式使开发人员可以轻松理解对 API 集合所做的更改。

下面是带有一些查询参数的 GET 请求的 Bru 文件示例

bru lang sample

您可以查看包含 GitHub REST API 集合的示例存储库 [这里(在新标签页打开)(github.com/usebruno/gi…

如果您想知道为什么我们设计了一个 DSL 而不仅仅是使用 JSON/YAML,你可以查看这个 github 讨论

示例

以下是一些示例 Bru 文档。

GET

get {
  url: https://api.github.com/users/usebruno
}

带头部的 GET

get {
  url: https://api.textlocal.in/send?apiKey=secret&numbers=9988776655&message=hello
}
 
headers {
  content-type: application/json
  Authorization: Bearer topsecret
}

带请求体的 POST

post {
  url: https://api.textlocal.in/send
}
 
body {
  {
    "apiKey": "secret",
    "numbers": "9988776655",
    "message": "Woof! lets play with some apis"
  }
}
 
headers {
  content-type: application/json
  Authorization: Bearer topsecret
}

脚本

post {
  url: https://api.textlocal.in/login
}
 
body {
  {
    "username": "johnnash",
    "password": "governingdynamics"
  }
}
 
script:post-response {
  bru.setVar("token", res.body.token);
}

测试

post {
  url: https://api.textlocal.in/login
}
 
body {
  {
    "username": "johnnash",
    "password": "governingdynamics"
  }
}
 
tests {
  test("should be able to login", function() {
    expect(res.status).to.equal(201);
  });
 
  test("should receive the token", function() {
    expect(res.body.token).to.be.a('string');
  });
}

语言设计

Bru 文档由块组成。有三种区块

  • 字典块
  • 文本块
  • 数据块

字典块

字典块包含一组键值对。

get {
  url: https://api.textlocal.in/send
}
 
headers {
  content-type: application/json
  Authorization: Bearer 123
  ~transaction-id: {{transactionId}}
}

字典块中的任何键都可以以 ~ 为前缀,以指示它已被禁用。

文本块

文本块是一组行

body {
  {
    "hello": "world"
  }
}
 
tests {
  expect(res.status).to.equal(200);
}

数组块

数组块是字符串列表

vars:secret [
  access_key,
  access_secret,
  ~transactionId
]

数组块中的任何键都可以以 ~ 为前缀,以指示它已被禁用。

Bru 标签参照

元数据

存储有关请求的元数据

meta {
  name: Get users,
  type: http
  seq: 1
}

seq 用于存储串行号。这将决定请求在 UI 中的排序位置。type 可以是 httpgraphql

get

发起 GET http 调用

get {
  url: https://api.github.com/users/usebruno
}

post

发起 POST http 调用

post {
  url: https://api.github.com/users/usebruno
}

put

发起 PUT http 调用

put {
  url: https://api.github.com/users/usebruno
}

delete

发起 DELETE http 调用

delete {
  url: https://api.github.com/users/usebruno
}

options

发起 Get 的 OPTIONS 调用

options {
  url: https://api.github.com/users/usebruno
}

trace

发起 TRACE http 调用

trace {
  url: https://api.github.com/users/usebruno
}

connect

发起 CONNECT http 调用

connect {
  url: https://api.github.com/users/usebruno
}

head

发起 HEAD http 调用

head {
  url: https://api.github.com/users/usebruno
}

查询

请求查询参数

get {
  url: https://api.textlocal.in/send?apiKey=secret&numbers=9988776655&message=hello
}
 
query {
  apiKey: secret
  numbers: 9988776655
  message: hello
}

头部

请求查询头部

get {
  url: https://api.textlocal.in/send?apiKey=secret&numbers=9988776655&message=hello
}
 
headers {
  content-type: application/json
  Authorization: Bearer topsecret
}

请求正文

请求正文 (默认为 json)

body {
  {
    username: 'john',
    password: 'governingdynamics'
  }
}

body:text

请求正文为文本

body:text {
  This is a text body
}

body:xml

请求正文为 xml

body:xml {
  <xml>
    <name>John</name>
    <age>30</age>
  </xml>
}

body:form-urlencoded

请求正文为 form-urlencoded

body:form-urlencoded {
  apikey: secret
  numbers: +91998877665
  ~message: hello
}

body:multipart-form

请求正文为 multipart-form

body:multipart-form {
  apikey: secret
  numbers: +91998877665
  ~message: hello
}

body:graphql

请求正文为 graphql

body:graphql {
  {
    launchesPast {
      launch_site {
        site_name
      }
      launch_success
    }
  }
}

body:graphql:vars

请求正文为 graphql vars

body:graphql:vars {
  {
    "limit": 5
  }
}

script:pre-request

请求正文为 pre-request

script:pre-request {
  req.setHeader("Authorization", "{{token}}");
}

script:post-response

请求正文为 post-response

script:post-response {
  bru.setVar("token", res.body.token);
}

test

测试

body:test {
  expect(res.status).to.equal(200);
}

语法高亮支持

Bruno 为 VS Code 发布了编辑器扩展。您可以从 Visual Studio Marketplace(在新标签页打开) 下载。

IntelliJ 和 JetBrains 系列 IDE 的扩展正在开发中。