From 00bf0c24b6cc4a4a1ee88e180eeae01386b414b0 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Thu, 11 Oct 2018 21:19:38 +0200 Subject: [PATCH 1/3] CurlResult Tests --- src/Network/CurlResult.php | 2 +- tests/datasets/curl/about.body | 189 +++++++++++++++++++++++++++ tests/datasets/curl/about.head | 21 +++ tests/src/Network/CurlResultTest.php | 30 +++++ 4 files changed, 241 insertions(+), 1 deletion(-) create mode 100644 tests/datasets/curl/about.body create mode 100644 tests/datasets/curl/about.head create mode 100644 tests/src/Network/CurlResultTest.php diff --git a/src/Network/CurlResult.php b/src/Network/CurlResult.php index dc83182a64..04f183538a 100644 --- a/src/Network/CurlResult.php +++ b/src/Network/CurlResult.php @@ -119,7 +119,7 @@ class CurlResult $header = ''; $base = $result; - while (preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/', $base)) { + while (preg_match('/^HTTP\/[1-2].+?[1-5][0-9][0-9]/', $base)) { $chunk = substr($base, 0, strpos($base, "\r\n\r\n") + 4); $header .= $chunk; $base = substr($base, strlen($chunk)); diff --git a/tests/datasets/curl/about.body b/tests/datasets/curl/about.body new file mode 100644 index 0000000000..b49bca8d97 --- /dev/null +++ b/tests/datasets/curl/about.body @@ -0,0 +1,189 @@ + + + + + + + + + + + + +mastodonten.de - Mastodonten.de + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+
+Mastodon +
+
+
+
+
+Mastodon +
+Sign up on another server +
+Ich habe die öffentliche Registrierung geschlossen, nachdem sich etwas über 1000 Nutzer bei mastodonten.de angemeldet hatten. Mastodonten.de ist ein Freizeit-Projekt. Würde es weiter wachsen, wäre es in der Freizeit, ohne Beeinträchtigungen meiner Familie, wegen des höheren Administrations- und Moderationsaufwands nicht mehr zu stemmen. Daher dieser Schritt. Jeder, der Nutzer von mastodonten.de kennt, kann sich aber von diesen einladen lassen. Ansonsten gibt es auch viele andere nette Instanzen. +
+
+ +I closed the public registration after a little over 1000 users had registered at mastodonten.de. Mastodonten.de is a leisure project. If it would grow further, it would be no more to stemmed in the spare time, without impairments of my family, because of the higher administration and moderation expenditure. Hence this step. Everybody who knows users of mastodonten.de can be invited by them. Otherwise there are also many other nice instances. +
+
+or +
+Log in + +
+
+
+
+Mastodonten.de +
+
+
+
+ +

+Mastodonten.de +Mastodon hosted on mastodonten.de +

+
+

mastodonten.de ist eine Mastodon Instanz offen für alle netten Leute (über 16). +Geht gut miteinander um (und lest die Regeln wenn ihr euch nicht sicher seid, was das heißt). Wer möchte, kann auch gerne etwas spenden, um den Betrieb von mastodonten.de zu unterstützen. + +
+
+mastodonten.de is a Mastodon instance open to nice people (over 16). +Be excellent to each other (and read the rules if you're not sure what that means). Who would like, can donate something, in order to support the operation of mastodonten.de. +

+
+
+
+
+
+
+Home to +1,034 +users +
+
+Who authored +82,821 +statuses +
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+
+

What is Mastodon?

+

Mastodon is a social network based on open web protocols and free, open-source software. It is decentralized like e-mail.

+
+ +
+
+
+
+
Built for real conversation
+With 500 characters at your disposal and support for granular content and media warnings, you can express yourself the way you want to. +
+
+ +
+
+
+
+
You’re a person, not a product
+Mastodon is not a commercial network. No advertising, no data mining, no walled gardens. There is no central authority. +
+
+ +
+
+
+
+
Always within reach
+Multiple apps for iOS, Android, and other platforms thanks to a developer-friendly API ecosystem allow you to keep up with your friends anywhere. +
+
+ +
+
+
+
+
A more humane approach
+Learning from failures of other networks, Mastodon aims to make ethical design choices to combat the misuse of social media. +
+
+ +
+
+
+ + +
+ +
+
+
+
+ + + + diff --git a/tests/datasets/curl/about.head b/tests/datasets/curl/about.head new file mode 100644 index 0000000000..c1d4b165d0 --- /dev/null +++ b/tests/datasets/curl/about.head @@ -0,0 +1,21 @@ +HTTP/2 200 +date: Thu, 11 Oct 2018 18:43:54 GMT +content-type: text/html; charset=utf-8 +vary: Accept-Encoding +server: Mastodon +x-frame-options: DENY +x-content-type-options: nosniff +x-xss-protection: 1; mode=block +vary: Accept-Encoding +etag: W/"706e6c48957e1d46ecf9d7597a7880af" +cache-control: max-age=0, private, must-revalidate +set-cookie: _mastodon_session=v3kcy%2FW3aZYBBvZUohuwksEKwzYIyEUlEuJ1KqTAfWPKvVQq%2F4UuJ39zp621VyfpQNlvY46TL%2FYutzXowSLYQBNFCJcrEiF04aU0TdtHls9zynMiyeHhoVgCijOXWXNt9%2FCmpQ49RkNEujkv9NaJ0cum32MCVZKjE9%2BMKmLM%2F8ZygZeLBGJ7sg%3D%3D--QGIiU0%2FpXc3Aym8F--he2iRRPePOdtEs3z%2BufSXg%3D%3D; path=/; secure; HttpOnly +x-request-id: a0c0b8e7-cd60-4efa-b79b-cf1b0d5a0784 +x-runtime: 0.049566 +strict-transport-security: max-age=31536000; includeSubDomains; preload +x-frame-options: SAMEORIGIN +x-content-type-options: nosniff +referrer-policy: same-origin +content-security-policy: frame-ancestors 'none'; script-src 'self'; object-src 'self'; img-src * data: blob:; media-src 'self' data:; font-src 'self' data: https://fonts.gstatic.com/; connect-src 'self' blob: wss://mastodonten.de +x-xss-protection: 1; mode=block + diff --git a/tests/src/Network/CurlResultTest.php b/tests/src/Network/CurlResultTest.php new file mode 100644 index 0000000000..79b950ca18 --- /dev/null +++ b/tests/src/Network/CurlResultTest.php @@ -0,0 +1,30 @@ + 200 ]); + + $this->assertTrue($curlResult->isSuccess()); + $this->assertSame($header, $curlResult->getHeader()); + $this->assertSame($body, $curlResult->getBody()); + } +} \ No newline at end of file From b099cba4b473686ae3e769b7e5c481b310c726e8 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Thu, 11 Oct 2018 22:18:27 +0200 Subject: [PATCH 2/3] CurlResult Tests & RegEx fix --- src/Network/CurlResult.php | 4 +- tests/datasets/curl/about.redirect | 22 +++++++ tests/src/Network/CurlResultTest.php | 95 +++++++++++++++++++++++++++- 3 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 tests/datasets/curl/about.redirect diff --git a/src/Network/CurlResult.php b/src/Network/CurlResult.php index 04f183538a..e246f4fa56 100644 --- a/src/Network/CurlResult.php +++ b/src/Network/CurlResult.php @@ -119,7 +119,7 @@ class CurlResult $header = ''; $base = $result; - while (preg_match('/^HTTP\/[1-2].+?[1-5][0-9][0-9]/', $base)) { + while (preg_match('/^HTTP\/.+? \d+/', $base)) { $chunk = substr($base, 0, strpos($base, "\r\n\r\n") + 4); $header .= $chunk; $base = substr($base, strlen($chunk)); @@ -177,7 +177,7 @@ class CurlResult $this->redirectUrl .= '?' . $old_location_query; } - $this->isRedirectUrl = filter_var($this->redirectUrl, FILTER_VALIDATE_URL); + $this->isRedirectUrl = filter_var($this->redirectUrl, FILTER_VALIDATE_URL) !== false; } else { $this->isRedirectUrl = false; } diff --git a/tests/datasets/curl/about.redirect b/tests/datasets/curl/about.redirect new file mode 100644 index 0000000000..9033e0f665 --- /dev/null +++ b/tests/datasets/curl/about.redirect @@ -0,0 +1,22 @@ +HTTP/2 301 +date: Thu, 11 Oct 2018 18:43:54 GMT +content-type: text/html; charset=utf-8 +vary: Accept-Encoding +server: Mastodon +Location: https://test.other/some/ +x-frame-options: DENY +x-content-type-options: nosniff +x-xss-protection: 1; mode=block +vary: Accept-Encoding +etag: W/"706e6c48957e1d46ecf9d7597a7880af" +cache-control: max-age=0, private, must-revalidate +set-cookie: _mastodon_session=v3kcy%2FW3aZYBBvZUohuwksEKwzYIyEUlEuJ1KqTAfWPKvVQq%2F4UuJ39zp621VyfpQNlvY46TL%2FYutzXowSLYQBNFCJcrEiF04aU0TdtHls9zynMiyeHhoVgCijOXWXNt9%2FCmpQ49RkNEujkv9NaJ0cum32MCVZKjE9%2BMKmLM%2F8ZygZeLBGJ7sg%3D%3D--QGIiU0%2FpXc3Aym8F--he2iRRPePOdtEs3z%2BufSXg%3D%3D; path=/; secure; HttpOnly +x-request-id: a0c0b8e7-cd60-4efa-b79b-cf1b0d5a0784 +x-runtime: 0.049566 +strict-transport-security: max-age=31536000; includeSubDomains; preload +x-frame-options: SAMEORIGIN +x-content-type-options: nosniff +referrer-policy: same-origin +content-security-policy: frame-ancestors 'none'; script-src 'self'; object-src 'self'; img-src * data: blob:; media-src 'self' data:; font-src 'self' data: https://fonts.gstatic.com/; connect-src 'self' blob: wss://mastodonten.de +x-xss-protection: 1; mode=block + diff --git a/tests/src/Network/CurlResultTest.php b/tests/src/Network/CurlResultTest.php index 79b950ca18..13a14d985e 100644 --- a/tests/src/Network/CurlResultTest.php +++ b/tests/src/Network/CurlResultTest.php @@ -9,22 +9,111 @@ class CurlResultTest extends TestCase { public function setUp() { - parent::setUp(); // TODO: Change the autogenerated stub + parent::setUp(); require_once __DIR__.'/../../../boot.php'; require_once __DIR__.'/../../../include/text.php'; } + /** + * @small + */ public function testNormal() { $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head'); $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body'); - $curlResult = new CurlResult('https://test.local', $header . $body, [ 'http_code' => 200 ]); + $curlResult = new CurlResult('https://test.local', $header . $body, [ + 'http_code' => 200, + 'content_type' => 'text/html; charset=utf-8', + 'url' => 'https://test.local' + ]); $this->assertTrue($curlResult->isSuccess()); + $this->assertFalse($curlResult->isTimeout()); + $this->assertFalse($curlResult->isRedirectUrl()); $this->assertSame($header, $curlResult->getHeader()); $this->assertSame($body, $curlResult->getBody()); + $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType()); + $this->assertSame('https://test.local', $curlResult->getUrl()); + $this->assertSame('https://test.local', $curlResult->getRedirectUrl()); } -} \ No newline at end of file + + /** + * @small + */ + public function testRedirect() + { + $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head'); + $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body'); + + + $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [ + 'http_code' => 301, + 'content_type' => 'text/html; charset=utf-8', + 'url' => 'https://test.local/test/it', + 'redirect_url' => 'https://test.other' + ]); + + $this->assertTrue($curlResult->isSuccess()); + $this->assertFalse($curlResult->isTimeout()); + $this->assertTrue($curlResult->isRedirectUrl()); + $this->assertSame($header, $curlResult->getHeader()); + $this->assertSame($body, $curlResult->getBody()); + $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType()); + $this->assertSame('https://test.local/test/it', $curlResult->getUrl()); + $this->assertSame('https://test.other/test/it', $curlResult->getRedirectUrl()); + } + + /** + * @small + */ + public function testTimeout() + { + $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head'); + $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body'); + + + $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [ + 'http_code' => 500, + 'content_type' => 'text/html; charset=utf-8', + 'url' => 'https://test.local/test/it', + 'redirect_url' => 'https://test.other' + ], CURLE_OPERATION_TIMEDOUT, 'Tested error'); + + $this->assertFalse($curlResult->isSuccess()); + $this->assertTrue($curlResult->isTimeout()); + $this->assertFalse($curlResult->isRedirectUrl()); + $this->assertSame($header, $curlResult->getHeader()); + $this->assertSame($body, $curlResult->getBody()); + $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType()); + $this->assertSame('https://test.local/test/it', $curlResult->getRedirectUrl()); + $this->assertSame('Tested error', $curlResult->getError()); + } + + /** + * @small + */ + public function testRedirectHeader() + { + $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.redirect'); + $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body'); + + + $curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [ + 'http_code' => 301, + 'content_type' => 'text/html; charset=utf-8', + 'url' => 'https://test.local/test/it?key=value', + ]); + + $this->assertTrue($curlResult->isSuccess()); + $this->assertFalse($curlResult->isTimeout()); + $this->assertTrue($curlResult->isRedirectUrl()); + $this->assertSame($header, $curlResult->getHeader()); + $this->assertSame($body, $curlResult->getBody()); + $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType()); + $this->assertSame('https://test.local/test/it?key=value', $curlResult->getUrl()); + $this->assertSame('https://test.other/some/?key=value', $curlResult->getRedirectUrl()); + } +} From 5f9065e00aaa47a0348c4f22ce588a01a75f0a18 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Thu, 11 Oct 2018 22:21:05 +0200 Subject: [PATCH 3/3] smaller about.body --- tests/datasets/curl/about.body | 172 --------------------------------- 1 file changed, 172 deletions(-) diff --git a/tests/datasets/curl/about.body b/tests/datasets/curl/about.body index b49bca8d97..86d8712d03 100644 --- a/tests/datasets/curl/about.body +++ b/tests/datasets/curl/about.body @@ -3,184 +3,12 @@ - - - - - - - -mastodonten.de - Mastodonten.de - - - - - - - - - - - - - - - - -
-
-
-
-
-Mastodon -
-
-
-
-
-Mastodon -
-Sign up on another server -
-Ich habe die öffentliche Registrierung geschlossen, nachdem sich etwas über 1000 Nutzer bei mastodonten.de angemeldet hatten. Mastodonten.de ist ein Freizeit-Projekt. Würde es weiter wachsen, wäre es in der Freizeit, ohne Beeinträchtigungen meiner Familie, wegen des höheren Administrations- und Moderationsaufwands nicht mehr zu stemmen. Daher dieser Schritt. Jeder, der Nutzer von mastodonten.de kennt, kann sich aber von diesen einladen lassen. Ansonsten gibt es auch viele andere nette Instanzen. -
-
- -I closed the public registration after a little over 1000 users had registered at mastodonten.de. Mastodonten.de is a leisure project. If it would grow further, it would be no more to stemmed in the spare time, without impairments of my family, because of the higher administration and moderation expenditure. Hence this step. Everybody who knows users of mastodonten.de can be invited by them. Otherwise there are also many other nice instances. -
-
-or -
-Log in - -
-
-
-
-Mastodonten.de -
-
-
-
- -

-Mastodonten.de -Mastodon hosted on mastodonten.de -

-
-

mastodonten.de ist eine Mastodon Instanz offen für alle netten Leute (über 16). -Geht gut miteinander um (und lest die Regeln wenn ihr euch nicht sicher seid, was das heißt). Wer möchte, kann auch gerne etwas spenden, um den Betrieb von mastodonten.de zu unterstützen. - -
-
-mastodonten.de is a Mastodon instance open to nice people (over 16). -Be excellent to each other (and read the rules if you're not sure what that means). Who would like, can donate something, in order to support the operation of mastodonten.de. -

-
-
-
-
-
-
-Home to -1,034 -users -
-
-Who authored -82,821 -statuses -
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-

What is Mastodon?

-

Mastodon is a social network based on open web protocols and free, open-source software. It is decentralized like e-mail.

-
- -
-
-
-
-
Built for real conversation
-With 500 characters at your disposal and support for granular content and media warnings, you can express yourself the way you want to. -
-
- -
-
-
-
-
You’re a person, not a product
-Mastodon is not a commercial network. No advertising, no data mining, no walled gardens. There is no central authority. -
-
- -
-
-
-
-
Always within reach
-Multiple apps for iOS, Android, and other platforms thanks to a developer-friendly API ecosystem allow you to keep up with your friends anywhere. -
-
- -
-
-
-
-
A more humane approach
-Learning from failures of other networks, Mastodon aims to make ethical design choices to combat the misuse of social media. -
-
- -
-
-
- - -
- -
-