From 60927c30b163ef5f858c5b841d8c35bf4246ef3a Mon Sep 17 00:00:00 2001 From: Matheus Marques Date: Thu, 10 Mar 2016 11:49:26 -0300 Subject: [PATCH 1/2] Created Request::reparseBody --- Slim/Http/Request.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Slim/Http/Request.php b/Slim/Http/Request.php index d6c77f9a2..67585d61d 100644 --- a/Slim/Http/Request.php +++ b/Slim/Http/Request.php @@ -1024,6 +1024,20 @@ public function withParsedBody($data) return $clone; } + /** + * Force Body to be parsed again. + * + * Note: This method is not part of the PSR-7 standard. + * + * @return self + */ + public function reparseBody() + { + $this->bodyParsed = false; + + return $this; + } + /** * Register media type parser. * From 5c72d630117246ecaf710a0fbfe9923c0b658b16 Mon Sep 17 00:00:00 2001 From: Matheus Marques Date: Thu, 10 Mar 2016 11:49:40 -0300 Subject: [PATCH 2/2] Unit test --- tests/Http/RequestTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Http/RequestTest.php b/tests/Http/RequestTest.php index ec27eb2d3..82c250caf 100644 --- a/tests/Http/RequestTest.php +++ b/tests/Http/RequestTest.php @@ -772,6 +772,30 @@ public function testGetParsedBodyWhenBodyDoesNotExist() $this->assertNull($request->getParsedBody()); } + public function testGetParsedBodyAfterCallReparseBody() + { + $uri = Uri::createFromString('https://example.com:443/?one=1'); + $headers = new Headers([ + 'Content-Type' => 'application/x-www-form-urlencoded;charset=utf8', + ]); + $cookies = []; + $serverParams = []; + $body = new RequestBody(); + $body->write('foo=bar'); + $body->rewind(); + $request = new Request('POST', $uri, $headers, $cookies, $serverParams, $body); + + $this->assertEquals(['foo' => 'bar'], $request->getParsedBody()); + + $newBody = new RequestBody(); + $newBody->write('abc=123'); + $newBody->rewind(); + $request = $request->withBody($newBody); + $request->reparseBody(); + + $this->assertEquals(['abc' => '123'], $request->getParsedBody()); + } + /** * @expectedException \RuntimeException */