From 52dfe92054b7795212e18110fea998f794ac4f2e Mon Sep 17 00:00:00 2001 From: kwlo Date: Sat, 1 Nov 2025 12:59:59 -0400 Subject: [PATCH 1/2] Allow plain text response for plugin data polling --- app/Models/Plugin.php | 10 ++++++++-- tests/Feature/PluginXmlResponseTest.php | 24 +++++++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/app/Models/Plugin.php b/app/Models/Plugin.php index 3c279d7..d21f498 100644 --- a/app/Models/Plugin.php +++ b/app/Models/Plugin.php @@ -235,9 +235,15 @@ class Plugin extends Model } } - // Default to JSON parsing try { - return $httpResponse->json() ?? []; + // Attempt to parse it into JSON + $json = $httpResponse->json(); + if($json !== null) { + return $json; + } + + // Response doesn't seem to be JSON, wrap the response body text as a JSON object + return ['text' => $httpResponse->body()]; } catch (Exception $e) { Log::warning('Failed to parse JSON response: '.$e->getMessage()); diff --git a/tests/Feature/PluginXmlResponseTest.php b/tests/Feature/PluginXmlResponseTest.php index 308d914..9717d8d 100644 --- a/tests/Feature/PluginXmlResponseTest.php +++ b/tests/Feature/PluginXmlResponseTest.php @@ -72,7 +72,7 @@ test('plugin parses XML responses and wraps under rss key', function (): void { expect($plugin->data_payload['rss']['channel']['item'])->toHaveCount(2); }); -test('plugin handles non-XML content-type as JSON', function (): void { +test('plugin parses JSON-parsable response body as JSON', function (): void { $jsonContent = '{"title": "Test Data", "items": [1, 2, 3]}'; Http::fake([ @@ -95,6 +95,28 @@ test('plugin handles non-XML content-type as JSON', function (): void { ]); }); +test('plugin wraps plain text response body as JSON', function (): void { + $jsonContent = 'Lorem ipsum dolor sit amet'; + + Http::fake([ + 'example.com/data' => Http::response($jsonContent, 200, ['Content-Type' => 'text/plain']), + ]); + + $plugin = Plugin::factory()->create([ + 'data_strategy' => 'polling', + 'polling_url' => 'https://example.com/data', + 'polling_verb' => 'get', + ]); + + $plugin->updateDataPayload(); + + $plugin->refresh(); + + expect($plugin->data_payload)->toBe([ + 'text' => 'Lorem ipsum dolor sit amet', + ]); +}); + test('plugin handles invalid XML gracefully', function (): void { $invalidXml = 'unclosed tag'; From 10b53c377251df080fe5ba131f79cc9a6764cb35 Mon Sep 17 00:00:00 2001 From: kwlo Date: Mon, 3 Nov 2025 21:58:36 -0500 Subject: [PATCH 2/2] Wrapping text in json object with 'data' as key --- app/Models/Plugin.php | 2 +- tests/Feature/PluginXmlResponseTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Models/Plugin.php b/app/Models/Plugin.php index d21f498..33a29d5 100644 --- a/app/Models/Plugin.php +++ b/app/Models/Plugin.php @@ -243,7 +243,7 @@ class Plugin extends Model } // Response doesn't seem to be JSON, wrap the response body text as a JSON object - return ['text' => $httpResponse->body()]; + return ['data' => $httpResponse->body()]; } catch (Exception $e) { Log::warning('Failed to parse JSON response: '.$e->getMessage()); diff --git a/tests/Feature/PluginXmlResponseTest.php b/tests/Feature/PluginXmlResponseTest.php index 9717d8d..5811089 100644 --- a/tests/Feature/PluginXmlResponseTest.php +++ b/tests/Feature/PluginXmlResponseTest.php @@ -113,7 +113,7 @@ test('plugin wraps plain text response body as JSON', function (): void { $plugin->refresh(); expect($plugin->data_payload)->toBe([ - 'text' => 'Lorem ipsum dolor sit amet', + 'data' => 'Lorem ipsum dolor sit amet', ]); });