Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/96-empty-string'
Browse files Browse the repository at this point in the history
Close #97
Fixes #96
  • Loading branch information
weierophinney committed Nov 15, 2017
2 parents 4bf815b + 8f3cccf commit aae8115
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 1.3.2 - TBD
## 1.3.2 - 2017-11-15

### Added

Expand All @@ -22,7 +22,9 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#97](https://github.com/zfcampus/zf-content-negotiation/pull/97) fixes an
issue in the `ContentTypeListener` whereby empty content was leading to an
uninitialized string offset notice .

## 1.3.1 - 2017-11-14

Expand Down
2 changes: 1 addition & 1 deletion src/ContentTypeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function __invoke(MvcEvent $e)
}

// Try to assume JSON if content starts like JSON and no explicit Content-Type was provided
if (! $bodyParams && in_array($content[0], ['{', '['], true)) {
if (! $bodyParams && strlen($content) > 0 && in_array($content[0], ['{', '['], true)) {
$bodyParams = $this->decodeJson($content);
}

Expand Down
60 changes: 60 additions & 0 deletions test/ContentTypeListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -647,4 +647,64 @@ public function testStringContentIsParsedCorrectlyToAnArray($method, $data, $key
$this->assertArrayHasKey($key, $array);
$this->assertSame('', $array[$key]);
}

public function nonPostMethodsContent()
{
$dataSets = [
'object' => [
'data' => '{"key": "value"}',
'expected' => ['key' => 'value'],
],
'array' => [
'data' => '["first", "second"]',
'expected' => ['first', 'second'],
],
/** @see https://github.com/zfcampus/zf-content-negotiation/pull/96 */
'empty' => [
'data' => '',
'expected' => [],
],
];

foreach (['PUT', 'PATCH', 'DELETE'] as $method) {
foreach ($dataSets as $type => $set) {
$name = sprintf('%s-%s', $type, $method);
yield $name => [$method, $set['data'], $set['expected']];
}
}
}

/**
* @dataProvider nonPostMethodsContent
*
* @see https://github.com/zfcampus/zf-content-negotiation/pull/94
* @see https://github.com/zfcampus/zf-content-negotiation/pull/96
* @param string $method HTTP method
* @param string $data HTTP body content
* @param mixed $expected Expected body params
*/
public function testMissingContentTypeHeaderResultsInParsingAsJsonIfInitialCharacterIndicatesObjectOrArray(
$method,
$data,
$expected
) {
$listener = $this->listener;

$request = new Request();
$request->setMethod($method);
$request->setContent($data);

$event = new MvcEvent();
$event->setRequest($request);
$event->setRouteMatch($this->createRouteMatch([]));

$result = $listener($event);
$this->assertNull($result);

/** @var \ZF\ContentNegotiation\ParameterDataContainer $params */
$params = $event->getParam('ZFContentNegotiationParameterData');
$test = $params->getBodyParams();

$this->assertSame($expected, $test);
}
}

0 comments on commit aae8115

Please sign in to comment.