hurl: 用简单纯文本编写HTTP/API测试


Hurl 是一个命令行工具,它运行以简单的纯文本格式定义的HTTP 请求
它可以链接请求、捕获值并评估对标头和正文响应的查询。
Hurl 非常通用:它可用于获取数据测试 HTTP会话和测试XML/JSON API

实现多个请求很容易:

GET https://example.org/api/health
GET https:
//example.org/api/step1
GET https:
//example.org/api/step2
GET https:
//example.org/api/step3

访问登录:

# Get home:
GET https://example.org

HTTP/1.1 200
[Captures]
csrf_token: xpath
"string(//meta[@name='_csrf_token']/@content)"

# Do login!
POST https:
//example.org/login?user=toto&password=1234
X-CSRF-TOKEN: {{csrf_token}}

HTTP/1.1 302


Hurl 可以运行 HTTP 请求,但也可以用于测试 HTTP 响应。支持不同类型的查询和谓词,从主体响应的XPathJSONPath,到状态代码和响应标头的断言。

它非常适合REST / JSON API

POST https://example.org/api/tests
{
   
"id": "4568",
   
"evaluate": true
}

HTTP/1.1 200
[Asserts]
header
"X-Frame-Options" == "SAMEORIGIN"
jsonpath
"$.status" == "RUNNING"    # Check the status code
jsonpath
"$.tests" count == 25      # Check the number of items
jsonpath
"$.id" matches /\d{4}/     # Check the format of the id

测试HTML 内容

GET https://example.org

HTTP/1.1 200
[Asserts]
xpath
"normalize-space(//head/title)" == "Hello world!"