Skip to content

Commit

Permalink
Merge branch 'mathmarques-request-reparseBody' into 3.x
Browse files Browse the repository at this point in the history
Closes #1811
Fixes #1803
  • Loading branch information
akrabat committed Mar 10, 2016
2 parents bfc3543 + 5c72d63 commit 939f2e8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Slim/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 939f2e8

Please sign in to comment.