Skip to main content

Write response validation scripts for requests using flexible YAML syntax, including: status_code, headers, text, and json()

Project description

responses-validator

A more convenient response assertion tool suitable for requests(experimental ).

Write response validation scripts for requests using flexible YAML syntax, including: status_code, headers, text, and json()

Install

pip install responses-validator

Example

import logging

import requests
from responses_validator import ResponseValidator
from yaml import safe_load

logging.basicConfig(level=logging.DEBUG)
resp = requests.get("http://www.baidu.com/404.html")

yaml_text = """
status_code: 200 | 204   # 状态码 等于200 或 204
headers:
  server: 'nginx/*' # 服务器信息包含nginx
  Content-Type: 'application/json' # 响应类型为JSON
json: # 默认使用glob模式可以只提供部分数据
  name: foo
text: qqqqq
"""

validate = safe_load(yaml_text)  # 加载yaml格式的断言

obj = ResponseValidator(**validate)  # 实例化验证器

ret = obj.is_valid(resp)  # 对响应执行断言

Result

  File "src\\responses_validator\\__init__.py", line 150, in responses_validator.ResponseValidator.is_valid
responses_validator.ResponseAssertionError: 
{
    'status_code': "status_code is 404 and does not match the pattern '200 | 204'.", 
    
    'headers': "headers.server is 'Apache' and does not match the pattern '{'name': 'server', 'value': 'nginx/*', 'mode': 'glob'}'.", 
    
    'text': 'text is \'<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\n<p>The requested URL /404.html was not found on this server.</p>\n</body></html>\n\' and does not match the pattern \'{\'name\': \'text\', \'value\': \'qqqqq\', \'mode\': \'glob\'}\'.',
    
    'json': "resp.json() failed! It may not be a JSON type response, please use text to verify again'."
}

Feedback

wx: python_sanmu

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

responses_validator-0.1.2-cp312-cp312-win_amd64.whl (59.3 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

responses_validator-0.1.2-cp312-cp312-musllinux_1_1_x86_64.whl (425.1 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

responses_validator-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (429.1 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

responses_validator-0.1.2-cp312-cp312-macosx_11_0_arm64.whl (66.4 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

responses_validator-0.1.2-cp312-cp312-macosx_10_9_x86_64.whl (69.8 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page