|
| 1 | +import json |
1 | 2 | import pytest |
2 | 3 | from pydantic import ValidationError |
3 | 4 |
|
@@ -246,3 +247,39 @@ def test_parse_response_body_list() -> None: |
246 | 247 | ) |
247 | 248 | result = spec.parse_response_body('{"foo": [{"baz": 1}, {"baz": 2}]}') |
248 | 249 | assert result == "[1, 2]" |
| 250 | + |
| 251 | + |
| 252 | +def test_parse_single_value_response_body() -> None: |
| 253 | + spec = HTTPSpec( |
| 254 | + request={ |
| 255 | + "url": "https://api.example.com/v1/chat", |
| 256 | + "method": "POST", |
| 257 | + "headers": {"Authorization": "Bearer {{api_key}}"}, |
| 258 | + "transforms": [{"type": "json", "pattern": {"model": "$model", "messages": "$messages"}}], |
| 259 | + }, |
| 260 | + response={ |
| 261 | + "valid_status_codes": [200, 201], |
| 262 | + "transforms": [{"type": "jsonpath", "pattern": "$.foo"}], |
| 263 | + }, |
| 264 | + ) |
| 265 | + result = spec.parse_response_body('{"foo": 1, "bar": 2}') |
| 266 | + assert result == "1" |
| 267 | + |
| 268 | + |
| 269 | + |
| 270 | +def test_jsonpath_transform_into_json() -> None: |
| 271 | + spec = HTTPSpec( |
| 272 | + request={ |
| 273 | + "url": "https://api.example.com/v1/chat", |
| 274 | + "method": "POST", |
| 275 | + "headers": {"Authorization": "Bearer {{api_key}}"}, |
| 276 | + "transforms": [{"type": "json", "pattern": {"model": "$model", "messages": "$messages"}}], |
| 277 | + }, |
| 278 | + response={ |
| 279 | + "valid_status_codes": [200, 201], |
| 280 | + "transforms": [{"type": "jsonpath", "pattern": "$"}], |
| 281 | + }, |
| 282 | + ) |
| 283 | + result = spec.parse_response_body('{"foo": [{"baz": 1}, {"baz": 2}]}') |
| 284 | + assert result |
| 285 | + assert json.loads(result) |
0 commit comments