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

Commit

Permalink
#154 Handle Content-Type header
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-georgiev committed Aug 3, 2019
1 parent 3f464e1 commit 715bd91
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Validator/RequestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private function assertValidBody(RequestInterface $request)
{
$method = $request->getMethod();
$path = $request->getUri()->getPath();
$contentType = $request->getHeaderLine('Content-Type');
$contentType = \explode(';', $request->getHeaderLine('Content-Type'))[0];

$schemaBody = $this->schemaHelper->getRequestBody($method, $path, $contentType);

Expand Down
2 changes: 1 addition & 1 deletion src/Validator/ResponseValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private function assertValidBody(RequestInterface $request, ResponseInterface $r
$method = $request->getMethod();
$path = $request->getUri()->getPath();
$statusCode = $response->getStatusCode();
$contentType = $response->getHeaderLine('Content-Type');
$contentType = \explode(';', $response->getHeaderLine('Content-Type'))[0];

$schemaBody = $this->schemaHelper->getResponseBody($method, $path, $statusCode, $contentType);

Expand Down
37 changes: 37 additions & 0 deletions tests/Validator/RequestValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use Raml\Body;
use Raml\Parser;
use Raml\Validator\RequestValidator;
use Raml\Validator\ValidatorRequestException;
use Raml\Validator\ValidatorSchemaHelper;
use Raml\ValidatorInterface;

class RequestValidatorTest extends TestCase
{
Expand Down Expand Up @@ -159,6 +161,41 @@ public function shouldAllowEmptyRequestBody()
$validator->validateRequest($this->request);
}

/**
* @test
* @doesNotPerformAssertions
*/
public function shouldParseContentTypeHeader()
{
$body = $this->createMock(StreamInterface::class);
$body->method('getContents')->willReturn('{"title":"Aaa"}');

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

$schemaBody = $this->createMock(Body::class);
$schemaBody
->expects($this->atLeastOnce())
->method('getValidator')
->willReturn($this->createMock(ValidatorInterface::class));

$apiDefinition = $this->parser->parse(__DIR__ . '/../fixture/validator/requestBody.raml');
$schemaHelper = $this->getMockBuilder(ValidatorSchemaHelper::class)
->setConstructorArgs([$apiDefinition])
->setMethods(['getRequestBody'])
->getMock();
$schemaHelper
->expects($this->once())
->method('getRequestBody')
->with('post', '/songs', 'application/json')
->willReturn($schemaBody);

$validator = new RequestValidator($schemaHelper, new Negotiator());
$validator->validateRequest($this->request);
}

/**
* @param string $fixturePath
* @return RequestValidator
Expand Down
52 changes: 52 additions & 0 deletions tests/Validator/ResponseValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use Raml\Body;
use Raml\Parser;
use Raml\Validator\ResponseValidator;
use Raml\Validator\ValidatorResponseException;
use Raml\Validator\ValidatorSchemaHelper;
use Raml\ValidatorInterface;

class ResponseValidatorTest extends TestCase
{
Expand Down Expand Up @@ -169,4 +171,54 @@ public function shouldCatchInvalidBody()
$validator = $this->getValidatorForSchema(__DIR__ . '/../fixture/validator/responseBody.raml');
$validator->validateResponse($this->request, $this->response);
}

/**
* @test
* @doesNotPerformAssertions
*/
public function shouldParseContentTypeHeader()
{
$json = '{}';

$headers = [
'X-Required-Header' => ['123456'],
'X-Long-Optional-Header' => ['Abcdefghijkl'],
];

$map = [
['X-Required-Header', [['123456']]],
['X-Long-Optional-Header', [['Abcdefg', 'Abc']]],
];

$body = $this->createMock(StreamInterface::class);
$body->method('getContents')->willReturn($json);

$this->request->method('getMethod')->willReturn('post');
$this->uri->method('getPath')->willReturn('/songs');
$this->response->method('getStatusCode')->willReturn(200);
$this->response->method('getHeader')->willReturnMap($map);
$this->response->method('getHeaders')->willReturn($headers);
$this->response->method('getHeaderLine')->with('Content-Type')->willReturn('application/json; charset=us-ascii');
$this->response->method('getBody')->willReturn($body);

$schemaBody = $this->createMock(Body::class);
$schemaBody
->expects($this->atLeastOnce())
->method('getValidator')
->willReturn($this->createMock(ValidatorInterface::class));

$apiDefinition = $this->parser->parse(__DIR__ . '/../fixture/validator/responseBody.raml');
$schemaHelper = $this->getMockBuilder(ValidatorSchemaHelper::class)
->setConstructorArgs([$apiDefinition])
->setMethods(['getResponseBody'])
->getMock();
$schemaHelper
->expects($this->once())
->method('getResponseBody')
->with('post', '/songs', 200, 'application/json')
->willReturn($schemaBody);

$validator = new ResponseValidator($schemaHelper);
$validator->validateResponse($this->request, $this->response);
}
}

0 comments on commit 715bd91

Please sign in to comment.