Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
Merge pull request #35 from jasny/reparse-on-null
Browse files Browse the repository at this point in the history
Setting parsedBody to null, means that the body will be (re-)parsed
  • Loading branch information
jasny authored Jan 26, 2017
2 parents 20b5df3 + 4527b44 commit 4a2160a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 19 deletions.
36 changes: 24 additions & 12 deletions src/ServerRequest/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ trait Headers
*/
abstract public function getServerParams();


/**
* Turn a server parameter key to a header name
*
* @param string $key
* @return string|null
*/
protected function serverParamKeyToHeaderName($key)
{
$name = null;

if (\Jasny\str_starts_with($key, 'HTTP_')) {
$name = $this->headerCase(substr($key, 5));
} elseif (in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH'])) {
$name = $this->headerCase($key);
}

return $name;
}

/**
* Determine the headers based on the server parameters
Expand All @@ -30,19 +49,12 @@ protected function determineHeaders()
$params = $this->getServerParams();
$headers = [];

foreach ($params as $param => $value) {
if (!is_string($value)) {
continue;
}
if (\Jasny\str_starts_with($param, 'HTTP_')) {
$key = $this->headerCase(substr($param, 5));
} elseif (in_array($param, ['CONTENT_TYPE', 'CONTENT_LENGTH'])) {
$key = $this->headerCase($param);
} else {
continue;
}
foreach ($params as $key => $value) {
$name = $this->serverParamKeyToHeaderName($key);

$headers[$key] = [$value];
if (isset($name) && is_string($value)) {
$headers[$name] = [$value];
}
}

return $headers;
Expand Down
5 changes: 3 additions & 2 deletions src/ServerRequest/ParsedBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ trait ParsedBody

/**
* The condition under which the body was parsed
* @var array|null
* @var array|false|null
*/
protected $parseCondition;

Expand Down Expand Up @@ -232,6 +232,7 @@ public function getParsedBody()

/**
* Return an instance with the specified body parameters.
* Setting the parsed body to `null` means that the body will be (re-)parsed on `getParsedBody()`.
*
* @param null|array|object|mixed $data The deserialized body data.
* @return static
Expand All @@ -240,7 +241,7 @@ public function withParsedBody($data)
{
$request = $this->copy();

$request->parseCondition = false;
$request->parseCondition = ($data === null ? null : false);

if ($this->shouldUsePostData()) {
$request->postData = $data;
Expand Down
11 changes: 8 additions & 3 deletions tests/LegacyCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,16 @@ public function setUp()
'cookie' => $_COOKIE,
'server' => $_SERVER
];

ob_start();
}

public function tearDown()
{
ob_end_clean();
}


/**
* Initialize the request
*
Expand Down Expand Up @@ -137,8 +145,6 @@ protected function assertCleanedEnvironment()
*/
public function test()
{
ob_start();

// Create server request with (actual) global enviroment.
$request = (new ServerRequest())->withGlobalEnvironment(true);

Expand Down Expand Up @@ -176,6 +182,5 @@ public function test()
$this->assertEquals('http://example.com/foo/1', $finalResponse->getHeaderLine('Location'));
$this->assertStringStartsWith('text/plain', $finalResponse->getHeaderLine('Content-Type'));
$this->assertEquals("Hello world", (string)$finalResponse->getBody());

}
}
2 changes: 0 additions & 2 deletions tests/Response/ProtocolVersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public function protocolVersionProvider()
*/
public function testWithProtocolVersion($version, $expect)
{
$this->status->expects($this->never())->method('withProtocolVersion');

$response = $this->baseResponse->withProtocolVersion($version);

$this->assertInstanceof(Response::class, $response);
Expand Down
18 changes: 18 additions & 0 deletions tests/ServerRequest/ParsedBodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ public function testWithParsedBody()

public function testWithParsedBodyNoReset()
{
$this->setContentType('application/json');

$body = $this->createMock(Stream::class);
$body->expects($this->never())
->method('__toString');
Expand All @@ -254,6 +256,22 @@ public function testWithParsedBodyNoReset()
$this->assertEquals(['foo' => 'bar'], $request->getParsedBody());
}

public function testWithParsedBodySetNull()
{
$this->setContentType('application/json');

$body = $this->createMock(Stream::class);
$body->expects($this->once())
->method('__toString')
->willReturn('{"foo": "bar"}');

$request = $this->baseRequest->withBody($body)->withParsedBody(['foo' => 'qux']);
$this->assertEquals(['foo' => 'qux'], $request->getParsedBody());

$nextRequest = $request->withParsedBody(null);
$this->assertEquals(['foo' => 'bar'], $nextRequest->getParsedBody());
}

public function testWithParsedBodyTurnStale()
{
$refl = new \ReflectionProperty(ServerRequest::class, 'isStale');
Expand Down

0 comments on commit 4a2160a

Please sign in to comment.