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

Commit

Permalink
Fixing issue 109 - Allow requests with empty body (#114)
Browse files Browse the repository at this point in the history
Support OPTIONS method
  • Loading branch information
jefferson-lima authored and martin-georgiev committed May 1, 2018
1 parent a41f47b commit 4d7a777
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/Exception/EmptyBodyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace Raml\Exception;

class EmptyBodyException extends \Exception
{
public function __construct($message = "", $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
11 changes: 8 additions & 3 deletions src/Method.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Raml;

use Raml\Exception\EmptyBodyException;

/**
* Method
*
Expand All @@ -10,10 +12,9 @@ class Method implements ArrayInstantiationInterface, MessageSchemaInterface
{
/**
* Valid METHODS
* - Currently missing OPTIONS as this is unlikely to be specified in RAML
* @var array
*/
public static $validMethods = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'PATCH'];
public static $validMethods = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'];

// ---

Expand Down Expand Up @@ -381,8 +382,12 @@ public function addQueryParameter(NamedParameter $queryParameter)
*/
public function getBodyByType($type)
{
if (empty($this->getBodies())){
throw new EmptyBodyException();
}

if (!isset($this->bodyList[$type])) {
throw new \Exception('No body of type "' . $type . '"');
throw new \Exception("No body of type '$type'");
}

return $this->bodyList[$type];
Expand Down
6 changes: 5 additions & 1 deletion src/Validator/ValidatorSchemaHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Raml\ApiDefinition;
use Raml\Body;
use Raml\Exception\BadParameter\ResourceNotFoundException;
use Raml\Exception\EmptyBodyException;
use Raml\MessageSchemaInterface;
use Raml\Method;
use Raml\NamedParameter;
Expand Down Expand Up @@ -142,20 +143,23 @@ public function getResponseHeaders($method, $path, $statusCode, $requiredOnly =
* @param string $path
* @param string $contentType
* @return Body
* @throws EmptyBodyException
* @throws ValidatorSchemaException
*/
private function getBody(MessageSchemaInterface $schema, $method, $path, $contentType)
{
try {
$body = $schema->getBodyByType($contentType);
} catch (EmptyBodyException $e) {
throw new EmptyBodyException($e->getMessage());
} catch (Exception $exception) {
$message = sprintf(
'Schema for %s %s with content type %s was not found in API definition',
strtoupper($method),
$path,
$contentType
);

throw new ValidatorSchemaException($message, 0, $exception);
}

Expand Down
3 changes: 3 additions & 0 deletions test/MethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public function shouldGetTheTypeInUpperCase()

$method = \Raml\Method::createFromArray('Post', [], $apiDefinition);
$this->assertSame('POST', $method->getType());

$method = \Raml\Method::createFromArray('options', [], $apiDefinition);
$this->assertSame('OPTIONS', $method->getType());
}

/** @test */
Expand Down
16 changes: 16 additions & 0 deletions test/Validator/RequestValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@ public function shouldCatchInvalidBody()
$validator->validateRequest($this->request);
}

/** @test */
public function shouldAllowEmptyRequestBody()
{
$body = $this->getMock('\Psr\Http\Message\StreamInterface');
$body->method('getContents')->willReturn('');

$this->request->method('getMethod')->willReturn('get');
$this->uri->method('getPath')->willReturn('/songs');
$this->request->method('getHeaderLine')->with('Content-Type')->willReturn('application/json');
$this->request->method('getBody')->willReturn($body);

$validator = $this->getValidatorForSchema(__DIR__ . '/../fixture/validator/requestBody.raml');
$validator->validateRequest($this->request);

}

/**
* @param string $fixturePath
* @return RequestValidator
Expand Down
5 changes: 5 additions & 0 deletions test/fixture/validator/requestBody.raml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ title: Example API
version: v1

/songs:

get:
responses:
200:

post:
body:
application/json:
Expand Down

0 comments on commit 4d7a777

Please sign in to comment.