Throw exception if client sends invalid request data
This commit is contained in:
parent
056b12a667
commit
bac196e0b5
@ -3,19 +3,22 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace OCA\GPodderSync\Core\EpisodeAction;
|
namespace OCA\GPodderSync\Core\EpisodeAction;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
class EpisodeActionReader {
|
class EpisodeActionReader {
|
||||||
private array $requiredProperties = ['podcast', 'episode', 'action', 'timestamp'];
|
private array $requiredProperties = ['podcast', 'episode', 'action', 'timestamp'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $episodeActionsArray []
|
* @param array $episodeActionsArray []
|
||||||
* @return EpisodeAction[]
|
* @return EpisodeAction[]
|
||||||
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function fromArray(array $episodeActionsArray): array {
|
public function fromArray(array $episodeActionsArray): array {
|
||||||
$episodeActions = [];
|
$episodeActions = [];
|
||||||
|
|
||||||
foreach ($episodeActionsArray as $episodeAction) {
|
foreach ($episodeActionsArray as $episodeAction) {
|
||||||
if ($this->hasRequiredProperties($episodeAction) === false) {
|
if ($this->hasRequiredProperties($episodeAction) === false) {
|
||||||
continue;
|
throw new InvalidArgumentException(sprintf('Client sent incomplete or invalid data: %s', json_encode($episodeAction, JSON_THROW_ON_ERROR)));
|
||||||
}
|
}
|
||||||
$episodeActions[] = new EpisodeAction(
|
$episodeActions[] = new EpisodeAction(
|
||||||
$episodeAction["podcast"],
|
$episodeAction["podcast"],
|
||||||
|
@ -100,15 +100,6 @@ class EpisodeActionControllerTest extends TestCase
|
|||||||
"started": 15,
|
"started": 15,
|
||||||
"position": 120,
|
"position": 120,
|
||||||
"total": 500
|
"total": 500
|
||||||
},
|
|
||||||
{
|
|
||||||
"podcast": "https://example.org/podcast.php",
|
|
||||||
"episode": "https://example.com/files/foo-bar-123.mp3",
|
|
||||||
"guid": "foo-bar-123",
|
|
||||||
"timestamp": "2009-12-12T09:05:21",
|
|
||||||
"started": -1,
|
|
||||||
"position": -1,
|
|
||||||
"total": -1
|
|
||||||
}
|
}
|
||||||
]', true, 512, JSON_THROW_ON_ERROR);
|
]', true, 512, JSON_THROW_ON_ERROR);
|
||||||
$request = new Request([], new SecureRandom(), $this->getMockBuilder(IConfig::class)->getMock());
|
$request = new Request([], new SecureRandom(), $this->getMockBuilder(IConfig::class)->getMock());
|
||||||
|
@ -57,11 +57,12 @@ class EpisodeActionReaderTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testCreateWithFaultyData(): void {
|
public function testCreateWithFaultyData(): void {
|
||||||
$episodeActions = (new EpisodeActionReader())->fromArray([
|
$this->expectException(\InvalidArgumentException::class);
|
||||||
|
$this->expectExceptionMessage('Client sent incomplete or invalid data: {"podcast":"https:\/\/example.org\/feed.xml","action":"download","timestamp":"2021-10-03T12:03:17"}');
|
||||||
|
(new EpisodeActionReader())->fromArray([
|
||||||
["podcast" => "https://example.org/feed.xml", "action" => "download", "timestamp" => "2021-10-03T12:03:17"],
|
["podcast" => "https://example.org/feed.xml", "action" => "download", "timestamp" => "2021-10-03T12:03:17"],
|
||||||
["podcast" => "https://example.org/feed.xml", "episode" => "https://example.org/episode2.mp3", "guid" => "episode2", "action" => "download", "timestamp" => "2021-10-03T12:03:17"],
|
["podcast" => "https://example.org/feed.xml", "episode" => "https://example.org/episode2.mp3", "guid" => "episode2", "action" => "download", "timestamp" => "2021-10-03T12:03:17"],
|
||||||
]);
|
]);
|
||||||
$this->assertCount(1, $episodeActions);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user