Skip to content

Commit ce7d1cf

Browse files
authored
Merge pull request #123 from Khabib73/refactor/validator-extract-format
Refactor/validator extract format
2 parents 4e751fe + bbf6890 commit ce7d1cf

3 files changed

Lines changed: 31 additions & 35 deletions

File tree

smoke/google.yatl.yaml

Lines changed: 0 additions & 23 deletions
This file was deleted.

smoke/jsonplaceholder.yatl.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
base_url: https://jsonplaceholder.typicode.com
2+
3+
steps:
4+
5+
- name: create_post
6+
request:
7+
method: POST
8+
url: /posts
9+
timeout: 5
10+
body:
11+
json:
12+
title: YATL - api testing in a new way
13+
body: Test body
14+
userId: 1
15+
expect:
16+
status: 201
17+
body:
18+
json:
19+
id: 101
20+
title: YATL - api testing in a new way
21+
body: Test body
22+
userId: 1

src/yatl/validator/base.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,32 +140,30 @@ def _get_body_validator(self, content_type: str) -> Callable | None:
140140
except ValueError:
141141
return None
142142

143-
def _extract_format_and_spec(
144-
self, body_spec: Any, content_type: str
145-
) -> tuple[ContentFormat, Any]:
143+
def _extract_format_and_spec(self, body_spec: Any) -> tuple[ContentFormat, Any]:
146144
"""
147145
Extracts the format and spec from the body spec.
148146
149147
Args:
150148
body_spec: The body spec to extract from.
151-
content_type: The content-type string.
152149
153150
Returns:
154151
A tuple containing the format and the spec.
155152
156153
Raises:
157-
AssertionError: If the content-type is not supported.
154+
AssertionError: If the body_spec is not a dict with one of
155+
{ContentFormat} keys (json, xml, or text).
156+
158157
"""
159158
if isinstance(body_spec, dict):
160159
for fmt in ContentFormat:
161160
if fmt in body_spec:
162161
return fmt, body_spec[fmt]
163162

164-
try:
165-
fmt = ContentFormat.from_mime_type(content_type)
166-
return fmt, body_spec
167-
except ValueError:
168-
raise AssertionError(f"Unsupported content-type: {content_type}")
163+
raise AssertionError(
164+
f"Body spec must be a dict with one of {list(ContentFormat)} keys, "
165+
f"got: {type(body_spec).__name__}"
166+
)
169167

170168
def _validate_body(self, body_format: ContentFormat, body_spec: Any) -> None:
171169
"""
@@ -195,6 +193,5 @@ def check_expectations(self) -> None:
195193
if body_spec is None:
196194
return
197195

198-
content_type = get_content_type(self.response.headers.get("Content-Type", ""))
199-
body_format, body_spec = self._extract_format_and_spec(body_spec, content_type)
196+
body_format, body_spec = self._extract_format_and_spec(body_spec)
200197
self._validate_body(body_format, body_spec)

0 commit comments

Comments
 (0)