mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-13 23:18:10 +00:00
Compare commits
2 commits
882cbff7fe
...
10b53c3772
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
10b53c3772 | ||
|
|
52dfe92054 |
2 changed files with 31 additions and 3 deletions
|
|
@ -235,9 +235,15 @@ class Plugin extends Model
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default to JSON parsing
|
|
||||||
try {
|
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 ['data' => $httpResponse->body()];
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Log::warning('Failed to parse JSON response: '.$e->getMessage());
|
Log::warning('Failed to parse JSON response: '.$e->getMessage());
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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);
|
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]}';
|
$jsonContent = '{"title": "Test Data", "items": [1, 2, 3]}';
|
||||||
|
|
||||||
Http::fake([
|
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([
|
||||||
|
'data' => 'Lorem ipsum dolor sit amet',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
test('plugin handles invalid XML gracefully', function (): void {
|
test('plugin handles invalid XML gracefully', function (): void {
|
||||||
$invalidXml = '<root><item>unclosed tag';
|
$invalidXml = '<root><item>unclosed tag';
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue