Skip to content

Commit

Permalink
Combine qs on either request or rule
Browse files Browse the repository at this point in the history
  • Loading branch information
midwestE authored and midwestE committed Jul 1, 2020
1 parent 3f111f7 commit 685ea2b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/RedirectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,19 +290,26 @@ protected function mergeRuleIntoUri(RedirectRule $rule, RedirectUri $uri): Redir
$uri = $uri
->withPath($destination->getPath())
->withStatusCode($rule->getHttpStatus());
if (!empty($uri->getQuery())) {

// default is combined querystring with rule overridding request
if (!empty($uri->getQuery()) || !empty($destination->getQuery())) {
parse_str($destination->getQuery(), $destinationQs);
parse_str($uri->getQuery(), $sourceQs);
$combinedQs = \array_replace_recursive($sourceQs, $destinationQs);
ksort($combinedQs);
$uri = $uri->withQuery(\http_build_query($combinedQs));
}
if (!empty($uri->getFragment())) {
$uri = $uri->withFragment($uri->getFragment());

// rule fragment overrides request fragment
if (!empty($destination->getFragment())) {
$uri = $uri->withFragment($destination->getFragment());
}

// rule user info overrides request user info
if (!empty($uri->getUserInfo())) {
$uri = $uri->withUserInfo($uri->getUserInfo());
}

return $uri;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,21 @@ public function testTrailingSlash()
$this->assertEquals($result->location, 'https://localhost/root/?new=querystring&query=string');
}

public function testOnlyDestinationHasQs()
{
$rule = [
"id" => "1",
"source" => "/",
"type" => "path",
"destination" => "/root/?key=newvalue",
"httpStatus" => 302,
"active" => 1
];
$result = $this->slimRedirect('https://localhost', [$rule]);
$this->assertEquals($rule['httpStatus'], $result->responseStatus);
$this->assertEquals($result->location, 'https://localhost/root/?key=newvalue');
}

public function testCombinedQsOverwrite()
{
$rule = [
Expand Down

0 comments on commit 685ea2b

Please sign in to comment.