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.6-cp312-cp312-win_amd64.whl (94.6 kB view details)

Uploaded CPython 3.12 Windows x86-64

responses_validator-0.1.6-cp312-cp312-musllinux_1_1_x86_64.whl (677.0 kB view details)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

responses_validator-0.1.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (687.8 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

responses_validator-0.1.6-cp312-cp312-macosx_11_0_arm64.whl (102.3 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

responses_validator-0.1.6-cp312-cp312-macosx_10_9_x86_64.whl (107.2 kB view details)

Uploaded CPython 3.12 macOS 10.9+ x86-64

File details

Details for the file responses_validator-0.1.6-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for responses_validator-0.1.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 93bd95106a30bd03672f39a6b070a6b52afe144cc8460809114f1aab54b71c6d
MD5 b3dba3984022d879b70f4887488460c8
BLAKE2b-256 9a108130bcd568a6e647e73dfedbdaf889aba87459d9df94cfaa225dac93889c

See more details on using hashes here.

File details

Details for the file responses_validator-0.1.6-cp312-cp312-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for responses_validator-0.1.6-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 76391460495bddeac0163fdf75dc2e989abf78997980f21e0e7106abc1d1bde1
MD5 9ce08d1f610f0fb4853717ee2d425ce3
BLAKE2b-256 9c8756e6a9d1573afa7deaecbdec0b2fff6b6858a91048a815a88ea8934b991f

See more details on using hashes here.

File details

Details for the file responses_validator-0.1.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for responses_validator-0.1.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 92c6cec50dbe876482d3e6b9ad4d50b5d393c761fc6945070845f4a3d2db5f4c
MD5 7a8bdc18a82d066b5bc1087247e87e9b
BLAKE2b-256 6a9cddd127a9a8ed8373bada4ff7eb07178928c850e50751f478588f52f85b52

See more details on using hashes here.

File details

Details for the file responses_validator-0.1.6-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for responses_validator-0.1.6-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 10ad5d5b8ad14cdebaacb597795160828f0a349dd8156b478f2099445df2a7d7
MD5 b1ff410460807dc52b808d2e3c9780ff
BLAKE2b-256 451cf386abe111506fb98e81b3339e8429fc3216802a595182b6f6c95482fcf2

See more details on using hashes here.

File details

Details for the file responses_validator-0.1.6-cp312-cp312-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for responses_validator-0.1.6-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 5ed35dc9a1883c22110d6dbc37862ca8bca296d80f8178e62ea7f0fb3ebf2ed1
MD5 40ea5c688df789654f4c652dab8cb145
BLAKE2b-256 0521a425534b56e54b78a81edfd7aebf2b99c656eff9983295bb8351714b4219

See more details on using hashes here.

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