Skip to content

Commit

Permalink
Merge pull request #63 from aaemnnosttv/feature/trailing-slash-compat
Browse files Browse the repository at this point in the history
Add support for magic URLs with trailing slashes
  • Loading branch information
aaemnnosttv authored Oct 28, 2022
2 parents 98566b2 + 1d36e62 commit 7bbbd7f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
16 changes: 11 additions & 5 deletions plugin/wp-cli-login-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Author URI: https://aaemnnost.tv
* Plugin URI: https://aaemnnost.tv/wp-cli-commands/login/
*
* Version: 1.3
* Version: 1.4
*/

namespace WP_CLI_Login;
Expand Down Expand Up @@ -83,10 +83,16 @@ public function __construct($endpoint, $publicKey)
*/
public static function parseUri($uri)
{
return array_slice(
array_merge(['',''], explode('/', $uri)),
-2
);
$uri = trim($uri, '/');
$segments = explode('/', $uri);

// If there aren't at least 2 segments,
// return empty values to always return an array with the same length.
if (count($segments) < 2) {
return ['', ''];
}

return array_slice($segments, -2);
}

/**
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/WP_CLI_Login_ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ function parses_endpoint_and_key_from_uri()
$this->assertSame('key', $public);
}

/** @test */
function parses_endpoint_and_key_from_uri_with_trailing_slash()
{
list($endpoint, $public) = WP_CLI_Login_Server::parseUri('/end/key/');

$this->assertSame('end', $endpoint);
$this->assertSame('key', $public);
}

/** @test */
function parses_endpoint_and_key_from_uri_for_subdirectory_site()
{
Expand All @@ -23,4 +32,28 @@ function parses_endpoint_and_key_from_uri_for_subdirectory_site()
$this->assertSame('key', $public);
}

/** @test */
function parses_endpoint_and_key_from_uri_with_trailing_slash_for_subdirectory_site()
{
list($endpoint, $public) = WP_CLI_Login_Server::parseUri('/abc/end/key/');

$this->assertSame('end', $endpoint);
$this->assertSame('key', $public);
}

/** @test */
function parses_an_endpoint_with_a_single_segment() {
list($endpoint, $public) = WP_CLI_Login_Server::parseUri('/abc');

$this->assertSame('', $endpoint);
$this->assertSame('', $public);
}

/** @test */
function parses_an_endpoint_with_no_segment() {
list($endpoint, $public) = WP_CLI_Login_Server::parseUri('/');

$this->assertSame('', $endpoint);
$this->assertSame('', $public);
}
}

0 comments on commit 7bbbd7f

Please sign in to comment.