Skip to content

Commit

Permalink
Merge pull request #220 from bearsunday/spike
Browse files Browse the repository at this point in the history
Fix receiving json encoded data with POST method #219
  • Loading branch information
koriym committed Aug 17, 2016
2 parents 7c5a3be + f36d01c commit a96e7ac
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Bootstrap.php
Expand Up @@ -117,7 +117,7 @@ private function getCache(AbstractAppMeta $appMeta, $contexts)
{
$isProd = is_int(strpos($contexts, 'prod'));
if ($isProd) {
if(function_exists('apcu_fetch')){
if (function_exists('apcu_fetch')) {
return new ApcuCache;
}

Expand Down
48 changes: 26 additions & 22 deletions src/Provide/Router/HttpMethodParams.php
Expand Up @@ -61,57 +61,61 @@ public function get(array $server, array $get, array $post)
*/
private function unsafeMethod($method, array $server, array $post)
{
// must be a POST to do an override
$override = $this->getOverRideMethod($server, $post);
if (is_string($override)) {
// must be a POST to do an override
return [$override, $post];
}
$params = $this->getParams($method, $server, $post);

if ($method === 'post') {
return ['post', $post];
list($method, $params) = $this->getOverrideMethod($method, $server, $params);
}
// put / patch /delete
return [$method, $this->getParams($method, $post, $server)];

return [$method, $params];
}

/**
* HTTP Method override
*
* @param array $server
* @param array $post
* @param string $method
* @param array $server
* @param array $params
*
* @return bool|string
* @return array
*/
private function getOverRideMethod(array $server, array &$post)
private function getOverrideMethod($method, array $server, array $params)
{
// must be a POST to do an override

// look for override in post data
if (isset($post['_method'])) {
$method = strtolower($post['_method']);
unset($post['_method']);
if (isset($params['_method'])) {
$method = strtolower($params['_method']);
unset($params['_method']);

return $method;
return [$method, $params];
}

// look for override in headers
if (isset($server['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
return strtolower($server['HTTP_X_HTTP_METHOD_OVERRIDE']);
$method = strtolower($server['HTTP_X_HTTP_METHOD_OVERRIDE']);
}

return false;
return [$method, $params];
}

/**
* Return request parameters
*
* @param string $method
* @param array $post
* @param array $server
* @param array $post
*
* @return array
*/
private function getParams($method, array $post, array $server)
private function getParams($method, array $server, array $post)
{
if ($method === 'put' || $method === 'patch' || $method === 'delete') {
// post data exists
if ($method === 'post' && ! empty($post)) {
return $post;
}

if ($method === 'post' || $method === 'put' || $method === 'patch' || $method === 'delete') {
return $this->phpInput($server);
}

Expand Down
57 changes: 53 additions & 4 deletions tests/Provide/Router/HttpMethodParamsTest.php
Expand Up @@ -113,7 +113,56 @@ public function testOverrideHeaderDelete()
$this->assertSame('delete', $method);
}

public function testContentTypeJson()
public function testPostContentTypeJson()
{
$httpMethodParam = new HttpMethodParams;
$httpMethodParam->setStdIn(__DIR__ . '/json.txt');
$server = [
'REQUEST_METHOD' => 'POST',
'HTTP_CONTENT_TYPE' => 'application/json'
];
list(, $params) = $httpMethodParam->get($server, [], []);
$expected = ['name' => 'BEAR.Sunday v1.0', 'age' => 0];
$this->assertSame($expected, $params);
}

public function testPostContentTypeJsonAssocArray()
{
$httpMethodParam = new HttpMethodParams;
$httpMethodParam->setStdIn(__DIR__ . '/json_assoc.txt');
$server = [
'REQUEST_METHOD' => 'POST',
'HTTP_CONTENT_TYPE' => 'application/json'
];
list(, $params) = $httpMethodParam->get($server, [], []);
$expected = ['franeworks' => [['name' => 'BEAR.Sunday v1.0', 'age' => 0], ['name' => 'zend', 'age' => 9]]];
$this->assertSame($expected, $params);
}

public function testPostContentTypeUnknown()
{
$httpMethodParam = new HttpMethodParams;
$server = [
'REQUEST_METHOD' => 'POST',
'HTTP_CONTENT_TYPE' => 'text/xml'
];
list(, $params) = $httpMethodParam->get($server, [], []);
$expected = [];
$this->assertSame($expected, $params);
}

public function testPostNoContentType()
{
$httpMethodParam = new HttpMethodParams;
$server = [
'REQUEST_METHOD' => 'POST',
];
list(, $params) = $httpMethodParam->get($server, [], []);
$expected = [];
$this->assertSame($expected, $params);
}

public function testPutContentTypeJson()
{
$httpMethodParam = new HttpMethodParams;
$httpMethodParam->setStdIn(__DIR__ . '/json.txt');
Expand All @@ -126,7 +175,7 @@ public function testContentTypeJson()
$this->assertSame($expected, $params);
}

public function testContentTypeJsonAssocArray()
public function testPutContentTypeJsonAssocArray()
{
$httpMethodParam = new HttpMethodParams;
$httpMethodParam->setStdIn(__DIR__ . '/json_assoc.txt');
Expand All @@ -139,7 +188,7 @@ public function testContentTypeJsonAssocArray()
$this->assertSame($expected, $params);
}

public function testContentTypeUnknown()
public function testPutContentTypeUnknown()
{
$httpMethodParam = new HttpMethodParams;
$server = [
Expand All @@ -151,7 +200,7 @@ public function testContentTypeUnknown()
$this->assertSame($expected, $params);
}

public function testNoContentType()
public function testPutNoContentType()
{
$httpMethodParam = new HttpMethodParams;
$server = [
Expand Down

0 comments on commit a96e7ac

Please sign in to comment.