pytest-httpchain 0.17.0__tar.gz → 0.18.0__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.
Files changed (55) hide show
  1. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/PKG-INFO +4 -3
  2. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/README.md +2 -2
  3. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/pyproject.toml +3 -1
  4. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/pyproject.toml.orig +4 -1
  5. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/carrier.py +13 -3
  6. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/dataflow.py +6 -17
  7. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/har_writer.py +3 -3
  8. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/importers/builder.py +8 -2
  9. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/jsonref/plumbing/path.py +6 -0
  10. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/models/__init__.py +4 -0
  11. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/models/entities.py +22 -6
  12. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/report_formatter.py +33 -5
  13. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/request_builder.py +24 -1
  14. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/response_steps.py +59 -33
  15. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/scoping.py +44 -15
  16. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/templates/functions.py +46 -9
  17. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/templates/substitution.py +12 -3
  18. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/userfunc.py +4 -4
  19. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/validation/deep.py +6 -2
  20. pytest_httpchain-0.18.0/src/pytest_httpchain/wire_codec.py +30 -0
  21. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/LICENSE +0 -0
  22. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/__init__.py +0 -0
  23. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/body_schema.py +0 -0
  24. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/cli.py +0 -0
  25. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/constants.py +0 -0
  26. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/errors.py +0 -0
  27. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/factory.py +0 -0
  28. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/importers/__init__.py +0 -0
  29. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/importers/curl.py +0 -0
  30. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/importers/har.py +0 -0
  31. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/jsonref/__init__.py +0 -0
  32. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/jsonref/equality.py +0 -0
  33. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/jsonref/exceptions.py +0 -0
  34. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/jsonref/jsonc.py +0 -0
  35. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/jsonref/loader.py +0 -0
  36. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/jsonref/plumbing/__init__.py +0 -0
  37. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/jsonref/plumbing/circular.py +0 -0
  38. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/jsonref/plumbing/reference.py +0 -0
  39. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/models/types.py +0 -0
  40. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/parallel_stats.py +0 -0
  41. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/plugin.py +0 -0
  42. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/py.typed +0 -0
  43. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/redaction.py +0 -0
  44. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/schema.py +0 -0
  45. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/templates/__init__.py +0 -0
  46. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/templates/exceptions.py +0 -0
  47. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/templates/expressions.py +0 -0
  48. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/utils.py +0 -0
  49. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/validation/__init__.py +0 -0
  50. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/validation/diagnostics.py +0 -0
  51. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/validation/discovery.py +0 -0
  52. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/validation/loader.py +0 -0
  53. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/validation/semantic.py +0 -0
  54. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/src/pytest_httpchain/validation/validate.py +0 -0
  55. {pytest_httpchain-0.17.0 → pytest_httpchain-0.18.0}/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.17.0
3
+ Version: 0.18.0
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.17.0"
3
+ version = "0.18.0"
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.17.0"
3
+ version = "0.18.0"
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 == "json")
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(step.verify, response, cls.scenario_dir, cls.redaction, render=_verify_renderer(step.verify, step_context), ref_bounds=cls.ref_bounds)
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__}")
@@ -13,9 +13,8 @@ from pytest_httpchain.scoping import (
13
13
  RESPONSE_META_NAME,
14
14
  defined_names,
15
15
  extract_template_variables,
16
- raw_list_entries,
17
16
  raw_stages,
18
- saved_in_step,
17
+ response_step_templates,
19
18
  stage_scopes,
20
19
  substitution_names,
21
20
  substitution_step_templates,
@@ -123,22 +122,12 @@ def analyze_dataflow(scenario: Scenario, test_data: dict[str, Any]) -> DataFlow:
123
122
  consumes |= _consumed(extract_template_variables(raw.get("parallel"), defined=defined), scope.earlier_saves, scope.pre_iteration_shadows)
124
123
  consumes |= _consumed(extract_template_variables(raw.get("retry"), defined=defined), scope.earlier_saves, scope.pre_iteration_shadows)
125
124
  consumes |= _consumed(extract_template_variables(raw.get("request"), defined=defined), scope.earlier_saves, scope.request_shadows)
126
- # Response steps resolve in order, each save layering its names over the
127
- # context (the runtime's per-step with_saves): once a step re-saves a
128
- # name, later steps read this stage's fresh value, not the earlier
129
- # stage's — so accumulated own saves join the shadow set step by step.
130
- # The `response` namespace likewise shadows a same-named save.
131
- own_saves: frozenset[str] = frozenset()
132
- # raw_list_entries, not an isinstance(list) guard: the name-keyed mapping
133
- # form of `response` is first-class, and discarding it left every step's
134
- # raw text unread — so show/graph reported a consuming stage as
135
- # consuming nothing and dropped the dependency edge entirely.
136
- raw_response = raw_list_entries(raw.get("response"))
137
- for k, step in enumerate(stage.response):
138
- step_raw = raw_response[k] if k < len(raw_response) else None
139
- step_refs = extract_template_variables(step_raw, defined=defined) - {RESPONSE_META_NAME}
125
+ # The same order-aware iterator the validator uses also sees prior
126
+ # entries inside a substitutions-save step. Those entries resolve in
127
+ # sequence, and only their rendered values can consume an earlier save.
128
+ for templates, own_saves in response_step_templates(stage, raw.get("response")):
129
+ step_refs = extract_template_variables(templates, defined=defined) - {RESPONSE_META_NAME}
140
130
  consumes |= _consumed(step_refs, scope.earlier_saves, scope.request_shadows | own_saves)
141
- own_saves |= frozenset(saved_in_step(step))
142
131
  consumes |= _consumed(extract_template_variables(raw.get("always_run"), defined=defined), scope.earlier_saves, scope.always_run_shadows)
143
132
 
144
133
  by_producer: dict[int, list[str]] = {}
@@ -10,7 +10,7 @@ import hashlib
10
10
  import json
11
11
  import re
12
12
  from datetime import UTC, datetime
13
- from importlib.metadata import version
13
+ from importlib.metadata import PackageNotFoundError, version
14
14
  from pathlib import Path
15
15
  from typing import Any
16
16
  from urllib.parse import parse_qsl, urlparse
@@ -31,7 +31,7 @@ def _get_version() -> str:
31
31
  change in-process."""
32
32
  try:
33
33
  return version("pytest-httpchain")
34
- except Exception:
34
+ except PackageNotFoundError:
35
35
  return "unknown"
36
36
 
37
37
 
@@ -103,7 +103,7 @@ def _format_post_data(request: httpx.Request) -> dict[str, Any] | None:
103
103
  body_text = _body_text(content)
104
104
  post_data: dict[str, Any] = {"mimeType": _mime_type(content_type), **body_text}
105
105
 
106
- if "application/x-www-form-urlencoded" in content_type and "encoding" not in body_text:
106
+ if "application/x-www-form-urlencoded" in content_type.lower() and "encoding" not in body_text:
107
107
  # HAR params are repeated scalar name/value records, not one record with
108
108
  # an array value. parse_qsl also preserves the body's original ordering.
109
109
  post_data["params"] = [{"name": name, "value": value} for name, value in parse_qsl(body_text["text"], keep_blank_values=True)]
@@ -741,8 +741,9 @@ class _Builder:
741
741
  # as a host: absolute, that one URL ignores base_url.
742
742
  target = path if base_url is not None and not path.startswith("//") else f"{_origin(url.scheme, url.netloc)}{path}"
743
743
  params, query = self.query(url.query, stage)
744
+ empty_query = not url.query and recorded.url.partition("#")[0].endswith("?")
744
745
  # Closed before a query holding a placeholder.
745
- request["url"] = escape(target, closed=query is not None and contains_template(query)) + (f"?{query}" if query is not None else "")
746
+ request["url"] = escape(target, closed=query is not None and contains_template(query)) + (f"?{query or ''}" if query is not None or empty_query else "")
746
747
  if params:
747
748
  request["params"] = params
748
749
 
@@ -821,7 +822,12 @@ class _Builder:
821
822
  holds a secret, since ``params`` sends a value rendered to null
822
823
  empty, where the URL's placeholder fails the stage unset
823
824
  (`fields_text`). A piece without ``=`` is sent as an empty value."""
824
- pairs = _decoded_pairs([piece for piece in query.split("&") if piece])
825
+ if not query:
826
+ return None, None
827
+ pieces = query.split("&")
828
+ # An empty component (a leading, repeated or trailing ampersand) is
829
+ # lost when a query is rewritten as params. Keep such queries intact.
830
+ pairs = _decoded_pairs(pieces) if all(pieces) else None
825
831
  if pairs is not None and _mappable([name for name, _ in pairs]) and not self.any_secret(pairs):
826
832
  return _grouped([(name, escape(value)) for name, value in pairs]), None
827
833
  return None, self.fields_text(query, "query parameter", stage)
@@ -1,5 +1,6 @@
1
1
  """Reference path and JSON pointer helpers for reference resolution."""
2
2
 
3
+ import re
3
4
  import warnings
4
5
  from pathlib import Path, PurePosixPath, PureWindowsPath
5
6
 
@@ -90,4 +91,9 @@ def parse_json_pointer(pointer: str) -> list[str]:
90
91
  if not pointer.startswith("/"):
91
92
  raise ReferenceResolverError(f"Invalid JSON pointer: {pointer} (must start with '/')")
92
93
 
94
+ # RFC 6901 reserves ``~`` for the two escapes below. Leaving any other
95
+ # sequence literal would silently select a key the pointer does not name.
96
+ if re.search(r"~(?![01])", pointer):
97
+ raise ReferenceResolverError(f"Invalid JSON pointer: {pointer} (invalid '~' escape)")
98
+
93
99
  return [part.replace("~1", "/").replace("~0", "~") for part in pointer[1:].split("/")]
@@ -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,6 +26,7 @@ from pydantic import (
26
26
  PositiveFloat,
27
27
  PositiveInt,
28
28
  RootModel,
29
+ StrictBytes,
29
30
  Tag,
30
31
  TypeAdapter,
31
32
  ValidationError,
@@ -460,6 +461,14 @@ class BinaryBody(StrictModel):
460
461
  binary: SerializablePath | PartialTemplateStr = Field(description="Path to binary file.")
461
462
 
462
463
 
464
+ class BytesBody(StrictModel):
465
+ bytes: StrictBytes | TemplateExpressionOnly = Field(description="Raw bytes from a template expression, such as msgpack_pack(value).")
466
+
467
+
468
+ class MsgpackBody(StrictModel):
469
+ msgpack: Any = Field(description="MessagePack value to encode. Templates may provide binary values within it.")
470
+
471
+
463
472
  class FileSpec(StrictModel):
464
473
  """A file of a multipart body, written as an object: where its bytes come
465
474
  from (exactly one of path, content and base64), and the filename and
@@ -596,6 +605,8 @@ get_request_body_discriminator = _create_discriminator(
596
605
  TextBody: "text",
597
606
  Base64Body: "base64",
598
607
  BinaryBody: "binary",
608
+ BytesBody: "bytes",
609
+ MsgpackBody: "msgpack",
599
610
  FilesBody: "files",
600
611
  MultipartBody: "multipart",
601
612
  GraphQLBody: "graphql",
@@ -610,6 +621,8 @@ RequestBody = Annotated[
610
621
  | Annotated[TextBody, Tag("text")]
611
622
  | Annotated[Base64Body, Tag("base64")]
612
623
  | Annotated[BinaryBody, Tag("binary")]
624
+ | Annotated[BytesBody, Tag("bytes")]
625
+ | Annotated[MsgpackBody, Tag("msgpack")]
613
626
  | Annotated[FilesBody, Tag("files")]
614
627
  | Annotated[MultipartBody, Tag("multipart")]
615
628
  | Annotated[GraphQLBody, Tag("graphql")],
@@ -836,7 +849,7 @@ class HeaderMatcher(StrictModel):
836
849
 
837
850
  # An operand compared as JSON. A template over `vars` renders an object as a
838
851
  # SimpleNamespace, which stands for the object it was written as.
839
- _JsonOperand = Annotated[JsonValue, BeforeValidator(convert_namespace_to_dict)]
852
+ _JsonOperand = Annotated[JsonValue | bytes, BeforeValidator(convert_namespace_to_dict)]
840
853
 
841
854
 
842
855
  def _operand_schema(schema: JsonDict) -> None:
@@ -965,7 +978,7 @@ def _declared_value(v: Any) -> Any:
965
978
  # A value compared by JSON equality: anything but an object, which is a matcher.
966
979
  # The schema says so, so an editor holds a literal object to the matcher's keys.
967
980
  _JsonEqualityValue = Annotated[
968
- JsonValue,
981
+ JsonValue | bytes,
969
982
  WithJsonSchema({"type": ["string", "number", "boolean", "null", "array"], "description": "Equal to this JSON value (true is not 1; 1 equals 1.0)."}),
970
983
  ]
971
984
 
@@ -1086,7 +1099,7 @@ class IndividualParameter(StrictModel):
1086
1099
 
1087
1100
  @model_validator(mode="after")
1088
1101
  def validate_ids_match_values(self) -> Self:
1089
- if self.ids and self.individual:
1102
+ if self.ids is not None:
1090
1103
  values = next(iter(self.individual.values()))
1091
1104
  if isinstance(values, str): # template form: count unknown until runtime
1092
1105
  return self
@@ -1118,9 +1131,8 @@ class CombinationsParameter(StrictModel):
1118
1131
  if combo_keys != first_keys:
1119
1132
  raise ValueError(f"Combination {i} has different parameters than combination 0")
1120
1133
 
1121
- if self.ids and self.combinations:
1122
- if len(self.ids) != len(self.combinations):
1123
- raise ValueError(f"Number of ids ({len(self.ids)}) must match number of combinations ({len(self.combinations)})")
1134
+ if self.ids is not None and len(self.ids) != len(self.combinations):
1135
+ raise ValueError(f"Number of ids ({len(self.ids)}) must match number of combinations ({len(self.combinations)})")
1124
1136
  return self
1125
1137
 
1126
1138
 
@@ -1335,6 +1347,10 @@ class Stage(Marked, Fixtured, Descripted):
1335
1347
  "With parallel, each iteration retries on its own.",
1336
1348
  )
1337
1349
  request: Request = Field(description="HTTP request details.")
1350
+ response_codec: Literal["auto", "json", "msgpack"] = Field(
1351
+ default="auto",
1352
+ description="Codec for JMESPath and body schema checks: auto uses MessagePack for a MessagePack Content-Type, otherwise JSON.",
1353
+ )
1338
1354
  response: Responses = Field(default_factory=list, description="Sequential steps to process the response.")
1339
1355
 
1340
1356
 
@@ -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,13 +59,19 @@ 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:
53
71
  body = f"<Binary content: {len(content)} bytes>"
54
72
  else:
55
73
  body = _format_body_text(decoded)
56
- if "application/json" in content_type:
74
+ if "application/json" in content_type.lower():
57
75
  try:
58
76
  body = _format_json(json.loads(decoded))
59
77
  except JSON_PARSE_ERRORS:
@@ -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
- if "application/json" in content_type:
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 _PRETTY_JSON.iterencode(value):
193
+ for chunk in encoder.iterencode(value):
166
194
  chunks.append(chunk)
167
195
  length += len(chunk)
168
196
  if length > _MAX_BODY_CHARS:
@@ -255,7 +283,7 @@ def format_curl(request: httpx.Request, redaction: Redaction = DEFAULT_REDACTION
255
283
  if lowered == "authorization" and value[: len(_DIGEST_SCHEME)].lower() == _DIGEST_SCHEME:
256
284
  notes.append("The Digest Authorization answered one challenge and is left out: add --digest -u 'user:password' to answer a new one.")
257
285
  continue
258
- if lowered == "content-type" and content is None and value.startswith("multipart/"):
286
+ if lowered == "content-type" and content is None and value.lower().startswith("multipart/"):
259
287
  # Its boundary is the uncaptured body's: -F writes its own.
260
288
  continue
261
289
  shown = redaction.header(name, value)
@@ -265,7 +293,7 @@ def format_curl(request: httpx.Request, redaction: Redaction = DEFAULT_REDACTION
265
293
 
266
294
  data: str | None = None
267
295
  if content is None:
268
- if content_type is not None and content_type.startswith("multipart/"):
296
+ if content_type is not None and content_type.lower().startswith("multipart/"):
269
297
  notes.append("The multipart body was not captured: add each part with -F 'name=@file'.")
270
298
  else:
271
299
  notes.append("The streamed body was not captured: add it with --data-binary @file.")
@@ -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,7 +509,13 @@ 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
 
@@ -633,6 +642,15 @@ def build_request_kwargs(
633
642
  case JsonBody(json=data):
634
643
  request_kwargs["json"] = data
635
644
 
645
+ case MsgpackBody(msgpack=data):
646
+ try:
647
+ request_kwargs["content"] = pack_msgpack(data)
648
+ except (TypeError, ValueError, OverflowError, RecursionError) as e:
649
+ raise RequestError(f"Cannot encode MessagePack body: {e}") from e
650
+ if not _declares_content_type((*client.headers, *request_model.headers)):
651
+ request_kwargs["headers"] = {**request_model.headers, "Content-Type": "application/msgpack"}
652
+ body_content_type = "application/msgpack"
653
+
636
654
  case GraphQLBody(graphql=gql):
637
655
  request_kwargs["json"] = {"query": gql.query, "variables": gql.variables}
638
656
 
@@ -651,6 +669,11 @@ def build_request_kwargs(
651
669
  path = resolve_scenario_path(scenario_dir, file_path)
652
670
  request_kwargs["content"] = _read_file(path, file_path, "Binary file not found", "Cannot read binary file")
653
671
 
672
+ case BytesBody(bytes=data):
673
+ if not isinstance(data, bytes):
674
+ raise RequestError("The bytes body must resolve to bytes")
675
+ request_kwargs["content"] = data
676
+
654
677
  case FilesBody(files=files):
655
678
  multipart_parts = list(_file_parts(files, scenario_dir))
656
679
 
@@ -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
- def process_save(save_model: Save, response: httpx.Response, context: ChainMap[str, Any]) -> dict[str, Any]:
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 = _response_json(response, SaveError, "extract variables")
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 (`_JsonBody`).
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 = _JsonBody(response, failures)
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 `_JsonBody` no check has read yet: None is a body, JSON's null.
580
+ # A `_StructuredBody` no check has read yet: None is a body, JSON's null.
558
581
  _UNPARSED = object()
559
- # A `_JsonBody` json could not read, which the step has a failure for.
582
+ # A `_StructuredBody` could not decode, which the step has a failure for.
560
583
  _UNPARSABLE = object()
561
584
 
562
585
 
563
- class _JsonBody:
564
- """A verify step's response body as JSON, parsed by the first check that
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 json cannot read is one failure of the step, recorded by the check
568
- that asked first; the checks after it that need the body do not run. That
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.json()
589
- except ValueError as e:
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(f"Cannot {check}, response JSON is nested too deeply to parse: {e}", cause=e)
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 _verify_body_schema(schema: Any, body: _JsonBody, scenario_dir: Path | None, ref_bounds: ReferenceBounds, failures: _Failures) -> None:
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 _response_json(response: httpx.Response, error: type[StageExecutionError], purpose: str) -> Any:
877
- """The body parsed as JSON, or ``error`` naming what it was needed for, in
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.json()
881
- except ValueError as e:
882
- raise error(f"Cannot {purpose}, response is not valid JSON: {e}") from e
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:
@@ -127,20 +127,49 @@ class _TemplateNames(NamedTuple):
127
127
  self.to_methods.update(other.to_methods)
128
128
 
129
129
 
130
- def _receiver(node: ast.expr) -> str | None:
131
- """The name a method's object is reached from (``helper`` for
130
+ def _receiver(node: ast.expr) -> ast.Name | None:
131
+ """The name node a method's object is reached from (``helper`` for
132
132
  ``helper.api.ids``, ``helper().ids``, ``helper['x'].ids``), or None for an
133
133
  object written in place (``', '.join``)."""
134
134
  while True:
135
135
  match node:
136
136
  case ast.Attribute(value=inner) | ast.Subscript(value=inner) | ast.Call(func=inner):
137
137
  node = inner
138
- case ast.Name(id=name):
139
- return name
138
+ case ast.Name():
139
+ return node
140
140
  case _:
141
141
  return None
142
142
 
143
143
 
144
+ def _local_comprehension_reads(tree: ast.expr) -> set[int]:
145
+ """The name nodes read from comprehension targets, rather than context.
146
+
147
+ A generator's iterable is evaluated before its target is bound. The
148
+ target is visible in its filters, later generators and result, but not in
149
+ an expression outside the comprehension. Keep this walk iterative as the
150
+ other expression walks are: valid expressions can be deeply nested.
151
+ """
152
+ local_reads: set[int] = set()
153
+ pending: list[tuple[ast.AST, frozenset[str]]] = [(tree, frozenset())]
154
+ while pending:
155
+ node, bound = pending.pop()
156
+ if isinstance(node, ast.Name):
157
+ if isinstance(node.ctx, ast.Load) and node.id in bound:
158
+ local_reads.add(id(node))
159
+ elif isinstance(node, ast.ListComp | ast.GeneratorExp | ast.DictComp):
160
+ for generator in node.generators:
161
+ pending.append((generator.iter, bound))
162
+ bound |= frozenset(name.id for name in ast.walk(generator.target) if isinstance(name, ast.Name))
163
+ pending.extend((condition, bound) for condition in generator.ifs)
164
+ if isinstance(node, ast.DictComp):
165
+ pending.extend(((node.key, bound), (node.value, bound)))
166
+ else:
167
+ pending.append((node.elt, bound))
168
+ else:
169
+ pending.extend((child, bound) for child in ast.iter_child_nodes(node))
170
+ return local_reads
171
+
172
+
144
173
  def _extract_names_from_expr(expr: str) -> _TemplateNames:
145
174
  """Free identifiers a Python expression reads, and those it calls.
146
175
 
@@ -170,16 +199,16 @@ def _extract_names_from_expr(expr: str) -> _TemplateNames:
170
199
  except TemplatesError:
171
200
  return names
172
201
 
173
- bound: set[str] = set()
174
- callees: set[int] = set()
202
+ local_reads = _local_comprehension_reads(tree)
203
+ # simpleeval calls a bare name through its function table, even where a
204
+ # comprehension target of that name is bound as a value.
205
+ callees = {id(node.func) for node in ast.walk(tree) if isinstance(node, ast.Call) and isinstance(node.func, ast.Name)}
175
206
  # What each argument that may be a function is handed to, by node.
176
207
  keys: set[int] = set()
177
208
  to_functions: set[int] = set()
178
209
  to_methods: dict[int, str] = {}
179
210
  for node in ast.walk(tree):
180
- if isinstance(node, ast.comprehension):
181
- bound |= {n.id for n in ast.walk(node.target) if isinstance(n, ast.Name)}
182
- elif isinstance(node, ast.Call):
211
+ if isinstance(node, ast.Call):
183
212
  key = [keyword.value for keyword in node.keywords if keyword.arg == "key"]
184
213
  others = [*node.args, *(keyword.value for keyword in node.keywords if keyword.arg != "key")]
185
214
  match node.func:
@@ -189,22 +218,22 @@ def _extract_names_from_expr(expr: str) -> _TemplateNames:
189
218
  keys.update(id(argument) for argument in key)
190
219
  case ast.Attribute(value=value):
191
220
  keys.update(id(argument) for argument in key)
192
- if (receiver := _receiver(value)) is not None:
193
- to_methods.update(dict.fromkeys(map(id, others), receiver))
194
- if isinstance(node.func, ast.Name):
195
- callees.add(id(node.func))
221
+ if (receiver := _receiver(value)) is not None and (id(receiver) not in local_reads or id(receiver) in callees):
222
+ to_methods.update(dict.fromkeys(map(id, others), receiver.id))
196
223
 
197
224
  for node in ast.walk(tree):
198
- if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Load) and node.id not in bound:
225
+ if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Load):
199
226
  if id(node) in callees:
200
227
  names.called.add(node.id)
201
228
  continue
229
+ if id(node) in local_reads:
230
+ continue
202
231
  names.read.add(node.id)
203
232
  if id(node) in keys:
204
233
  names.keys.add(node.id)
205
234
  elif id(node) in to_functions:
206
235
  names.to_functions.add(node.id)
207
- elif (receiver := to_methods.get(id(node))) is not None and receiver not in bound:
236
+ elif (receiver := to_methods.get(id(node))) is not None:
208
237
  names.to_methods.add((receiver, node.id))
209
238
  else:
210
239
  names.loose.add(node.id)
@@ -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
- urlsafe = _require_bool("b64decode", "urlsafe", urlsafe)
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,
@@ -133,7 +133,7 @@ class _KeyDoesNotExist(InvalidExpression):
133
133
 
134
134
  def __init__(self, key: object, expression: str):
135
135
  self.message = f"Key {key!r} does not exist in expression '{expression}'"
136
- super(InvalidExpression, self).__init__(self.message)
136
+ super().__init__(self.message)
137
137
 
138
138
 
139
139
  class _MethodNotCalled(AttributeDoesNotExist):
@@ -462,11 +462,20 @@ def _any_string(obj: Any, predicate: Callable[[str], bool]) -> bool:
462
462
  """True when ``predicate`` holds for any string anywhere in the structure.
463
463
 
464
464
  Iterative on purpose: a recursive walk spent two stack frames per level of
465
- nesting, so a value a few hundred levels deep overflowed it.
465
+ nesting, so a value a few hundred levels deep overflowed it. Track objects
466
+ already visited so a fixture's cyclic container cannot loop forever.
466
467
  """
467
468
  pending = [obj]
469
+ # Keep the objects alive while scanning: a set of IDs alone could confuse
470
+ # a newly dumped model with an earlier object whose ID was reused.
471
+ seen: dict[int, Any] = {}
468
472
  while pending:
469
- match pending.pop():
473
+ current = pending.pop()
474
+ if isinstance(current, dict | list | tuple | BaseModel | SimpleNamespace):
475
+ if id(current) in seen:
476
+ continue
477
+ seen[id(current)] = current
478
+ match current:
470
479
  case str() as text:
471
480
  if predicate(text):
472
481
  return True
@@ -46,10 +46,10 @@ def import_function(name: str, *, quoted: bool = True) -> Callable[..., Any]:
46
46
  raise UserFunctionError(f"importing its module raised {type(e).__name__}") from None
47
47
  raise UserFunctionError(f"Failed to import module '{module_path}': {e}") from e
48
48
 
49
- if not hasattr(module, function_name):
50
- raise UserFunctionError(f"Function '{function_name}' not found in module '{module_path}'" if quoted else "its module has no function of that name")
51
-
52
- func = getattr(module, function_name)
49
+ try:
50
+ func = getattr(module, function_name)
51
+ except AttributeError:
52
+ raise UserFunctionError(f"Function '{function_name}' not found in module '{module_path}'" if quoted else "its module has no function of that name") from None
53
53
  if not callable(func):
54
54
  raise UserFunctionError(f"'{module_path}:{function_name}' is not callable" if quoted else "what it names is not callable")
55
55
 
@@ -186,12 +186,16 @@ def _signature_problems(func: Any, provided: set[str]) -> Iterator[tuple[Diagnos
186
186
 
187
187
  params = list(signature.parameters.values())
188
188
  keyword_acceptable = {p.name for p in params if p.kind in (p.POSITIONAL_OR_KEYWORD, p.KEYWORD_ONLY)}
189
- required = {p.name for p in params if p.default is p.empty and p.kind in (p.POSITIONAL_OR_KEYWORD, p.KEYWORD_ONLY, p.POSITIONAL_ONLY)}
189
+ required_keywords = {p.name for p in params if p.default is p.empty and p.kind in (p.POSITIONAL_OR_KEYWORD, p.KEYWORD_ONLY)}
190
+ required_positionals = {p.name for p in params if p.default is p.empty and p.kind is p.POSITIONAL_ONLY}
190
191
 
191
192
  if not any(p.kind is p.VAR_KEYWORD for p in params):
192
193
  for name in sorted(provided - keyword_acceptable):
193
194
  yield DiagnosticCode.UNKNOWN_ARG, f"unexpected argument '{name}'"
194
- for name in sorted(required - provided):
195
+ # A keyword with the same name cannot fill a positional-only parameter.
196
+ # With **kwargs it is accepted into kwargs, but the positional argument is
197
+ # still missing when the function is called.
198
+ for name in sorted(required_positionals | (required_keywords - provided)):
195
199
  yield DiagnosticCode.MISSING_ARG, f"missing required argument '{name}'"
196
200
 
197
201
 
@@ -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")