Parcourir la source

Add test cases for shlink

Karthik Kasturi il y a 11 mois
Parent
commit
39e527915e
1 fichiers modifiés avec 80 ajouts et 0 suppressions
  1. 80 0
      tst/JsonApiTest.php

+ 80 - 0
tst/JsonApiTest.php

@@ -313,6 +313,60 @@ class JsonApiTest extends TestCase
         $this->assertStringContainsString('id="pasteurl" href="https://example.com/1"', $content, "'{$baseUri}' outputs shortened URL correctly");
     }
 
+    /**
+     * @runInSeparateProcess
+     * @dataProvider baseShlinkUriProvider
+     */
+    public function testShortenViaShlink($baseUri)
+    {
+        $mock_shlink_service                = $this->_path . DIRECTORY_SEPARATOR . 'shlink.json';
+        $options                            = parse_ini_file(CONF, true);
+        $options['main']['basepath']        = 'https://example.com/path'; // missing slash gets added by Configuration constructor
+        $options['main']['urlshortener']    = 'https://example.com' . $baseUri . 'link=';
+        $options['shlink']['apiurl']        = $mock_shlink_service;
+        Helper::createIniFile(CONF, $options);
+
+        // the real service answer is more complex, but we only look for the shorturl & statusCode
+        file_put_contents($mock_shlink_service, '{"shortUrl":"https:\/\/example.com\/1"}');
+
+        $_SERVER['REQUEST_URI'] = $baseUri . 'link=https%3A%2F%2Fexample.com%2Fpath%2F%3Ffoo%23bar';
+        $_GET['link']           = 'https://example.com/path/?foo#bar';
+        if (str_contains($baseUri, '?shortenviashlink')) {
+            $_GET['shortenviashlink'] = null;
+        }
+        ob_start();
+        new Controller;
+        $content = ob_get_contents();
+        ob_end_clean();
+        $this->assertStringContainsString('id="pasteurl" href="https://example.com/1"', $content, "'{$baseUri}' outputs shortened URL correctly");
+    }
+
+    /**
+     * @runInSeparateProcess
+     * @dataProvider baseShlinkUriProvider
+     */
+    public function testShortenViaShlinkFailureHttp($baseUri)
+    {
+        $mock_shlink_service                = 'https://httpbin.org/status/403';
+        $options                            = parse_ini_file(CONF, true);
+        $options['main']['basepath']        = 'https://example.com/path'; // missing slash gets added by Configuration constructor
+        $options['main']['urlshortener']    = 'https://example.com' . $baseUri . 'link=';
+        $options['shlink']['apiurl']        = $mock_shlink_service;
+        Helper::createIniFile(CONF, $options);
+
+        $_SERVER['REQUEST_URI'] = $baseUri . 'link=https%3A%2F%2Fexample.com%2Fpath%2F%3Ffoo%23bar';
+        $_GET['link']           = 'https://example.com/path/?foo#bar';
+        if (str_contains($baseUri, '?shortenviashlink')) {
+            $_GET['shortenviashlink'] = null;
+        }
+        ob_start();
+        // Use @ to ignore the http warning for the 403. It will be handled appropriately by AbstractProxy
+        @new Controller;
+        $content = ob_get_contents();
+        ob_end_clean();
+        $this->assertStringContainsString('Error calling proxy.', $content, 'outputs error correctly');
+    }
+
     public function baseUriProvider()
     {
         return array(
@@ -322,6 +376,15 @@ class JsonApiTest extends TestCase
         );
     }
 
+    public function baseShlinkUriProvider()
+    {
+        return array(
+            array('/path/shortenviashlink?'),
+            array('/path/index.php/shortenviashlink?'),
+            array('/path?shortenviashlink&'),
+        );
+    }
+
     /**
      * @runInSeparateProcess
      */
@@ -338,4 +401,21 @@ class JsonApiTest extends TestCase
         ob_end_clean();
         $this->assertStringContainsString('Error calling proxy.', $content, 'outputs error correctly');
     }
+
+    /**
+     * @runInSeparateProcess
+     */
+    public function testShortenViaShlinkFailure()
+    {
+        $options                            = parse_ini_file(CONF, true);
+        $options['main']['basepath']        = 'https://example.com/path'; // missing slash gets added by Configuration constructor
+        Helper::createIniFile(CONF, $options);
+        $_SERVER['REQUEST_URI'] = '/path/shortenviashlink?link=https%3A%2F%2Fexample.com%2Fpath%2F%3Ffoo%23bar';
+        $_GET['link']           = 'https://example.com/path/?foo#bar';
+        ob_start();
+        new Controller;
+        $content = ob_get_contents();
+        ob_end_clean();
+        $this->assertStringContainsString('Error calling proxy.', $content, 'outputs error correctly');
+    }
 }