From 6c5b5e6d5d3bdcdc4b266dc7cade0e8985fdfdc3 Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Tue, 11 Dec 2018 19:15:47 +0100 Subject: [PATCH] AddPathPlugin > Trim ending slash on path --- CHANGELOG.md | 1 + spec/Plugin/AddPathPluginSpec.php | 18 +++++++++++++++--- src/Plugin/AddPathPlugin.php | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a32a18d..c9bb8ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Changed - When multiple cookies exist, a single header with all cookies is sent as per RFC 6265 Section 5.4 +- AddPathPlugin will now trim of ending slashes in paths ## 1.8.1 - 2018-10-09 diff --git a/spec/Plugin/AddPathPluginSpec.php b/spec/Plugin/AddPathPluginSpec.php index a7ec9b5..6838ed8 100644 --- a/spec/Plugin/AddPathPluginSpec.php +++ b/spec/Plugin/AddPathPluginSpec.php @@ -46,12 +46,24 @@ function it_adds_path( $this->handleRequest($request, function () {}, function () {}); } - function it_throws_exception_on_trailing_slash(UriInterface $host) - { + function it_removes_ending_slashes( + RequestInterface $request, + UriInterface $host, + UriInterface $host2, + UriInterface $uri + ) { $host->getPath()->shouldBeCalled()->willReturn('/api/'); + $host2->getPath()->shouldBeCalled()->willReturn('/api'); + $host->withPath('/api')->shouldBeCalled()->willReturn($host2); + + $request->getUri()->shouldBeCalled()->willReturn($uri); + $request->withUri($uri)->shouldBeCalled()->willReturn($request); + + $uri->withPath('/api/users')->shouldBeCalled()->willReturn($uri); + $uri->getPath()->shouldBeCalled()->willReturn('/users'); $this->beConstructedWith($host); - $this->shouldThrow('\LogicException')->duringInstantiation(); + $this->handleRequest($request, function () {}, function () {}); } function it_throws_exception_on_empty_path(UriInterface $host) diff --git a/src/Plugin/AddPathPlugin.php b/src/Plugin/AddPathPlugin.php index 93ded1c..0a1bca2 100644 --- a/src/Plugin/AddPathPlugin.php +++ b/src/Plugin/AddPathPlugin.php @@ -35,7 +35,7 @@ public function __construct(UriInterface $uri) } if ('/' === substr($uri->getPath(), -1)) { - throw new \LogicException('URI path cannot end with a slash.'); + $uri = $uri->withPath(rtrim($uri->getPath(), '/')); } $this->uri = $uri;