Skip to content

Commit

Permalink
Fix Utils::modifyRequest dropping ServerRequest attributes (#400)
Browse files Browse the repository at this point in the history
Fixes #344

Co-Authored-By: Imad Bazzal <12641890+ImadBazzal@users.noreply.github.com>

Co-authored-by: Imad Bazzal <12641890+ImadBazzal@users.noreply.github.com>
  • Loading branch information
GrahamCampbell and ImadBazzal committed Mar 21, 2021
1 parent 72dde17 commit aa50bd7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public static function modifyRequest(RequestInterface $request, array $changes)
}

if ($request instanceof ServerRequestInterface) {
return (new ServerRequest(
$new = (new ServerRequest(
isset($changes['method']) ? $changes['method'] : $request->getMethod(),
$uri,
$headers,
Expand All @@ -217,6 +217,12 @@ public static function modifyRequest(RequestInterface $request, array $changes)
->withQueryParams($request->getQueryParams())
->withCookieParams($request->getCookieParams())
->withUploadedFiles($request->getUploadedFiles());

foreach ($request->getAttributes() as $key => $value) {
$new = $new->withAttribute($key, $value);
}

return $new;
}

return new Request(
Expand Down
11 changes: 11 additions & 0 deletions tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,15 @@ public function testModifyServerRequestQueryParams()

self::assertSame(['name' => 'value'], $modifiedRequest->getQueryParams());
}

public function testModifyServerRequestRetainsAttributes()
{
$request = (new Psr7\ServerRequest('GET', 'http://example.com/bla'))
->withAttribute('foo', 'bar');

/** @var Psr7\ServerRequest $modifiedRequest */
$modifiedRequest = Psr7\Utils::modifyRequest($request, []);

self::assertSame(['foo' => 'bar'], $modifiedRequest->getAttributes());
}
}

0 comments on commit aa50bd7

Please sign in to comment.