Skip to content

Commit

Permalink
Fix parse PUT request
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzychu committed Oct 28, 2014
1 parent 8dbc696 commit ebc85ea
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 3 deletions.
37 changes: 35 additions & 2 deletions src/Ouzo/Uri.php
@@ -1,6 +1,8 @@
<?php
namespace Ouzo;

use Ouzo\Api\InternalException;
use Ouzo\ExceptionHandling\Error;
use Ouzo\Uri\PathProvider;
use Ouzo\Utilities\Arrays;
use Ouzo\Utilities\Json;
Expand Down Expand Up @@ -126,12 +128,43 @@ public static function getRequestParameters($stream = 'php://input')

private static function _parseRequest($content)
{
if (Json::isJson($content)) {
return Json::decode($content, true);
$jsonParameters = self::_jsonParameters($content);
if ($jsonParameters) {
return $jsonParameters;
}

$putParameters = self::_putRequestParameters($content);
if ($putParameters) {
return $putParameters;
}

return array();
}

private static function _jsonParameters($content)
{
if (Strings::equalsIgnoreCase(ContentType::value(), 'application/json')) {
$json = Json::decode($content, true);
if (Json::lastError() !== 0) {
throw new InternalException(new Error(0, 'JSON string is malformed'));
}
return Arrays::toArray($json);
}
return false;
}

private static function _putRequestParameters($content)
{
if (
Strings::equal(Arrays::getValue($_SERVER, 'REQUEST_METHOD'), 'PUT')
&& Strings::equalsIgnoreCase(ContentType::value(), 'application/x-www-form-urlencoded')
) {
parse_str($content, $parameters);
return Arrays::toArray($parameters);
}
return false;
}

public static function addPrefixIfNeeded($url)
{
$prefix = Config::getValue('global', 'prefix_system');
Expand Down
83 changes: 82 additions & 1 deletion test/src/Ouzo/UriTest.php
@@ -1,6 +1,8 @@
<?php
use Ouzo\Config;
use Ouzo\ContentType;
use Ouzo\Tests\ArrayAssert;
use Ouzo\Tests\CatchException;
use Ouzo\Tests\StreamStub;
use Ouzo\Uri;

Expand Down Expand Up @@ -206,6 +208,42 @@ public function shouldParseUrlWhenSlashInGET()
$this->assertEquals($paramsExpected, $params);
}

/**
* @test
*/
public function shouldParsePutRequestInStream()
{
//given
$_SERVER['REQUEST_METHOD'] = 'PUT';
StreamStub::register('put');
StreamStub::$body = 'a=test2&t=test3';
ContentType::set('application/x-www-form-urlencoded');

//when
$parameters = $this->getRequestParameters('put://input');

//then
ArrayAssert::that($parameters)
->hasSize(2)
->contains('test2')
->contains('test3');
StreamStub::unregister();
}

public function shouldCorrectParseStream()
{
//given
StreamStub::register('uri');
StreamStub::$body = 'album%5Bdigital_photos%5D=false&name=john&phones%5B%5D=123&phones%5B%5D=456&phones%5B%5D=789&colors%5Bfloor%5D=red&colors%5Bdoors%5D=blue';

//when
$parameters = Uri::getRequestParameters('uri://input');

//then
ArrayAssert::that($parameters)->hasSize(4);
StreamStub::unregister();
}

/**
* @test
*/
Expand All @@ -214,15 +252,58 @@ public function shouldCorrectParseJsonInStream()
//given
StreamStub::register('json');
StreamStub::$body = '{"name":"jonh","id":123,"ip":"127.0.0.1"}';
ContentType::set('application/json');

//when
$parameters = Uri::getRequestParameters('json://input');
$parameters = $this->getRequestParameters('json://input');

//then
ArrayAssert::that($parameters)->hasSize(3);
StreamStub::unregister();
}

function getRequestParameters($stream)
{
return Uri::getRequestParameters($stream);
}


/**
* @test
*/
public function shouldAcceptEmptyJson()
{
//given
StreamStub::register('json');
StreamStub::$body = '';
ContentType::set('application/json');

//when
$parameters = $this->getRequestParameters('json://input');

//then
ArrayAssert::that($parameters)->hasSize(0);
StreamStub::unregister();
}

/**
* @test
*/
public function shouldFailForInvalidJson()
{
//given
StreamStub::register('json');
StreamStub::$body = '{"name":"jonh","id":123,"ip":"127.0.0.1}';
ContentType::set('application/json');

//when
CatchException::when($this)->getRequestParameters('json://input');

//then
CatchException::assertThat()->isInstanceOf('Ouzo\Api\InternalException');
StreamStub::unregister();
}

/**
* @test
* @dataProvider malformedSlashes
Expand Down

0 comments on commit ebc85ea

Please sign in to comment.