responses-validator 专用于对 reqeuests 的响应对象进行断言,
同时为了适用于 yaml 的场景,支持了多种灵活、可扩展的写法
本文详细叙述了响应头和正文所支持的 4 种断言写法:
-
断言部分字段
-
断言包含匹配
-
断言相等匹配
-
断言正则匹配
-
断言函数匹配
详细示例见下文
1. 响应头断言的基本用法
首先,对响应头和响应正文的断言均是可选的。
其次,响应头中字段较多,可只对部分字段进行断言
假设某响应头原文如下
HTTP/1.1 200 OK
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html
Date: Mon, 12 Apr 2022 13:10:34 GMT
Last-Modified: Mon, 23 Jan 2017 13:24:17 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
Transfer-Encoding: chunked
在编写断言时,可仅对其中 2 个字段进行断言
Connection: keep-alive
Content-Type: text/html
具体示例:
# tests/test_headers_fields.yaml
test_name: 断言响应头部分字段
steps:
- request:
method: get
url: https://www.baidu.com
- response: # 对部分稳定字段进行断言,测试通过
status_code: 200
headers:
Connection: keep-alive
Content-Type: tex