From 4ac2840a67d74937863a645357d2cd5a1079a5f9 Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Thu, 16 Jan 2025 17:34:41 +0000 Subject: [PATCH 01/27] add PHPUnit test for unparse_url function --- tests/urls_test.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tests/urls_test.php diff --git a/tests/urls_test.php b/tests/urls_test.php new file mode 100644 index 0000000..ab08f59 --- /dev/null +++ b/tests/urls_test.php @@ -0,0 +1,30 @@ +assertEquals($url, $rebuild, "Failed for URL: $url"); + } + } + +} From 88cc08db4344dd3b83d0f57c3b8d98f32b4a17db Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Thu, 16 Jan 2025 17:40:35 +0000 Subject: [PATCH 02/27] add Urls.php in src --- src/Urls.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/Urls.php diff --git a/src/Urls.php b/src/Urls.php new file mode 100644 index 0000000..2bc473d --- /dev/null +++ b/src/Urls.php @@ -0,0 +1,30 @@ + 0 ? "$scheme:" : "") . + (\strlen($authority) > 0 ? "//$authority" : "") . + ($parsed['path'] ?? "") . + (\strlen($query) > 0 ? "?$query" : "") . + (\strlen($fragment) > 0 ? "#$fragment" : "") + ); + } + +} + From 5a59dc46b10be91961d33232acc47f75c0468238 Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Fri, 17 Jan 2025 17:00:26 +0000 Subject: [PATCH 03/27] Fix unparse_url to epmty string to null in line 13 and 14 --- src/Urls.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Urls.php b/src/Urls.php index 2bc473d..3ee684e 100644 --- a/src/Urls.php +++ b/src/Urls.php @@ -9,8 +9,8 @@ public static function unparse_url(array $parsed): string { $user = $parsed['user'] ?? null; $userinfo = $pass !== null ? "$user:$pass" : $user; $port = $parsed['port'] ?? 0; - $scheme = $parsed['scheme'] ?? ""; - $query = $parsed['query'] ?? ""; + $scheme = $parsed['scheme'] ?? null; + $query = $parsed['query'] ?? null; $fragment = $parsed['fragment'] ?? ""; $authority = ( ($userinfo !== null ? "$userinfo@" : "") . @@ -18,7 +18,6 @@ public static function unparse_url(array $parsed): string { ($port ? ":$port" : "") ); return ( - (\strlen($scheme) > 0 ? "$scheme:" : "") . (\strlen($authority) > 0 ? "//$authority" : "") . ($parsed['path'] ?? "") . (\strlen($query) > 0 ? "?$query" : "") . From 6873c7347699bbe72e5b895b924315e42c8d7251 Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Fri, 17 Jan 2025 17:51:41 +0000 Subject: [PATCH 04/27] Fix TypeError in unparse_url caused by null values from query, line 23 --- src/Urls.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Urls.php b/src/Urls.php index 3ee684e..7fa8dce 100644 --- a/src/Urls.php +++ b/src/Urls.php @@ -20,7 +20,7 @@ public static function unparse_url(array $parsed): string { return ( (\strlen($authority) > 0 ? "//$authority" : "") . ($parsed['path'] ?? "") . - (\strlen($query) > 0 ? "?$query" : "") . + (\strlen($query ?? '') > 0 ? "?$query" : "") . (\strlen($fragment) > 0 ? "#$fragment" : "") ); } From 99544ba7d0a974dd9c2c5b6acc2aeac1ff7e2550 Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Mon, 20 Jan 2025 12:30:44 +0000 Subject: [PATCH 05/27] Add validation to unparse_url function to ensure input is array --- src/Urls.php | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/Urls.php b/src/Urls.php index 7fa8dce..01a98fa 100644 --- a/src/Urls.php +++ b/src/Urls.php @@ -1,27 +1,37 @@ - 0 ? "//$authority" : "") . - ($parsed['path'] ?? "") . - (\strlen($query ?? '') > 0 ? "?$query" : "") . - (\strlen($fragment) > 0 ? "#$fragment" : "") + ($scheme !== null ? "$scheme://" : "") . + ($authority !== null ? "//$authority" : "") . + ($path !== null ?? "") . + ($query !== null ? "$query" : "") . + ($fragment !== null ? "#$fragment" : "") ); } From 1d567b60aa2da047770445753a52e6e2aa42d15c Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Tue, 21 Jan 2025 11:52:09 +0000 Subject: [PATCH 06/27] Create boilerplate PHPUnit test class for unparse_url --- src/Urls.php | 39 --------------------------------------- tests/urls_test.php | 37 ++++++++----------------------------- 2 files changed, 8 insertions(+), 68 deletions(-) delete mode 100644 src/Urls.php diff --git a/src/Urls.php b/src/Urls.php deleted file mode 100644 index 01a98fa..0000000 --- a/src/Urls.php +++ /dev/null @@ -1,39 +0,0 @@ -assertEquals($url, $rebuild, "Failed for URL: $url"); - } - } - -} +class UrlsTest extends \PHPUnit\Framework\TestCase +{ + public function test_unparse_url() + { + $this->asserEquals('Test case for unparse_url not implement yet'); + } +} \ No newline at end of file From 2fbdce0b1ced3a7430097d99303c74ddd2e1b777 Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Tue, 21 Jan 2025 12:10:52 +0000 Subject: [PATCH 07/27] Add test case for empty string --- tests/urls_test.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/urls_test.php b/tests/urls_test.php index 856fc14..66b31ec 100644 --- a/tests/urls_test.php +++ b/tests/urls_test.php @@ -4,6 +4,11 @@ class UrlsTest extends \PHPUnit\Framework\TestCase { public function test_unparse_url() { - $this->asserEquals('Test case for unparse_url not implement yet'); + $url = ''; + $parsed = parse_url($url); + $rebuild = Urls::unparse_url($parsed); + + + $this->assertEquals('Test case for unparse_url not implement yet'); } } \ No newline at end of file From d9290c2f634513e9f196035de43780c41dcab392 Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Tue, 21 Jan 2025 13:13:35 +0000 Subject: [PATCH 08/27] Implement basic unparse_url to handle empty string --- src/Urls.php | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/Urls.php diff --git a/src/Urls.php b/src/Urls.php new file mode 100644 index 0000000..38470de --- /dev/null +++ b/src/Urls.php @@ -0,0 +1,11 @@ + Date: Tue, 21 Jan 2025 13:16:50 +0000 Subject: [PATCH 09/27] Fix namespace mismatch in UrlsTest file --- tests/urls_test.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/urls_test.php b/tests/urls_test.php index 66b31ec..db0dfdf 100644 --- a/tests/urls_test.php +++ b/tests/urls_test.php @@ -1,6 +1,11 @@ assertEquals('Test case for unparse_url not implement yet'); + $this->assertEquals($url, 'Test case for unparse_url not implement yet'); } } \ No newline at end of file From cce4b0ff5ee82adbc2e45a2dab5c07f049264ad8 Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Tue, 21 Jan 2025 14:17:31 +0000 Subject: [PATCH 10/27] Fix class name --- tests/urls_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/urls_test.php b/tests/urls_test.php index db0dfdf..90ae403 100644 --- a/tests/urls_test.php +++ b/tests/urls_test.php @@ -5,7 +5,7 @@ use \PHPUnit\Framework\TestCase; use Missing\Urls; -class UrlsTest extends TestCase +class Urls_test extends TestCase { public function test_unparse_url() { From 823ae2c211481a550945bc48df20c4ee2aab925e Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Tue, 21 Jan 2025 14:49:24 +0000 Subject: [PATCH 11/27] Add parsing logic --- tests/urls_test.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/urls_test.php b/tests/urls_test.php index 90ae403..ac6f4b8 100644 --- a/tests/urls_test.php +++ b/tests/urls_test.php @@ -11,9 +11,10 @@ public function test_unparse_url() { $url = ''; $parsed = parse_url($url); - $rebuild = Urls::unparse_url($parsed); + $rebuildUrl = Urls::unparse_url($parsed); + $parsedRebuilt = parse_url($rebuildUrl); - $this->assertEquals($url, 'Test case for unparse_url not implement yet'); + $this->assertEquals($parsed, $parsedRebuilt, 'Failed to reconstruct empty string URL'); } } \ No newline at end of file From 877c16af801802e462ee386ed17334974cf4d36e Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Tue, 21 Jan 2025 18:02:14 +0000 Subject: [PATCH 12/27] Add test for simple string --- tests/urls_test.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/urls_test.php b/tests/urls_test.php index ac6f4b8..afc8610 100644 --- a/tests/urls_test.php +++ b/tests/urls_test.php @@ -17,4 +17,15 @@ public function test_unparse_url() $this->assertEquals($parsed, $parsedRebuilt, 'Failed to reconstruct empty string URL'); } + + public function testUnparseUrlString() + { + $url = 'foo'; + $parsed = parse_url($url); + $rebuildUrl = Urls::unparse_url($parsed); + $parsedRebuilt = parse_url($rebuildUrl); + + + $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct simple string URL: '{$url}'"); + } } \ No newline at end of file From db2e81c11ab268dd2954a33d8565f1c4c5f1f75c Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Wed, 22 Jan 2025 10:17:35 +0000 Subject: [PATCH 13/27] Add test for simple URL --- tests/urls_test.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/urls_test.php b/tests/urls_test.php index afc8610..e2b2136 100644 --- a/tests/urls_test.php +++ b/tests/urls_test.php @@ -28,4 +28,15 @@ public function testUnparseUrlString() $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct simple string URL: '{$url}'"); } + + public function testUnparseFullUrl() + { + $url = 'http://www.google.com/'; + $parsed = parse_url($url); + $rebuildUrl = Urls::unparse_url($parsed); + $parsedRebuilt = parse_url($rebuildUrl); + + + $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct full URL: '{$url}'"); + } } \ No newline at end of file From bbe7b80a473d2aa16619ecb749fb14be23acacce Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Wed, 22 Jan 2025 10:19:21 +0000 Subject: [PATCH 14/27] Add test for full URL --- tests/urls_test.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/urls_test.php b/tests/urls_test.php index e2b2136..0f2c212 100644 --- a/tests/urls_test.php +++ b/tests/urls_test.php @@ -29,7 +29,7 @@ public function testUnparseUrlString() $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct simple string URL: '{$url}'"); } - public function testUnparseFullUrl() + public function testUnparseSimpleUrl() { $url = 'http://www.google.com/'; $parsed = parse_url($url); @@ -37,6 +37,17 @@ public function testUnparseFullUrl() $parsedRebuilt = parse_url($rebuildUrl); + $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct simple URL: '{$url}'"); + } + + public function testUnparseFullUrl() + { + $url = 'http://u:p@foo:1/path/path?q#frag'; + $parsed = parse_url($url); + $rebuildUrl = Urls::unparse_url($parsed); + $parsedRebuilt = parse_url($rebuildUrl); + + $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct full URL: '{$url}'"); } } \ No newline at end of file From b7f93de58a7df470b1ff7ac9250d4fad9ba0f383 Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Wed, 22 Jan 2025 10:22:17 +0000 Subject: [PATCH 15/27] Add test for incomplete URL --- tests/urls_test.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/urls_test.php b/tests/urls_test.php index 0f2c212..3f8d163 100644 --- a/tests/urls_test.php +++ b/tests/urls_test.php @@ -50,4 +50,15 @@ public function testUnparseFullUrl() $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct full URL: '{$url}'"); } + + public function testUnparseUrlIncomplete() + { + $url = 'http://u:p@foo:1/path/path?#'; + $parsed = parse_url($url); + $rebuildUrl = Urls::unparse_url($parsed); + $parsedRebuilt = parse_url($rebuildUrl); + + + $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct incomplete URL: '{$url}'"); + } } \ No newline at end of file From 800ac307e7d64583e84fe06d749a7d581e9783af Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Wed, 22 Jan 2025 10:23:45 +0000 Subject: [PATCH 16/27] Add test for SSH URL --- tests/urls_test.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/urls_test.php b/tests/urls_test.php index 3f8d163..de8ea20 100644 --- a/tests/urls_test.php +++ b/tests/urls_test.php @@ -61,4 +61,15 @@ public function testUnparseUrlIncomplete() $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct incomplete URL: '{$url}'"); } + + public function testUnparseUrlSsh() + { + $url = 'ssh://root@host'; + $parsed = parse_url($url); + $rebuildUrl = Urls::unparse_url($parsed); + $parsedRebuilt = parse_url($rebuildUrl); + + + $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct SSH URL: '{$url}'"); + } } \ No newline at end of file From f9d297b3a8cd090d11d856d640052018cb09771c Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Wed, 22 Jan 2025 10:25:09 +0000 Subject: [PATCH 17/27] Add test for malformed URL --- tests/urls_test.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/urls_test.php b/tests/urls_test.php index de8ea20..ade1be1 100644 --- a/tests/urls_test.php +++ b/tests/urls_test.php @@ -72,4 +72,15 @@ public function testUnparseUrlSsh() $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct SSH URL: '{$url}'"); } + + public function testUnparseMalformedUrl() + { + $url = '://:@:1/?#'; + $parsed = parse_url($url); + $rebuildUrl = Urls::unparse_url($parsed); + $parsedRebuilt = parse_url($rebuildUrl); + + + $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct malformed URL: '{$url}'"); + } } \ No newline at end of file From 6635c8d173551bc39f64b30dc201161ccb24e810 Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Wed, 22 Jan 2025 10:28:17 +0000 Subject: [PATCH 18/27] Add test for Url with empty user and password --- tests/urls_test.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/urls_test.php b/tests/urls_test.php index ade1be1..fedc881 100644 --- a/tests/urls_test.php +++ b/tests/urls_test.php @@ -83,4 +83,15 @@ public function testUnparseMalformedUrl() $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct malformed URL: '{$url}'"); } + + public function testUnparseUrlWithEmptyUserAndPass() + { + $url = 'http://:@foo:1/path/path?#'; + $parsed = parse_url($url); + $rebuildUrl = Urls::unparse_url($parsed); + $parsedRebuilt = parse_url($rebuildUrl); + + + $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct URL with empty user and password: '{$url}'"); + } } \ No newline at end of file From 8a930f1918085e0308bcfa01332639de32b94c84 Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Wed, 22 Jan 2025 10:29:31 +0000 Subject: [PATCH 19/27] Add test for Url with empty user --- tests/urls_test.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/urls_test.php b/tests/urls_test.php index fedc881..df196fc 100644 --- a/tests/urls_test.php +++ b/tests/urls_test.php @@ -94,4 +94,16 @@ public function testUnparseUrlWithEmptyUserAndPass() $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct URL with empty user and password: '{$url}'"); } + + + public function testUnparseUrlWithEmptyUser() + { + $url = 'http://:@foo:1/path/path?#'; + $parsed = parse_url($url); + $rebuildUrl = Urls::unparse_url($parsed); + $parsedRebuilt = parse_url($rebuildUrl); + + + $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct URL with empty user: '{$url}'"); + } } \ No newline at end of file From ebd8d5b540d57c0d421ec5d46b3ad13376761dd5 Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Wed, 22 Jan 2025 10:54:08 +0000 Subject: [PATCH 20/27] Implement unparse_url to handle path only URL --- src/Urls.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Urls.php b/src/Urls.php index 38470de..f7ee78a 100644 --- a/src/Urls.php +++ b/src/Urls.php @@ -6,6 +6,10 @@ class Urls { public static function unparse_url(array $parsed): string { - return ''; + // $scheme = isset($parsed['scheme']) ? $parsed['scheme'] . "://" : ""; + // $host = isset($parsed['host']) ? $parsed['host'] : ""; + + return $parsed['path'] ?? ''; + } } \ No newline at end of file From 3f1d7afabee07e8d3245143406824c00e9752aea Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Wed, 22 Jan 2025 11:54:40 +0000 Subject: [PATCH 21/27] Implement unparse_url to handle path only URL --- src/Urls.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Urls.php b/src/Urls.php index f7ee78a..647fc62 100644 --- a/src/Urls.php +++ b/src/Urls.php @@ -8,8 +8,9 @@ public static function unparse_url(array $parsed): string { // $scheme = isset($parsed['scheme']) ? $parsed['scheme'] . "://" : ""; // $host = isset($parsed['host']) ? $parsed['host'] : ""; + $path = $parsed['path'] ?? ''; - return $parsed['path'] ?? ''; + return $path; } } \ No newline at end of file From c7170f6e9661067c68df3508c0e6e4c38ea6bf2f Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Wed, 22 Jan 2025 12:00:23 +0000 Subject: [PATCH 22/27] Add scheme and host to unparse_url --- src/Urls.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Urls.php b/src/Urls.php index 647fc62..18cd5db 100644 --- a/src/Urls.php +++ b/src/Urls.php @@ -6,11 +6,11 @@ class Urls { public static function unparse_url(array $parsed): string { - // $scheme = isset($parsed['scheme']) ? $parsed['scheme'] . "://" : ""; - // $host = isset($parsed['host']) ? $parsed['host'] : ""; + $scheme = isset($parsed['scheme']) ? $parsed['scheme'] . "://" : ""; + $host = isset($parsed['host']) ? $parsed['host'] : ""; $path = $parsed['path'] ?? ''; - return $path; + return "$scheme$host$path"; } } \ No newline at end of file From fd3dc26d18794f1b1c1371e66ca86e5bc4ba57bd Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Wed, 22 Jan 2025 12:10:20 +0000 Subject: [PATCH 23/27] Add test for URL with host --- tests/urls_test.php | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/tests/urls_test.php b/tests/urls_test.php index df196fc..4c72358 100644 --- a/tests/urls_test.php +++ b/tests/urls_test.php @@ -7,16 +7,16 @@ class Urls_test extends TestCase { - public function test_unparse_url() - { - $url = ''; - $parsed = parse_url($url); - $rebuildUrl = Urls::unparse_url($parsed); - $parsedRebuilt = parse_url($rebuildUrl); + // public function test_unparse_url() + // { + // $url = ''; + // $parsed = parse_url($url); + // $rebuildUrl = Urls::unparse_url($parsed); + // $parsedRebuilt = parse_url($rebuildUrl); - $this->assertEquals($parsed, $parsedRebuilt, 'Failed to reconstruct empty string URL'); - } + // $this->assertEquals($parsed, $parsedRebuilt, 'Failed to reconstruct empty string URL'); + // } public function testUnparseUrlString() { @@ -40,6 +40,17 @@ public function testUnparseSimpleUrl() $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct simple URL: '{$url}'"); } + public function testUnparseHostUrl() + { + $url = 'http://www.google.com:8080/'; + $parsed = parse_url($url); + $rebuildUrl = Urls::unparse_url($parsed); + $parsedRebuilt = parse_url($rebuildUrl); + + + $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct URL host and port: '{$url}'"); + } + public function testUnparseFullUrl() { $url = 'http://u:p@foo:1/path/path?q#frag'; From a1cbaa1681480b7183885125492f8cf56d9b9dee Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Wed, 22 Jan 2025 12:12:57 +0000 Subject: [PATCH 24/27] Add port reconstruction --- src/Urls.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Urls.php b/src/Urls.php index 18cd5db..e4a20a6 100644 --- a/src/Urls.php +++ b/src/Urls.php @@ -9,8 +9,9 @@ public static function unparse_url(array $parsed): string $scheme = isset($parsed['scheme']) ? $parsed['scheme'] . "://" : ""; $host = isset($parsed['host']) ? $parsed['host'] : ""; $path = $parsed['path'] ?? ''; + $port = isset($parsed['port']) ? ':' . $parsed['port'] : ''; - return "$scheme$host$path"; + return "$scheme$host$port$path"; } } \ No newline at end of file From 23f7d92e4ecd533254c215aa3c96286b5baad329 Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Wed, 22 Jan 2025 12:31:44 +0000 Subject: [PATCH 25/27] Add user and pass to URL --- src/Urls.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Urls.php b/src/Urls.php index e4a20a6..e264e6e 100644 --- a/src/Urls.php +++ b/src/Urls.php @@ -10,8 +10,14 @@ public static function unparse_url(array $parsed): string $host = isset($parsed['host']) ? $parsed['host'] : ""; $path = $parsed['path'] ?? ''; $port = isset($parsed['port']) ? ':' . $parsed['port'] : ''; + $user = $parsed['user'] ?? ''; + $pass = isset($parsed['pass']) ? ':' . $parsed['pass'] : ''; + $auth = ($user || $pass) ? "$user$pass@" : ''; - return "$scheme$host$port$path"; + // $query = isset($parsed['query']) ? "?" . $parsed['query'] : ""; + // $fragment = isset($parsed['fragment']) ? "#" . $parsed['fragment'] : ""; + + return "$scheme$auth$host$port$path"; } } \ No newline at end of file From 90fe1d5e9037fb6c0519453f25742760ec4d8e29 Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Wed, 22 Jan 2025 12:34:45 +0000 Subject: [PATCH 26/27] Add query and fragment to reconstruction URL --- src/Urls.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Urls.php b/src/Urls.php index e264e6e..acb8d4b 100644 --- a/src/Urls.php +++ b/src/Urls.php @@ -13,11 +13,10 @@ public static function unparse_url(array $parsed): string $user = $parsed['user'] ?? ''; $pass = isset($parsed['pass']) ? ':' . $parsed['pass'] : ''; $auth = ($user || $pass) ? "$user$pass@" : ''; + $query = isset($parsed['query']) ? "?" . $parsed['query'] : ""; + $fragment = isset($parsed['fragment']) ? "#" . $parsed['fragment'] : ""; - // $query = isset($parsed['query']) ? "?" . $parsed['query'] : ""; - // $fragment = isset($parsed['fragment']) ? "#" . $parsed['fragment'] : ""; - - return "$scheme$auth$host$port$path"; + return "$scheme$auth$host$port$path$query$fragment"; } } \ No newline at end of file From 92f936f361de0a83a78c96d42fd6e1097e877cd4 Mon Sep 17 00:00:00 2001 From: Marika Bertelli Date: Wed, 22 Jan 2025 12:53:46 +0000 Subject: [PATCH 27/27] Refactor PHPUnit test class for unparse_url --- tests/urls_test.php | 131 ++++++++------------------------------------ 1 file changed, 22 insertions(+), 109 deletions(-) diff --git a/tests/urls_test.php b/tests/urls_test.php index 4c72358..fb8f8e2 100644 --- a/tests/urls_test.php +++ b/tests/urls_test.php @@ -7,114 +7,27 @@ class Urls_test extends TestCase { - // public function test_unparse_url() - // { - // $url = ''; - // $parsed = parse_url($url); - // $rebuildUrl = Urls::unparse_url($parsed); - // $parsedRebuilt = parse_url($rebuildUrl); - - - // $this->assertEquals($parsed, $parsedRebuilt, 'Failed to reconstruct empty string URL'); - // } - - public function testUnparseUrlString() - { - $url = 'foo'; - $parsed = parse_url($url); - $rebuildUrl = Urls::unparse_url($parsed); - $parsedRebuilt = parse_url($rebuildUrl); - - - $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct simple string URL: '{$url}'"); - } - - public function testUnparseSimpleUrl() - { - $url = 'http://www.google.com/'; - $parsed = parse_url($url); - $rebuildUrl = Urls::unparse_url($parsed); - $parsedRebuilt = parse_url($rebuildUrl); - - - $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct simple URL: '{$url}'"); - } - - public function testUnparseHostUrl() - { - $url = 'http://www.google.com:8080/'; - $parsed = parse_url($url); - $rebuildUrl = Urls::unparse_url($parsed); - $parsedRebuilt = parse_url($rebuildUrl); - - - $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct URL host and port: '{$url}'"); - } - - public function testUnparseFullUrl() - { - $url = 'http://u:p@foo:1/path/path?q#frag'; - $parsed = parse_url($url); - $rebuildUrl = Urls::unparse_url($parsed); - $parsedRebuilt = parse_url($rebuildUrl); - - - $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct full URL: '{$url}'"); - } - - public function testUnparseUrlIncomplete() - { - $url = 'http://u:p@foo:1/path/path?#'; - $parsed = parse_url($url); - $rebuildUrl = Urls::unparse_url($parsed); - $parsedRebuilt = parse_url($rebuildUrl); - - - $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct incomplete URL: '{$url}'"); - } - - public function testUnparseUrlSsh() - { - $url = 'ssh://root@host'; - $parsed = parse_url($url); - $rebuildUrl = Urls::unparse_url($parsed); - $parsedRebuilt = parse_url($rebuildUrl); - - - $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct SSH URL: '{$url}'"); - } - - public function testUnparseMalformedUrl() - { - $url = '://:@:1/?#'; - $parsed = parse_url($url); - $rebuildUrl = Urls::unparse_url($parsed); - $parsedRebuilt = parse_url($rebuildUrl); - - - $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct malformed URL: '{$url}'"); - } - - public function testUnparseUrlWithEmptyUserAndPass() - { - $url = 'http://:@foo:1/path/path?#'; - $parsed = parse_url($url); - $rebuildUrl = Urls::unparse_url($parsed); - $parsedRebuilt = parse_url($rebuildUrl); - - - $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct URL with empty user and password: '{$url}'"); - } - - - public function testUnparseUrlWithEmptyUser() - { - $url = 'http://:@foo:1/path/path?#'; - $parsed = parse_url($url); - $rebuildUrl = Urls::unparse_url($parsed); - $parsedRebuilt = parse_url($rebuildUrl); - - - $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct URL with empty user: '{$url}'"); + public function test_unparse_url() + { + $urls = [ + '', + 'foo', + 'http://www.google.com/', + 'http://www.google.com:8080/', + 'http://u:p@foo:1/path/path?q#frag', + 'http://u:p@foo:1/path/path?#', + 'ssh://root@host', + '://:@:1/?#', + 'http://:@foo:1/path/path?#' + ]; + + foreach ($urls as $url) { + $parsed = parse_url($url); + $rebuildUrl = Urls::unparse_url($parsed); + $parsedRebuilt = parse_url($rebuildUrl); + + $this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct URL: '{$url}'"); + + } } } \ No newline at end of file