From 51c210e1a69348f055dc5e64a41d675ce61a1554 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez=20Mondrag=C3=B3n?= Date: Wed, 13 Nov 2024 20:50:52 -0600 Subject: [PATCH] Add test --- tests/core/test_streams.py | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/core/test_streams.py b/tests/core/test_streams.py index 1a25defac..b9d432f2c 100644 --- a/tests/core/test_streams.py +++ b/tests/core/test_streams.py @@ -5,6 +5,7 @@ import datetime import logging import typing as t +import urllib.parse import pytest import requests @@ -24,6 +25,8 @@ from tests.core.conftest import SimpleTestStream if t.TYPE_CHECKING: + import requests_mock + from singer_sdk import Stream, Tap from tests.core.conftest import SimpleTestTap @@ -484,6 +487,47 @@ def calculate_test_cost( assert f"Total Sync costs for stream {stream.name}" in record.message +def test_non_json_payload(tap: Tap, requests_mock: requests_mock.Mocker): + """Test non-JSON payload is handled correctly.""" + + def callback(request: requests.PreparedRequest, context: requests_mock.Context): # noqa: ARG001 + assert request.headers["Content-Type"] == "application/x-www-form-urlencoded" + assert request.body == "my_key=my_value" + + data = urllib.parse.parse_qs(request.body) + + return { + "data": [ + {"id": 1, "value": f"{data['my_key'][0]}_1"}, + {"id": 2, "value": f"{data['my_key'][0]}_2"}, + ] + } + + class NonJsonStream(RestTestStream): + payload_as_json = False + http_method = "POST" + path = "/non-json" + records_jsonpath = "$.data[*]" + + def prepare_request_payload(self, context, next_page_token): # noqa: ARG002 + return {"my_key": "my_value"} + + stream = NonJsonStream(tap) + + requests_mock.post( + "https://example.com/non-json", + json=callback, + ) + + with pytest.deprecated_call(): + records = list(stream.request_records(None)) + + assert records == [ + {"id": 1, "value": "my_value_1"}, + {"id": 2, "value": "my_value_2"}, + ] + + @pytest.mark.parametrize( "input_catalog,selection", [