Allow for shortenviayourls in query params

This commit is contained in:
Aaron Sherber 2024-03-16 10:17:58 -04:00
parent 6eb5fdcc06
commit 7c9cc7754f
No known key found for this signature in database
GPG Key ID: AEB76A0BD39D7B28
2 changed files with 18 additions and 5 deletions

View File

@ -143,7 +143,7 @@ class Request
} elseif (array_key_exists('jsonld', $this->_params) && !empty($this->_params['jsonld'])) { } elseif (array_key_exists('jsonld', $this->_params) && !empty($this->_params['jsonld'])) {
$this->_operation = 'jsonld'; $this->_operation = 'jsonld';
} elseif (array_key_exists('link', $this->_params) && !empty($this->_params['link'])) { } elseif (array_key_exists('link', $this->_params) && !empty($this->_params['link'])) {
if (strpos($this->getRequestUri(), '/shortenviayourls') !== false) { if (strpos($this->getRequestUri(), '/shortenviayourls') !== false || array_key_exists('shortenviayourls', $this->_params)) {
$this->_operation = 'yourlsproxy'; $this->_operation = 'yourlsproxy';
} }
} }

View File

@ -285,26 +285,39 @@ class JsonApiTest extends TestCase
/** /**
* @runInSeparateProcess * @runInSeparateProcess
* @dataProvider baseUriProvider
*/ */
public function testShortenViaYourls() public function testShortenViaYourls($baseUri)
{ {
$mock_yourls_service = $this->_path . DIRECTORY_SEPARATOR . 'yourls.json'; $mock_yourls_service = $this->_path . DIRECTORY_SEPARATOR . 'yourls.json';
$options = parse_ini_file(CONF, true); $options = parse_ini_file(CONF, true);
$options['main']['basepath'] = 'https://example.com/path'; // missing slash gets added by Configuration constructor $options['main']['basepath'] = 'https://example.com/path'; // missing slash gets added by Configuration constructor
$options['main']['urlshortener'] = 'https://example.com/path/shortenviayourls?link='; $options['main']['urlshortener'] = 'https://example.com' . $baseUri . 'link=';
$options['yourls']['apiurl'] = $mock_yourls_service; $options['yourls']['apiurl'] = $mock_yourls_service;
Helper::createIniFile(CONF, $options); Helper::createIniFile(CONF, $options);
// the real service answer is more complex, but we only look for the shorturl & statusCode // the real service answer is more complex, but we only look for the shorturl & statusCode
file_put_contents($mock_yourls_service, '{"shorturl":"https:\/\/example.com\/1","statusCode":200}'); file_put_contents($mock_yourls_service, '{"shorturl":"https:\/\/example.com\/1","statusCode":200}');
$_SERVER['REQUEST_URI'] = '/path/shortenviayourls?link=https%3A%2F%2Fexample.com%2Fpath%2F%3Ffoo%23bar'; $_SERVER['REQUEST_URI'] = $baseUri . 'link=https%3A%2F%2Fexample.com%2Fpath%2F%3Ffoo%23bar';
$_GET['link'] = 'https://example.com/path/?foo#bar'; $_GET['link'] = 'https://example.com/path/?foo#bar';
if (strpos($baseUri, '?shortenviayourls') !== false) {
$_GET['shortenviayourls'] = null;
}
ob_start(); ob_start();
new Controller; new Controller;
$content = ob_get_contents(); $content = ob_get_contents();
ob_end_clean(); ob_end_clean();
$this->assertStringContainsString('id="pasteurl" href="https://example.com/1"', $content, 'outputs shortened URL correctly'); $this->assertStringContainsString('id="pasteurl" href="https://example.com/1"', $content, "'{$baseUri}' outputs shortened URL correctly");
}
public function baseUriProvider()
{
return array(
array('/path/shortenviayourls?'),
array('/path/index.php/shortenviayourls?'),
array('/path?shortenviayourls&'),
);
} }
/** /**