pytest-httpchain 0.17.1__tar.gz → 0.18.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/PKG-INFO +4 -3
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/README.md +2 -2
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/pyproject.toml +3 -1
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/pyproject.toml.orig +4 -1
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/carrier.py +13 -3
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/models/__init__.py +4 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/models/entities.py +46 -4
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/report_formatter.py +30 -2
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/request_builder.py +59 -2
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/response_steps.py +59 -33
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/templates/functions.py +46 -9
- pytest_httpchain-0.18.1/src/pytest_httpchain/wire_codec.py +30 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/LICENSE +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/__init__.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/body_schema.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/cli.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/constants.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/dataflow.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/errors.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/factory.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/har_writer.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/importers/__init__.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/importers/builder.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/importers/curl.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/importers/har.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/jsonref/__init__.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/jsonref/equality.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/jsonref/exceptions.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/jsonref/jsonc.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/jsonref/loader.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/jsonref/plumbing/__init__.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/jsonref/plumbing/circular.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/jsonref/plumbing/path.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/jsonref/plumbing/reference.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/models/types.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/parallel_stats.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/plugin.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/py.typed +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/redaction.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/schema.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/scoping.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/templates/__init__.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/templates/exceptions.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/templates/expressions.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/templates/substitution.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/userfunc.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/utils.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/validation/__init__.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/validation/deep.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/validation/diagnostics.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/validation/discovery.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/validation/loader.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/validation/semantic.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/validation/validate.py +0 -0
- {pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/warnings.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pytest-httpchain
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.18.1
|
|
4
4
|
Summary: pytest plugin for HTTP testing using JSON files
|
|
5
5
|
Keywords: testing,pytest,httpx,http,api,integration-testing
|
|
6
6
|
Author: Alexander Eresov
|
|
@@ -23,6 +23,7 @@ Requires-Dist: iniconfig>=1.0.1
|
|
|
23
23
|
Requires-Dist: jmespath>=1.0.0
|
|
24
24
|
Requires-Dist: jsonschema>=4.18.0
|
|
25
25
|
Requires-Dist: jsonschema-specifications>=2023.3.6
|
|
26
|
+
Requires-Dist: msgpack>=1.2.0
|
|
26
27
|
Requires-Dist: pydantic>=2.13.4
|
|
27
28
|
Requires-Dist: referencing>=0.28.4
|
|
28
29
|
Requires-Dist: pyrate-limiter>=4.2.0
|
|
@@ -89,11 +90,11 @@ A key-value store persists throughout scenario execution. Variables, fixtures, a
|
|
|
89
90
|
|
|
90
91
|
### Request bodies
|
|
91
92
|
|
|
92
|
-
JSON, form, XML, text, base64, a binary file and GraphQL, and multipart uploads that mix form fields with files: each file read from a path or given inline, several under one name if need be, with its own filename and content type.
|
|
93
|
+
JSON, MessagePack, form, XML, text, base64, a binary file and GraphQL, and multipart uploads that mix form fields with files: each file read from a path or given inline, several under one name if need be, with its own filename and content type.
|
|
93
94
|
|
|
94
95
|
### Response processing
|
|
95
96
|
|
|
96
|
-
- **JMESPath** — Assert on values in JSON responses directly (`"jmespath": {"data.id": 42, "items": {"length": 3}}`), or extract them for later stages
|
|
97
|
+
- **JMESPath** — Assert on values in JSON or MessagePack responses directly (`"jmespath": {"data.id": 42, "items": {"length": 3}}`), or extract them for later stages
|
|
97
98
|
- **Regex** — Save values from bodies that are not JSON, such as a CSRF token from an HTML form (`"regex": {"csrf": "name=\"csrf\" value=\"([^\"]+)\""}`)
|
|
98
99
|
- **JSON Schema** — Validate response structure against a schema, inline or from a file, or one inside a document you already have: `"schema": "./openapi.json#/components/schemas/User"` checks the response against an OpenAPI component, its `$ref`s resolved across the document and into local files, never over the network
|
|
99
100
|
- **User functions** — Call Python functions for custom extraction, verification, or [authentication](https://www.python-httpx.org/advanced/authentication/#custom-authentication-schemes)
|
|
@@ -52,11 +52,11 @@ A key-value store persists throughout scenario execution. Variables, fixtures, a
|
|
|
52
52
|
|
|
53
53
|
### Request bodies
|
|
54
54
|
|
|
55
|
-
JSON, form, XML, text, base64, a binary file and GraphQL, and multipart uploads that mix form fields with files: each file read from a path or given inline, several under one name if need be, with its own filename and content type.
|
|
55
|
+
JSON, MessagePack, form, XML, text, base64, a binary file and GraphQL, and multipart uploads that mix form fields with files: each file read from a path or given inline, several under one name if need be, with its own filename and content type.
|
|
56
56
|
|
|
57
57
|
### Response processing
|
|
58
58
|
|
|
59
|
-
- **JMESPath** — Assert on values in JSON responses directly (`"jmespath": {"data.id": 42, "items": {"length": 3}}`), or extract them for later stages
|
|
59
|
+
- **JMESPath** — Assert on values in JSON or MessagePack responses directly (`"jmespath": {"data.id": 42, "items": {"length": 3}}`), or extract them for later stages
|
|
60
60
|
- **Regex** — Save values from bodies that are not JSON, such as a CSRF token from an HTML form (`"regex": {"csrf": "name=\"csrf\" value=\"([^\"]+)\""}`)
|
|
61
61
|
- **JSON Schema** — Validate response structure against a schema, inline or from a file, or one inside a document you already have: `"schema": "./openapi.json#/components/schemas/User"` checks the response against an OpenAPI component, its `$ref`s resolved across the document and into local files, never over the network
|
|
62
62
|
- **User functions** — Call Python functions for custom extraction, verification, or [authentication](https://www.python-httpx.org/advanced/authentication/#custom-authentication-schemes)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "pytest-httpchain"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.18.1"
|
|
4
4
|
description = "pytest plugin for HTTP testing using JSON files"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.13"
|
|
@@ -13,6 +13,7 @@ dependencies = [
|
|
|
13
13
|
"jmespath>=1.0.0",
|
|
14
14
|
"jsonschema>=4.18.0",
|
|
15
15
|
"jsonschema-specifications>=2023.03.6",
|
|
16
|
+
"msgpack>=1.2.0",
|
|
16
17
|
"pydantic>=2.13.4",
|
|
17
18
|
"referencing>=0.28.4",
|
|
18
19
|
"pyrate-limiter>=4.2.0",
|
|
@@ -178,6 +179,7 @@ layers = [
|
|
|
178
179
|
"userfunc",
|
|
179
180
|
"models",
|
|
180
181
|
"templates : jsonref",
|
|
182
|
+
"wire_codec",
|
|
181
183
|
"errors : constants : warnings",
|
|
182
184
|
]
|
|
183
185
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "pytest-httpchain"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.18.1"
|
|
4
4
|
description = "pytest plugin for HTTP testing using JSON files"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.13"
|
|
@@ -27,6 +27,8 @@ dependencies = [
|
|
|
27
27
|
# The meta-schemas a body schema's registry holds (body_schema): the
|
|
28
28
|
# floor jsonschema 4.18.0 itself requires, as for attrs.
|
|
29
29
|
"jsonschema-specifications>=2023.03.6",
|
|
30
|
+
# 1.2.0 supplies Python 3.14 wheels, which the project supports.
|
|
31
|
+
"msgpack>=1.2.0",
|
|
30
32
|
"pydantic>=2.13.4",
|
|
31
33
|
"referencing>=0.28.4",
|
|
32
34
|
"pyrate-limiter>=4.2.0",
|
|
@@ -223,6 +225,7 @@ layers = [
|
|
|
223
225
|
"userfunc",
|
|
224
226
|
"models",
|
|
225
227
|
"templates : jsonref",
|
|
228
|
+
"wire_codec",
|
|
226
229
|
"errors : constants : warnings",
|
|
227
230
|
]
|
|
228
231
|
|
|
@@ -45,6 +45,7 @@ from pytest_httpchain.models import (
|
|
|
45
45
|
IndividualParameter,
|
|
46
46
|
JMESPathMatcher,
|
|
47
47
|
JsonBody,
|
|
48
|
+
MsgpackBody,
|
|
48
49
|
ParallelConfig,
|
|
49
50
|
ParallelForeachConfig,
|
|
50
51
|
ParallelRepeatConfig,
|
|
@@ -564,7 +565,7 @@ def _none_is_a_value(model: BaseModel, field: str) -> bool:
|
|
|
564
565
|
"""The fields exempt from `_render_declared`: None there is something the
|
|
565
566
|
scenario can mean, not a setting that vanished — a JSON body of ``null``
|
|
566
567
|
(``request_builder`` sends it as such) and free-text descriptions."""
|
|
567
|
-
return field == "description" or (isinstance(model, JsonBody) and field
|
|
568
|
+
return field == "description" or (isinstance(model, JsonBody | MsgpackBody) and field in ("json", "msgpack"))
|
|
568
569
|
|
|
569
570
|
|
|
570
571
|
def _none_is_compared(model: BaseModel, field: str) -> bool:
|
|
@@ -2132,6 +2133,7 @@ class Carrier:
|
|
|
2132
2133
|
sending = time.perf_counter()
|
|
2133
2134
|
try:
|
|
2134
2135
|
response = cls._execute_http_request(request_kwargs)
|
|
2136
|
+
response.extensions["httpchain_response_codec"] = stage.response_codec
|
|
2135
2137
|
except StageExecutionError as e:
|
|
2136
2138
|
e.started = started
|
|
2137
2139
|
raise
|
|
@@ -2153,7 +2155,7 @@ class Carrier:
|
|
|
2153
2155
|
# derived text containing '{{ }}' would be executed as an
|
|
2154
2156
|
# expression.
|
|
2155
2157
|
save_model = step.save if isinstance(step.save, SubstitutionsSave) else _render_save(step.save, step_context)
|
|
2156
|
-
step_saved = process_save(save_model, response, step_context)
|
|
2158
|
+
step_saved = process_save(save_model, response, step_context, codec=stage.response_codec)
|
|
2157
2159
|
# The static HTTPCHAIN027 check cannot see dynamically
|
|
2158
2160
|
# produced keys, so the shadowing is surfaced here too.
|
|
2159
2161
|
if RESPONSE_META_NAME in step_saved:
|
|
@@ -2177,7 +2179,15 @@ class Carrier:
|
|
|
2177
2179
|
# Each check's value is rendered on its own, all before the
|
|
2178
2180
|
# first check, through the guard (`_verify_renderer`): a
|
|
2179
2181
|
# template that fails is one failure among the step's.
|
|
2180
|
-
process_verify(
|
|
2182
|
+
process_verify(
|
|
2183
|
+
step.verify,
|
|
2184
|
+
response,
|
|
2185
|
+
cls.scenario_dir,
|
|
2186
|
+
cls.redaction,
|
|
2187
|
+
render=_verify_renderer(step.verify, step_context),
|
|
2188
|
+
ref_bounds=cls.ref_bounds,
|
|
2189
|
+
codec=stage.response_codec,
|
|
2190
|
+
)
|
|
2181
2191
|
|
|
2182
2192
|
case _:
|
|
2183
2193
|
raise RuntimeError(f"Unhandled response step: {type(step).__name__}")
|
|
@@ -23,6 +23,7 @@ from pytest_httpchain.models.entities import (
|
|
|
23
23
|
BasicAuth,
|
|
24
24
|
BearerAuth,
|
|
25
25
|
BinaryBody,
|
|
26
|
+
BytesBody,
|
|
26
27
|
ClientConfig,
|
|
27
28
|
CombinationsParameter,
|
|
28
29
|
DigestAuth,
|
|
@@ -39,6 +40,7 @@ from pytest_httpchain.models.entities import (
|
|
|
39
40
|
JMESPathMatcher,
|
|
40
41
|
JMESPathSave,
|
|
41
42
|
JsonBody,
|
|
43
|
+
MsgpackBody,
|
|
42
44
|
Multipart,
|
|
43
45
|
MultipartBody,
|
|
44
46
|
ParallelConfig,
|
|
@@ -136,6 +138,8 @@ __all__ = [
|
|
|
136
138
|
"TextBody",
|
|
137
139
|
"Base64Body",
|
|
138
140
|
"BinaryBody",
|
|
141
|
+
"BytesBody",
|
|
142
|
+
"MsgpackBody",
|
|
139
143
|
"FilesBody",
|
|
140
144
|
"FileSpec",
|
|
141
145
|
"MultipartBody",
|
|
@@ -26,12 +26,19 @@ from pydantic import (
|
|
|
26
26
|
PositiveFloat,
|
|
27
27
|
PositiveInt,
|
|
28
28
|
RootModel,
|
|
29
|
+
Strict,
|
|
30
|
+
StrictBool,
|
|
31
|
+
StrictBytes,
|
|
32
|
+
StrictFloat,
|
|
33
|
+
StrictInt,
|
|
34
|
+
StrictStr,
|
|
29
35
|
Tag,
|
|
30
36
|
TypeAdapter,
|
|
31
37
|
ValidationError,
|
|
32
38
|
ValidationInfo,
|
|
33
39
|
ValidatorFunctionWrapHandler,
|
|
34
40
|
WithJsonSchema,
|
|
41
|
+
WrapValidator,
|
|
35
42
|
model_validator,
|
|
36
43
|
)
|
|
37
44
|
from pydantic.json_schema import JsonDict
|
|
@@ -460,6 +467,14 @@ class BinaryBody(StrictModel):
|
|
|
460
467
|
binary: SerializablePath | PartialTemplateStr = Field(description="Path to binary file.")
|
|
461
468
|
|
|
462
469
|
|
|
470
|
+
class BytesBody(StrictModel):
|
|
471
|
+
bytes: StrictBytes | TemplateExpressionOnly = Field(description="Raw bytes from a template expression, such as msgpack_pack(value).")
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
class MsgpackBody(StrictModel):
|
|
475
|
+
msgpack: Any = Field(description="MessagePack value to encode. Templates may provide binary values within it.")
|
|
476
|
+
|
|
477
|
+
|
|
463
478
|
class FileSpec(StrictModel):
|
|
464
479
|
"""A file of a multipart body, written as an object: where its bytes come
|
|
465
480
|
from (exactly one of path, content and base64), and the filename and
|
|
@@ -596,6 +611,8 @@ get_request_body_discriminator = _create_discriminator(
|
|
|
596
611
|
TextBody: "text",
|
|
597
612
|
Base64Body: "base64",
|
|
598
613
|
BinaryBody: "binary",
|
|
614
|
+
BytesBody: "bytes",
|
|
615
|
+
MsgpackBody: "msgpack",
|
|
599
616
|
FilesBody: "files",
|
|
600
617
|
MultipartBody: "multipart",
|
|
601
618
|
GraphQLBody: "graphql",
|
|
@@ -610,6 +627,8 @@ RequestBody = Annotated[
|
|
|
610
627
|
| Annotated[TextBody, Tag("text")]
|
|
611
628
|
| Annotated[Base64Body, Tag("base64")]
|
|
612
629
|
| Annotated[BinaryBody, Tag("binary")]
|
|
630
|
+
| Annotated[BytesBody, Tag("bytes")]
|
|
631
|
+
| Annotated[MsgpackBody, Tag("msgpack")]
|
|
613
632
|
| Annotated[FilesBody, Tag("files")]
|
|
614
633
|
| Annotated[MultipartBody, Tag("multipart")]
|
|
615
634
|
| Annotated[GraphQLBody, Tag("graphql")],
|
|
@@ -834,9 +853,28 @@ class HeaderMatcher(StrictModel):
|
|
|
834
853
|
return self
|
|
835
854
|
|
|
836
855
|
|
|
837
|
-
#
|
|
838
|
-
#
|
|
839
|
-
|
|
856
|
+
# JSON's own validator keeps its exact rules for byte-free operands. If it
|
|
857
|
+
# rejects nested MessagePack bin values, this strict recursive form accepts
|
|
858
|
+
# those without admitting tuples or other values JSON itself would reject.
|
|
859
|
+
type BinaryStructuredValue = (
|
|
860
|
+
StrictStr | StrictBool | StrictInt | StrictFloat | StrictBytes | None | Annotated[list[BinaryStructuredValue], Strict()] | Annotated[dict[str, BinaryStructuredValue], Strict()]
|
|
861
|
+
)
|
|
862
|
+
_BINARY_STRUCTURED = TypeAdapter(BinaryStructuredValue)
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
def _allow_nested_bytes(value: Any, handler: ValidatorFunctionWrapHandler) -> Any:
|
|
866
|
+
try:
|
|
867
|
+
return handler(value)
|
|
868
|
+
except ValidationError as original:
|
|
869
|
+
try:
|
|
870
|
+
return _BINARY_STRUCTURED.validate_python(value)
|
|
871
|
+
except ValidationError:
|
|
872
|
+
raise original from None
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
# A template over `vars` renders an object as a SimpleNamespace, which stands
|
|
876
|
+
# for the object it was written as.
|
|
877
|
+
_JsonOperand = Annotated[JsonValue | bytes, BeforeValidator(convert_namespace_to_dict), WrapValidator(_allow_nested_bytes)]
|
|
840
878
|
|
|
841
879
|
|
|
842
880
|
def _operand_schema(schema: JsonDict) -> None:
|
|
@@ -965,7 +1003,7 @@ def _declared_value(v: Any) -> Any:
|
|
|
965
1003
|
# A value compared by JSON equality: anything but an object, which is a matcher.
|
|
966
1004
|
# The schema says so, so an editor holds a literal object to the matcher's keys.
|
|
967
1005
|
_JsonEqualityValue = Annotated[
|
|
968
|
-
JsonValue,
|
|
1006
|
+
Annotated[JsonValue | bytes, WrapValidator(_allow_nested_bytes)],
|
|
969
1007
|
WithJsonSchema({"type": ["string", "number", "boolean", "null", "array"], "description": "Equal to this JSON value (true is not 1; 1 equals 1.0)."}),
|
|
970
1008
|
]
|
|
971
1009
|
|
|
@@ -1334,6 +1372,10 @@ class Stage(Marked, Fixtured, Descripted):
|
|
|
1334
1372
|
"With parallel, each iteration retries on its own.",
|
|
1335
1373
|
)
|
|
1336
1374
|
request: Request = Field(description="HTTP request details.")
|
|
1375
|
+
response_codec: Literal["auto", "json", "msgpack"] = Field(
|
|
1376
|
+
default="auto",
|
|
1377
|
+
description="Codec for JMESPath and body schema checks: auto uses MessagePack for a MessagePack Content-Type, otherwise JSON.",
|
|
1378
|
+
)
|
|
1337
1379
|
response: Responses = Field(default_factory=list, description="Sequential steps to process the response.")
|
|
1338
1380
|
|
|
1339
1381
|
|
{pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/report_formatter.py
RENAMED
|
@@ -14,6 +14,7 @@ import httpx
|
|
|
14
14
|
|
|
15
15
|
from pytest_httpchain.redaction import DEFAULT_REDACTION, Redaction
|
|
16
16
|
from pytest_httpchain.utils import JSON_PARSE_ERRORS, request_content
|
|
17
|
+
from pytest_httpchain.wire_codec import MSGPACK_DECODE_ERRORS, is_msgpack_content_type, unpack_msgpack
|
|
17
18
|
|
|
18
19
|
_MAX_BODY_CHARS = 1000
|
|
19
20
|
|
|
@@ -23,6 +24,17 @@ _DECODE_CHUNK = 1 << 20
|
|
|
23
24
|
_PRETTY_JSON = json.JSONEncoder(indent=2, ensure_ascii=False)
|
|
24
25
|
|
|
25
26
|
|
|
27
|
+
def _msgpack_display(value: Any) -> Any:
|
|
28
|
+
if isinstance(value, bytes):
|
|
29
|
+
shown = value[:64].hex()
|
|
30
|
+
suffix = f"... ({len(value)} bytes)" if len(value) > 64 else ""
|
|
31
|
+
return f"<bytes: {shown}{suffix}>"
|
|
32
|
+
raise TypeError(f"Cannot display {type(value).__name__} as MessagePack data")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
_PRETTY_MSGPACK = json.JSONEncoder(indent=2, ensure_ascii=False, default=_msgpack_display)
|
|
36
|
+
|
|
37
|
+
|
|
26
38
|
def _is_textual_content_type(content_type: str) -> bool:
|
|
27
39
|
"""True when the Content-Type looks like text we can safely display."""
|
|
28
40
|
ct = content_type.lower()
|
|
@@ -47,6 +59,12 @@ def format_request(request: httpx.Request, redaction: Redaction = DEFAULT_REDACT
|
|
|
47
59
|
elif (parts := _format_multipart(content, content_type)) is not None:
|
|
48
60
|
body = _format_body_text(parts)
|
|
49
61
|
elif content:
|
|
62
|
+
if is_msgpack_content_type(content_type):
|
|
63
|
+
try:
|
|
64
|
+
body = _format_encoded(_PRETTY_MSGPACK, unpack_msgpack(content))
|
|
65
|
+
except MSGPACK_DECODE_ERRORS:
|
|
66
|
+
body = f"<Binary content: {len(content)} bytes>"
|
|
67
|
+
return _message_lines(f"{request.method} {redaction.url(request.url)}", request.headers, body, redaction)
|
|
50
68
|
try:
|
|
51
69
|
decoded = content.decode()
|
|
52
70
|
except UnicodeDecodeError:
|
|
@@ -137,7 +155,13 @@ def format_response(response: httpx.Response, redaction: Redaction = DEFAULT_RED
|
|
|
137
155
|
body = None
|
|
138
156
|
if response.content:
|
|
139
157
|
content_type = response.headers.get("Content-Type", "")
|
|
140
|
-
|
|
158
|
+
codec = response.extensions.get("httpchain_response_codec", "auto")
|
|
159
|
+
if codec == "msgpack" or (codec == "auto" and is_msgpack_content_type(content_type)):
|
|
160
|
+
try:
|
|
161
|
+
body = _format_encoded(_PRETTY_MSGPACK, unpack_msgpack(response.content))
|
|
162
|
+
except MSGPACK_DECODE_ERRORS:
|
|
163
|
+
body = f"<Binary content: {len(response.content)} bytes>"
|
|
164
|
+
elif "application/json" in content_type.lower():
|
|
141
165
|
try:
|
|
142
166
|
body = _format_json(response.json())
|
|
143
167
|
except JSON_PARSE_ERRORS:
|
|
@@ -160,9 +184,13 @@ def _format_json(value: Any) -> str:
|
|
|
160
184
|
50 MB) only to be cut to ``_MAX_BODY_CHARS``. The encoder streams, so stop
|
|
161
185
|
once past the cap.
|
|
162
186
|
"""
|
|
187
|
+
return _format_encoded(_PRETTY_JSON, value)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def _format_encoded(encoder: json.JSONEncoder, value: Any) -> str:
|
|
163
191
|
chunks: list[str] = []
|
|
164
192
|
length = 0
|
|
165
|
-
for chunk in
|
|
193
|
+
for chunk in encoder.iterencode(value):
|
|
166
194
|
chunks.append(chunk)
|
|
167
195
|
length += len(chunk)
|
|
168
196
|
if length > _MAX_BODY_CHARS:
|
|
@@ -16,7 +16,7 @@ from collections.abc import Generator, Iterable, Iterator, Mapping
|
|
|
16
16
|
from functools import partial
|
|
17
17
|
from pathlib import Path
|
|
18
18
|
from typing import Any
|
|
19
|
-
from urllib.parse import unquote_plus
|
|
19
|
+
from urllib.parse import unquote_plus, urlencode
|
|
20
20
|
|
|
21
21
|
import httpx
|
|
22
22
|
|
|
@@ -27,6 +27,7 @@ from pytest_httpchain.models import (
|
|
|
27
27
|
BasicAuth,
|
|
28
28
|
BearerAuth,
|
|
29
29
|
BinaryBody,
|
|
30
|
+
BytesBody,
|
|
30
31
|
ClientConfig,
|
|
31
32
|
DigestAuth,
|
|
32
33
|
FilesBody,
|
|
@@ -34,6 +35,7 @@ from pytest_httpchain.models import (
|
|
|
34
35
|
FormBody,
|
|
35
36
|
GraphQLBody,
|
|
36
37
|
JsonBody,
|
|
38
|
+
MsgpackBody,
|
|
37
39
|
MultipartBody,
|
|
38
40
|
Request,
|
|
39
41
|
RequestAuth,
|
|
@@ -48,6 +50,7 @@ from pytest_httpchain.redaction import DEFAULT_REDACTION, Redaction
|
|
|
48
50
|
from pytest_httpchain.templates import contains_template
|
|
49
51
|
from pytest_httpchain.userfunc import UserFunctionError, call_target, call_user_function, import_function
|
|
50
52
|
from pytest_httpchain.utils import resolve_scenario_path
|
|
53
|
+
from pytest_httpchain.wire_codec import pack_msgpack
|
|
51
54
|
|
|
52
55
|
|
|
53
56
|
def build_ssl_verify(config: SSLConfig, scenario_dir: Path | None) -> bool | ssl.SSLContext:
|
|
@@ -506,11 +509,45 @@ def _encode_param(key: str, value: Any) -> str:
|
|
|
506
509
|
instead of escaping as the raw exception.
|
|
507
510
|
"""
|
|
508
511
|
try:
|
|
512
|
+
if isinstance(value, list) and any(isinstance(item, bytes | bytearray) for item in value):
|
|
513
|
+
return "&".join(_encode_param(key, item) for item in value)
|
|
514
|
+
if isinstance(value, bytes | bytearray):
|
|
515
|
+
return urlencode({key: bytes(value)})
|
|
509
516
|
return str(httpx.QueryParams({key: value}))
|
|
517
|
+
except RequestError:
|
|
518
|
+
raise
|
|
510
519
|
except Exception as e:
|
|
511
520
|
raise RequestError(f"Cannot convert query parameter '{key}' to text: {type(e).__name__}: {e}") from e
|
|
512
521
|
|
|
513
522
|
|
|
523
|
+
def _encoded_binary_form(data: Mapping[str, Any]) -> bytes | None:
|
|
524
|
+
"""URL-encode form data ourselves only when a field holds bytes.
|
|
525
|
+
|
|
526
|
+
httpx stringifies bytes in ``data=``, losing the packet. For forms without
|
|
527
|
+
bytes, its own encoder remains in use. Its scalar spellings and repeated
|
|
528
|
+
fields are kept here for the mixed binary/text case.
|
|
529
|
+
"""
|
|
530
|
+
fields = [(key, item) for key, value in data.items() for item in (value if isinstance(value, list | tuple) else (value,))]
|
|
531
|
+
if not any(isinstance(value, bytes | bytearray) for _, value in fields):
|
|
532
|
+
return None
|
|
533
|
+
|
|
534
|
+
def scalar(value: Any) -> str | bytes:
|
|
535
|
+
if isinstance(value, bytes | bytearray):
|
|
536
|
+
return bytes(value)
|
|
537
|
+
if value is True:
|
|
538
|
+
return "true"
|
|
539
|
+
if value is False:
|
|
540
|
+
return "false"
|
|
541
|
+
if value is None:
|
|
542
|
+
return ""
|
|
543
|
+
return str(value)
|
|
544
|
+
|
|
545
|
+
try:
|
|
546
|
+
return urlencode([(key, scalar(value)) for key, value in fields]).encode("ascii")
|
|
547
|
+
except Exception as e:
|
|
548
|
+
raise RequestError(f"Cannot encode form data: {type(e).__name__}: {e}") from e
|
|
549
|
+
|
|
550
|
+
|
|
514
551
|
def _merge_query(query: str, params: dict[str, Any], defaults: Mapping[str, Any]) -> str:
|
|
515
552
|
"""The URL's raw ``query`` with ``params`` merged in, and ``defaults``
|
|
516
553
|
(``client.params``) for the keys neither sets.
|
|
@@ -633,11 +670,26 @@ def build_request_kwargs(
|
|
|
633
670
|
case JsonBody(json=data):
|
|
634
671
|
request_kwargs["json"] = data
|
|
635
672
|
|
|
673
|
+
case MsgpackBody(msgpack=data):
|
|
674
|
+
try:
|
|
675
|
+
request_kwargs["content"] = pack_msgpack(data)
|
|
676
|
+
except (TypeError, ValueError, OverflowError, RecursionError) as e:
|
|
677
|
+
raise RequestError(f"Cannot encode MessagePack body: {e}") from e
|
|
678
|
+
if not _declares_content_type((*client.headers, *request_model.headers)):
|
|
679
|
+
request_kwargs["headers"] = {**request_model.headers, "Content-Type": "application/msgpack"}
|
|
680
|
+
body_content_type = "application/msgpack"
|
|
681
|
+
|
|
636
682
|
case GraphQLBody(graphql=gql):
|
|
637
683
|
request_kwargs["json"] = {"query": gql.query, "variables": gql.variables}
|
|
638
684
|
|
|
639
685
|
case FormBody(form=data):
|
|
640
|
-
|
|
686
|
+
encoded = _encoded_binary_form(data)
|
|
687
|
+
if encoded is None:
|
|
688
|
+
request_kwargs["data"] = data
|
|
689
|
+
else:
|
|
690
|
+
request_kwargs["content"] = encoded
|
|
691
|
+
if not _declares_content_type(request_model.headers):
|
|
692
|
+
request_kwargs["headers"] = {**request_model.headers, "Content-Type": "application/x-www-form-urlencoded"}
|
|
641
693
|
if data:
|
|
642
694
|
body_content_type = "application/x-www-form-urlencoded"
|
|
643
695
|
|
|
@@ -651,6 +703,11 @@ def build_request_kwargs(
|
|
|
651
703
|
path = resolve_scenario_path(scenario_dir, file_path)
|
|
652
704
|
request_kwargs["content"] = _read_file(path, file_path, "Binary file not found", "Cannot read binary file")
|
|
653
705
|
|
|
706
|
+
case BytesBody(bytes=data):
|
|
707
|
+
if not isinstance(data, bytes):
|
|
708
|
+
raise RequestError("The bytes body must resolve to bytes")
|
|
709
|
+
request_kwargs["content"] = data
|
|
710
|
+
|
|
654
711
|
case FilesBody(files=files):
|
|
655
712
|
multipart_parts = list(_file_parts(files, scenario_dir))
|
|
656
713
|
|
|
@@ -15,7 +15,7 @@ from collections import ChainMap
|
|
|
15
15
|
from collections.abc import Callable, Iterable, Iterator
|
|
16
16
|
from dataclasses import dataclass
|
|
17
17
|
from pathlib import Path
|
|
18
|
-
from typing import Any, NoReturn
|
|
18
|
+
from typing import Any, Literal, NoReturn
|
|
19
19
|
|
|
20
20
|
import httpx
|
|
21
21
|
import jmespath
|
|
@@ -46,15 +46,37 @@ from pytest_httpchain.redaction import DEFAULT_REDACTION, REDACTED, Redaction
|
|
|
46
46
|
from pytest_httpchain.templates import TemplatesError, contains_template
|
|
47
47
|
from pytest_httpchain.userfunc import UserFunctionError, call_target, call_user_function
|
|
48
48
|
from pytest_httpchain.utils import optional_as_list, path_segment, process_substitutions, schema_error_text
|
|
49
|
+
from pytest_httpchain.wire_codec import MSGPACK_DECODE_ERRORS, is_msgpack_content_type, unpack_msgpack
|
|
49
50
|
|
|
51
|
+
type ResponseCodec = Literal["auto", "json", "msgpack"]
|
|
50
52
|
|
|
51
|
-
|
|
53
|
+
|
|
54
|
+
def _selected_codec(response: httpx.Response, codec: ResponseCodec) -> Literal["json", "msgpack"]:
|
|
55
|
+
if codec == "auto":
|
|
56
|
+
return "msgpack" if is_msgpack_content_type(response.headers.get("content-type", "")) else "json"
|
|
57
|
+
return codec
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _parse_structured(response: httpx.Response, codec: ResponseCodec) -> Any:
|
|
61
|
+
if _selected_codec(response, codec) == "msgpack":
|
|
62
|
+
return unpack_msgpack(response.content)
|
|
63
|
+
return response.json()
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _parse_failure(error: Exception, codec: Literal["json", "msgpack"], purpose: str) -> str:
|
|
67
|
+
if isinstance(error, RecursionError) and codec == "json":
|
|
68
|
+
return f"Cannot {purpose}, response JSON is nested too deeply to parse: {error}"
|
|
69
|
+
label = "JSON" if codec == "json" else "MessagePack"
|
|
70
|
+
return f"Cannot {purpose}, response is not valid {label}: {str(error) or type(error).__name__}"
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def process_save(save_model: Save, response: httpx.Response, context: ChainMap[str, Any], *, codec: ResponseCodec = "auto") -> dict[str, Any]:
|
|
52
74
|
"""Extract one save step's ``{name: value}`` contribution to the context."""
|
|
53
75
|
step_saved: dict[str, Any] = {}
|
|
54
76
|
|
|
55
77
|
match save_model:
|
|
56
78
|
case JMESPathSave():
|
|
57
|
-
response_json =
|
|
79
|
+
response_json = _response_data(response, SaveError, "extract variables", codec)
|
|
58
80
|
for var_name, jmespath_expr in save_model.jmespath.items():
|
|
59
81
|
try:
|
|
60
82
|
step_saved[var_name] = jmespath.search(jmespath_expr, response_json)
|
|
@@ -167,6 +189,7 @@ def process_verify(
|
|
|
167
189
|
redaction: Redaction = DEFAULT_REDACTION,
|
|
168
190
|
render: VerifyRender | None = None,
|
|
169
191
|
ref_bounds: ReferenceBounds = UNBOUNDED,
|
|
192
|
+
codec: ResponseCodec = "auto",
|
|
170
193
|
) -> None:
|
|
171
194
|
"""Run one verify step's assertions, raising one `VerificationError` that
|
|
172
195
|
names every check that failed.
|
|
@@ -193,7 +216,7 @@ def process_verify(
|
|
|
193
216
|
A check that cannot run fails once, not once per assertion it held: the
|
|
194
217
|
body is parsed as JSON at most once for the step, by the first check that
|
|
195
218
|
reads it, and one that is not JSON is one failure however many of the
|
|
196
|
-
step's checks wanted it, jmespath entries and body.schema (`
|
|
219
|
+
step's checks wanted it, jmespath entries and body.schema (`_StructuredBody`).
|
|
197
220
|
|
|
198
221
|
A user function's pytest.skip(), xfail() or fail() ends the step there, as
|
|
199
222
|
it ends the stage, and so does one that a function a template calls
|
|
@@ -221,7 +244,7 @@ def process_verify(
|
|
|
221
244
|
may name any local file.
|
|
222
245
|
"""
|
|
223
246
|
failures = _Failures()
|
|
224
|
-
body =
|
|
247
|
+
body = _StructuredBody(response, failures, codec)
|
|
225
248
|
step = _RenderedStep(verify_model, render)
|
|
226
249
|
|
|
227
250
|
def end_step(outcome: BaseException, raised_by: str, where: VerifyWhere) -> NoReturn:
|
|
@@ -554,29 +577,26 @@ def _status_failure(expected: Any, actual: int) -> str | None:
|
|
|
554
577
|
return f"Status code doesn't match: expected {shown}, got {actual}"
|
|
555
578
|
|
|
556
579
|
|
|
557
|
-
# A `
|
|
580
|
+
# A `_StructuredBody` no check has read yet: None is a body, JSON's null.
|
|
558
581
|
_UNPARSED = object()
|
|
559
|
-
# A `
|
|
582
|
+
# A `_StructuredBody` could not decode, which the step has a failure for.
|
|
560
583
|
_UNPARSABLE = object()
|
|
561
584
|
|
|
562
585
|
|
|
563
|
-
class
|
|
564
|
-
"""A verify step's response body
|
|
586
|
+
class _StructuredBody:
|
|
587
|
+
"""A verify step's response body, parsed by the first check that
|
|
565
588
|
reads it and kept for the next: jmespath and body.schema share one parse.
|
|
566
589
|
|
|
567
|
-
A body
|
|
568
|
-
that asked first; the checks after it that need the body do not run.
|
|
569
|
-
is a ValueError: JSONDecodeError and UnicodeDecodeError are ones, and so
|
|
570
|
-
is the error for an integer past Python's digit limit, which json raises
|
|
571
|
-
bare. Or it is a RecursionError, for valid JSON nested deeper than json
|
|
572
|
-
can parse (``[[[...]]]`` some thousand levels deep).
|
|
590
|
+
A body the codec cannot read is one failure of the step, recorded by the
|
|
591
|
+
check that asked first; the checks after it that need the body do not run.
|
|
573
592
|
"""
|
|
574
593
|
|
|
575
|
-
__slots__ = ("_failures", "_response", "_value")
|
|
594
|
+
__slots__ = ("_codec", "_failures", "_response", "_value")
|
|
576
595
|
|
|
577
|
-
def __init__(self, response: httpx.Response, failures: _Failures) -> None:
|
|
596
|
+
def __init__(self, response: httpx.Response, failures: _Failures, codec: ResponseCodec) -> None:
|
|
578
597
|
self._response = response
|
|
579
598
|
self._failures = failures
|
|
599
|
+
self._codec = codec
|
|
580
600
|
self._value: Any = _UNPARSED
|
|
581
601
|
|
|
582
602
|
def parsed(self, check: str) -> Any:
|
|
@@ -585,13 +605,10 @@ class _JsonBody:
|
|
|
585
605
|
verify.jmespath"``) cannot be done on it."""
|
|
586
606
|
if self._value is _UNPARSED:
|
|
587
607
|
try:
|
|
588
|
-
self._value = self._response.
|
|
589
|
-
except
|
|
590
|
-
self._value = _UNPARSABLE
|
|
591
|
-
self._failures.add(f"Cannot {check}, response is not valid JSON: {e}", cause=e)
|
|
592
|
-
except RecursionError as e:
|
|
608
|
+
self._value = _parse_structured(self._response, self._codec)
|
|
609
|
+
except MSGPACK_DECODE_ERRORS as e:
|
|
593
610
|
self._value = _UNPARSABLE
|
|
594
|
-
self._failures.add(
|
|
611
|
+
self._failures.add(_parse_failure(e, _selected_codec(self._response, self._codec), check), cause=e)
|
|
595
612
|
return self._value
|
|
596
613
|
|
|
597
614
|
|
|
@@ -761,6 +778,8 @@ def json_type(value: Any) -> str:
|
|
|
761
778
|
return "number"
|
|
762
779
|
case str():
|
|
763
780
|
return "string"
|
|
781
|
+
case bytes():
|
|
782
|
+
return "binary"
|
|
764
783
|
case list():
|
|
765
784
|
return "array"
|
|
766
785
|
case dict():
|
|
@@ -792,8 +811,12 @@ def _shown(value: Any) -> str:
|
|
|
792
811
|
back an expression reference (``&name``) as it is, from ``not_null(&a)``
|
|
793
812
|
or ``to_array(&a)``, or names one in a type error (``length(&a)``).
|
|
794
813
|
"""
|
|
814
|
+
if isinstance(value, bytes):
|
|
815
|
+
shown = value[:64].hex()
|
|
816
|
+
suffix = f"... ({len(value)} bytes)" if len(value) > 64 else ""
|
|
817
|
+
return f"0x{shown}{suffix}"
|
|
795
818
|
try:
|
|
796
|
-
text = json.dumps(value, ensure_ascii=False)
|
|
819
|
+
text = json.dumps(value, ensure_ascii=False, default=_shown_non_json)
|
|
797
820
|
except ValueError:
|
|
798
821
|
return f"(a {json_type(value)} too long to show)"
|
|
799
822
|
except TypeError:
|
|
@@ -801,7 +824,13 @@ def _shown(value: Any) -> str:
|
|
|
801
824
|
return text if len(text) <= _SHOWN_MAX else f"{text[:_SHOWN_MAX]}... ({len(text)} characters)"
|
|
802
825
|
|
|
803
826
|
|
|
804
|
-
def
|
|
827
|
+
def _shown_non_json(value: Any) -> str:
|
|
828
|
+
if isinstance(value, bytes):
|
|
829
|
+
return _shown(value)
|
|
830
|
+
raise TypeError(f"Cannot show {type(value).__name__} as JSON")
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
def _verify_body_schema(schema: Any, body: _StructuredBody, scenario_dir: Path | None, ref_bounds: ReferenceBounds, failures: _Failures) -> None:
|
|
805
834
|
"""Validate the response body against an inline JSON Schema, or one in a
|
|
806
835
|
file, where a JSON pointer may select it (`body_schema`): one check, which
|
|
807
836
|
fails once, for its first violation, or for why it cannot run.
|
|
@@ -873,15 +902,12 @@ def _verify_body_schema(schema: Any, body: _JsonBody, scenario_dir: Path | None,
|
|
|
873
902
|
failures.add(f"Cannot validate against {body_schema.where}, a schema it references is not valid: {schema_error_text(e)}", cause=e, retryable=False)
|
|
874
903
|
|
|
875
904
|
|
|
876
|
-
def
|
|
877
|
-
"""
|
|
878
|
-
the words `_JsonBody.parsed` uses for a verify step."""
|
|
905
|
+
def _response_data(response: httpx.Response, error: type[StageExecutionError], purpose: str, codec: ResponseCodec) -> Any:
|
|
906
|
+
"""A structured body, or an error naming why it could not be decoded."""
|
|
879
907
|
try:
|
|
880
|
-
return response
|
|
881
|
-
except
|
|
882
|
-
raise error(
|
|
883
|
-
except RecursionError as e:
|
|
884
|
-
raise error(f"Cannot {purpose}, response JSON is nested too deeply to parse: {e}") from e
|
|
908
|
+
return _parse_structured(response, codec)
|
|
909
|
+
except MSGPACK_DECODE_ERRORS as e:
|
|
910
|
+
raise error(_parse_failure(e, _selected_codec(response, codec), purpose)) from e
|
|
885
911
|
|
|
886
912
|
|
|
887
913
|
def _schema_violation(error: jsonschema.ValidationError) -> str:
|
{pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/templates/functions.py
RENAMED
|
@@ -20,6 +20,8 @@ from datetime import UTC, datetime
|
|
|
20
20
|
from types import SimpleNamespace
|
|
21
21
|
from typing import Any
|
|
22
22
|
|
|
23
|
+
from pytest_httpchain.wire_codec import pack_msgpack
|
|
24
|
+
|
|
23
25
|
|
|
24
26
|
def _kind(value: Any) -> str:
|
|
25
27
|
"""How an error names a value's type: a ``vars`` object renders as a
|
|
@@ -93,6 +95,18 @@ def b64encode(value: str | bytes, urlsafe: bool = False) -> str:
|
|
|
93
95
|
return encode(data).decode("ascii")
|
|
94
96
|
|
|
95
97
|
|
|
98
|
+
def _decode_base64(function: str, value: str | bytes, urlsafe: bool) -> bytes:
|
|
99
|
+
urlsafe = _require_bool(function, "urlsafe", urlsafe)
|
|
100
|
+
if isinstance(value, str) and not value.isascii():
|
|
101
|
+
raise ValueError(f"{function}() got text that is not base64: it holds a character outside ASCII")
|
|
102
|
+
data = _to_bytes(function, value)
|
|
103
|
+
try:
|
|
104
|
+
return base64.b64decode(data + b"=" * (-len(data) % 4), altchars=b"-_" if urlsafe else None, validate=True)
|
|
105
|
+
except binascii.Error as e:
|
|
106
|
+
hint = "; for URL-safe base64, pass urlsafe=true" if not urlsafe and (b"-" in data or b"_" in data) else ""
|
|
107
|
+
raise ValueError(f"{function}() got text that is not base64 ({e}){hint}") from None
|
|
108
|
+
|
|
109
|
+
|
|
96
110
|
def b64decode(value: str | bytes, urlsafe: bool = False) -> str:
|
|
97
111
|
"""The UTF-8 text a base64 string encodes.
|
|
98
112
|
|
|
@@ -101,21 +115,40 @@ def b64decode(value: str | bytes, urlsafe: bool = False) -> str:
|
|
|
101
115
|
character outside the alphabet fails, whitespace included, where the
|
|
102
116
|
standard library by default skips it and decodes what is left.
|
|
103
117
|
"""
|
|
104
|
-
|
|
105
|
-
if isinstance(value, str) and not value.isascii():
|
|
106
|
-
raise ValueError("b64decode() got text that is not base64: it holds a character outside ASCII")
|
|
107
|
-
data = _to_bytes("b64decode", value)
|
|
108
|
-
try:
|
|
109
|
-
decoded = base64.b64decode(data + b"=" * (-len(data) % 4), altchars=b"-_" if urlsafe else None, validate=True)
|
|
110
|
-
except binascii.Error as e:
|
|
111
|
-
hint = "; for URL-safe base64, pass urlsafe=true" if not urlsafe and (b"-" in data or b"_" in data) else ""
|
|
112
|
-
raise ValueError(f"b64decode() got text that is not base64 ({e}){hint}") from None
|
|
118
|
+
decoded = _decode_base64("b64decode", value, urlsafe)
|
|
113
119
|
try:
|
|
114
120
|
return decoded.decode("utf-8")
|
|
115
121
|
except UnicodeDecodeError:
|
|
116
122
|
raise ValueError(f"b64decode() decoded {len(decoded)} bytes that are not UTF-8 text") from None
|
|
117
123
|
|
|
118
124
|
|
|
125
|
+
def b64decode_bytes(value: str | bytes, urlsafe: bool = False) -> bytes:
|
|
126
|
+
"""Decode base64 to bytes, with optional padding and URL-safe alphabet."""
|
|
127
|
+
return _decode_base64("b64decode_bytes", value, urlsafe)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def hex_bytes(value: str) -> bytes:
|
|
131
|
+
"""Construct binary data from hexadecimal text."""
|
|
132
|
+
if not isinstance(value, str):
|
|
133
|
+
raise TypeError(f"hex_bytes() takes text, not {_kind(value)}")
|
|
134
|
+
try:
|
|
135
|
+
return bytes.fromhex(value)
|
|
136
|
+
except ValueError as e:
|
|
137
|
+
raise ValueError(f"hex_bytes() got invalid hexadecimal text ({e})") from None
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def hexencode(value: bytes) -> str:
|
|
141
|
+
"""Readable hexadecimal for a binary value, including one saved from a response."""
|
|
142
|
+
if not isinstance(value, bytes | bytearray):
|
|
143
|
+
raise TypeError(f"hexencode() takes bytes, not {_kind(value)}")
|
|
144
|
+
return value.hex()
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def msgpack_pack(value: Any) -> bytes:
|
|
148
|
+
"""MessagePack bytes for a raw body or a URL parameter."""
|
|
149
|
+
return pack_msgpack(value)
|
|
150
|
+
|
|
151
|
+
|
|
119
152
|
def _namespace_to_dict(value: Any) -> Any:
|
|
120
153
|
# json.dumps calls this for any value it cannot encode itself, and encodes
|
|
121
154
|
# what it returns in its place, circular-reference check included.
|
|
@@ -225,6 +258,10 @@ HELPER_FUNCTIONS = {
|
|
|
225
258
|
"timestamp_ms": timestamp_ms,
|
|
226
259
|
"b64encode": b64encode,
|
|
227
260
|
"b64decode": b64decode,
|
|
261
|
+
"b64decode_bytes": b64decode_bytes,
|
|
262
|
+
"hex_bytes": hex_bytes,
|
|
263
|
+
"hexencode": hexencode,
|
|
264
|
+
"msgpack_pack": msgpack_pack,
|
|
228
265
|
"json_dumps": json_dumps,
|
|
229
266
|
"json_loads": json_loads,
|
|
230
267
|
"urlencode": urlencode,
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""MessagePack's wire representation, shared by expressions and HTTP steps."""
|
|
2
|
+
|
|
3
|
+
from types import SimpleNamespace
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
import msgpack
|
|
7
|
+
|
|
8
|
+
MSGPACK_DECODE_ERRORS = (ValueError, TypeError, RecursionError, msgpack.UnpackException)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _pack_default(value: Any) -> Any:
|
|
12
|
+
# Scenario vars are namespaces so templates can use attribute access.
|
|
13
|
+
if isinstance(value, SimpleNamespace):
|
|
14
|
+
return vars(value)
|
|
15
|
+
raise TypeError(f"msgpack_pack() cannot encode {type(value).__name__}")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def pack_msgpack(value: Any) -> bytes:
|
|
19
|
+
"""Pack a value, keeping text and binary distinct on the wire."""
|
|
20
|
+
return msgpack.packb(value, use_bin_type=True, default=_pack_default)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def unpack_msgpack(data: bytes) -> Any:
|
|
24
|
+
"""Decode one value; MessagePack bin values stay Python bytes."""
|
|
25
|
+
return msgpack.unpackb(data, raw=False, strict_map_key=True)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def is_msgpack_content_type(content_type: str) -> bool:
|
|
29
|
+
media_type = content_type.partition(";")[0].strip().lower()
|
|
30
|
+
return media_type in {"application/msgpack", "application/x-msgpack"} or media_type.endswith("+msgpack")
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/importers/__init__.py
RENAMED
|
File without changes
|
{pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/importers/builder.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/jsonref/__init__.py
RENAMED
|
File without changes
|
{pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/jsonref/equality.py
RENAMED
|
File without changes
|
{pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/jsonref/exceptions.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/jsonref/plumbing/path.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/templates/__init__.py
RENAMED
|
File without changes
|
{pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/templates/exceptions.py
RENAMED
|
File without changes
|
{pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/templates/expressions.py
RENAMED
|
File without changes
|
{pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/templates/substitution.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/validation/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
{pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/validation/diagnostics.py
RENAMED
|
File without changes
|
{pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/validation/discovery.py
RENAMED
|
File without changes
|
{pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/validation/loader.py
RENAMED
|
File without changes
|
{pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/validation/semantic.py
RENAMED
|
File without changes
|
{pytest_httpchain-0.17.1 → pytest_httpchain-0.18.1}/src/pytest_httpchain/validation/validate.py
RENAMED
|
File without changes
|
|
File without changes
|