mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Add API test for redirect rules list
This commit is contained in:
parent
c4805b8152
commit
67bafbe44e
104
module/Rest/test-api/Action/ListRedirectRulesTest.php
Normal file
104
module/Rest/test-api/Action/ListRedirectRulesTest.php
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\Attributes\Test;
|
||||||
|
use PHPUnit\Framework\Attributes\TestWith;
|
||||||
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
||||||
|
|
||||||
|
use function sprintf;
|
||||||
|
|
||||||
|
class ListRedirectRulesTest extends ApiTestCase
|
||||||
|
{
|
||||||
|
private const LANGUAGE_EN_CONDITION = [
|
||||||
|
'name' => 'language-en',
|
||||||
|
'type' => 'language',
|
||||||
|
'matchKey' => null,
|
||||||
|
'matchValue' => 'en',
|
||||||
|
];
|
||||||
|
private const QUERY_FOO_BAR_CONDITION = [
|
||||||
|
'name' => 'query-foo-bar',
|
||||||
|
'type' => 'query',
|
||||||
|
'matchKey' => 'foo',
|
||||||
|
'matchValue' => 'bar',
|
||||||
|
];
|
||||||
|
|
||||||
|
#[Test]
|
||||||
|
public function errorIsReturnedWhenInvalidUrlIsFetched(): void
|
||||||
|
{
|
||||||
|
$response = $this->callApiWithKey(self::METHOD_GET, '/short-urls/invalid/redirect-rules');
|
||||||
|
$payload = $this->getJsonResponsePayload($response);
|
||||||
|
|
||||||
|
self::assertEquals(404, $response->getStatusCode());
|
||||||
|
self::assertEquals(404, $payload['status']);
|
||||||
|
self::assertEquals('invalid', $payload['shortCode']);
|
||||||
|
self::assertEquals('No URL found with short code "invalid"', $payload['detail']);
|
||||||
|
self::assertEquals('Short URL not found', $payload['title']);
|
||||||
|
self::assertEquals('https://shlink.io/api/error/short-url-not-found', $payload['type']);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Test]
|
||||||
|
#[TestWith(['abc123', []])]
|
||||||
|
#[TestWith(['def456', [
|
||||||
|
[
|
||||||
|
'longUrl' => 'https://example.com/english-and-foo-query',
|
||||||
|
'priority' => 1,
|
||||||
|
'conditions' => [
|
||||||
|
self::LANGUAGE_EN_CONDITION,
|
||||||
|
self::QUERY_FOO_BAR_CONDITION,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'longUrl' => 'https://example.com/multiple-query-params',
|
||||||
|
'priority' => 2,
|
||||||
|
'conditions' => [
|
||||||
|
self::QUERY_FOO_BAR_CONDITION,
|
||||||
|
[
|
||||||
|
'name' => 'query-hello-world',
|
||||||
|
'type' => 'query',
|
||||||
|
'matchKey' => 'hello',
|
||||||
|
'matchValue' => 'world',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'longUrl' => 'https://example.com/only-english',
|
||||||
|
'priority' => 3,
|
||||||
|
'conditions' => [self::LANGUAGE_EN_CONDITION],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'longUrl' => 'https://blog.alejandrocelaya.com/android',
|
||||||
|
'priority' => 4,
|
||||||
|
'conditions' => [
|
||||||
|
[
|
||||||
|
'name' => 'device-android',
|
||||||
|
'type' => 'device',
|
||||||
|
'matchKey' => null,
|
||||||
|
'matchValue' => 'android',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'longUrl' => 'https://blog.alejandrocelaya.com/ios',
|
||||||
|
'priority' => 5,
|
||||||
|
'conditions' => [
|
||||||
|
[
|
||||||
|
'name' => 'device-ios',
|
||||||
|
'type' => 'device',
|
||||||
|
'matchKey' => null,
|
||||||
|
'matchValue' => 'ios',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]])]
|
||||||
|
public function returnsListOfRulesForShortUrl(string $shortCode, array $expectedRules): void
|
||||||
|
{
|
||||||
|
$response = $this->callApiWithKey(self::METHOD_GET, sprintf('/short-urls/%s/redirect-rules', $shortCode));
|
||||||
|
$payload = $this->getJsonResponsePayload($response);
|
||||||
|
|
||||||
|
self::assertEquals(200, $response->getStatusCode());
|
||||||
|
self::assertEquals($expectedRules, $payload['redirectRules']);
|
||||||
|
}
|
||||||
|
}
|
@ -40,43 +40,44 @@ class ShortUrlRedirectRulesFixture extends AbstractFixture implements DependentF
|
|||||||
$iosCondition = RedirectCondition::forDevice(DeviceType::IOS);
|
$iosCondition = RedirectCondition::forDevice(DeviceType::IOS);
|
||||||
$manager->persist($iosCondition);
|
$manager->persist($iosCondition);
|
||||||
|
|
||||||
$englishAndFooQueryRule = new ShortUrlRedirectRule(
|
// Create rules disordered to make sure the order by priority works
|
||||||
$defShortUrl,
|
|
||||||
1,
|
|
||||||
'https://example.com/english-and-foo-query',
|
|
||||||
new ArrayCollection([$englishCondition, $fooQueryCondition]),
|
|
||||||
);
|
|
||||||
$manager->persist($englishAndFooQueryRule);
|
|
||||||
|
|
||||||
$multipleQueryParamsRule = new ShortUrlRedirectRule(
|
$multipleQueryParamsRule = new ShortUrlRedirectRule(
|
||||||
$defShortUrl,
|
shortUrl: $defShortUrl,
|
||||||
2,
|
priority: 2,
|
||||||
'https://example.com/multiple-query-params',
|
longUrl: 'https://example.com/multiple-query-params',
|
||||||
new ArrayCollection([$helloQueryCondition, $fooQueryCondition]),
|
conditions: new ArrayCollection([$helloQueryCondition, $fooQueryCondition]),
|
||||||
);
|
);
|
||||||
$manager->persist($multipleQueryParamsRule);
|
$manager->persist($multipleQueryParamsRule);
|
||||||
|
|
||||||
$onlyEnglishRule = new ShortUrlRedirectRule(
|
$englishAndFooQueryRule = new ShortUrlRedirectRule(
|
||||||
$defShortUrl,
|
shortUrl: $defShortUrl,
|
||||||
3,
|
priority: 1,
|
||||||
'https://example.com/only-english',
|
longUrl: 'https://example.com/english-and-foo-query',
|
||||||
new ArrayCollection([$englishCondition]),
|
conditions: new ArrayCollection([$englishCondition, $fooQueryCondition]),
|
||||||
);
|
);
|
||||||
$manager->persist($onlyEnglishRule);
|
$manager->persist($englishAndFooQueryRule);
|
||||||
|
|
||||||
$androidRule = new ShortUrlRedirectRule(
|
$androidRule = new ShortUrlRedirectRule(
|
||||||
$defShortUrl,
|
shortUrl: $defShortUrl,
|
||||||
4,
|
priority: 4,
|
||||||
'https://blog.alejandrocelaya.com/android',
|
longUrl: 'https://blog.alejandrocelaya.com/android',
|
||||||
new ArrayCollection([$androidCondition]),
|
conditions: new ArrayCollection([$androidCondition]),
|
||||||
);
|
);
|
||||||
$manager->persist($androidRule);
|
$manager->persist($androidRule);
|
||||||
|
|
||||||
|
$onlyEnglishRule = new ShortUrlRedirectRule(
|
||||||
|
shortUrl: $defShortUrl,
|
||||||
|
priority: 3,
|
||||||
|
longUrl: 'https://example.com/only-english',
|
||||||
|
conditions: new ArrayCollection([$englishCondition]),
|
||||||
|
);
|
||||||
|
$manager->persist($onlyEnglishRule);
|
||||||
|
|
||||||
$iosRule = new ShortUrlRedirectRule(
|
$iosRule = new ShortUrlRedirectRule(
|
||||||
$defShortUrl,
|
shortUrl: $defShortUrl,
|
||||||
5,
|
priority: 5,
|
||||||
'https://blog.alejandrocelaya.com/ios',
|
longUrl: 'https://blog.alejandrocelaya.com/ios',
|
||||||
new ArrayCollection([$iosCondition]),
|
conditions: new ArrayCollection([$iosCondition]),
|
||||||
);
|
);
|
||||||
$manager->persist($iosRule);
|
$manager->persist($iosRule);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user