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

Commit 1e8183c

Browse files
#154 Handle Content-Type header (#155)
1 parent 3f464e1 commit 1e8183c

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

src/Validator/RequestValidator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private function assertValidBody(RequestInterface $request)
103103
{
104104
$method = $request->getMethod();
105105
$path = $request->getUri()->getPath();
106-
$contentType = $request->getHeaderLine('Content-Type');
106+
$contentType = \explode(';', $request->getHeaderLine('Content-Type'))[0];
107107

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

src/Validator/ResponseValidator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private function assertValidBody(RequestInterface $request, ResponseInterface $r
9999
$method = $request->getMethod();
100100
$path = $request->getUri()->getPath();
101101
$statusCode = $response->getStatusCode();
102-
$contentType = $response->getHeaderLine('Content-Type');
102+
$contentType = \explode(';', $response->getHeaderLine('Content-Type'))[0];
103103

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

tests/Validator/RequestValidatorTest.php

+37
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
use Psr\Http\Message\RequestInterface;
88
use Psr\Http\Message\StreamInterface;
99
use Psr\Http\Message\UriInterface;
10+
use Raml\Body;
1011
use Raml\Parser;
1112
use Raml\Validator\RequestValidator;
1213
use Raml\Validator\ValidatorRequestException;
1314
use Raml\Validator\ValidatorSchemaHelper;
15+
use Raml\ValidatorInterface;
1416

1517
class RequestValidatorTest extends TestCase
1618
{
@@ -159,6 +161,41 @@ public function shouldAllowEmptyRequestBody()
159161
$validator->validateRequest($this->request);
160162
}
161163

164+
/**
165+
* @test
166+
* @doesNotPerformAssertions
167+
*/
168+
public function shouldParseContentTypeHeader()
169+
{
170+
$body = $this->createMock(StreamInterface::class);
171+
$body->method('getContents')->willReturn('{"title":"Aaa"}');
172+
173+
$this->request->method('getMethod')->willReturn('post');
174+
$this->uri->method('getPath')->willReturn('/songs');
175+
$this->request->method('getHeaderLine')->with('Content-Type')->willReturn('application/json; charset=us-ascii');
176+
$this->request->method('getBody')->willReturn($body);
177+
178+
$schemaBody = $this->createMock(Body::class);
179+
$schemaBody
180+
->expects($this->atLeastOnce())
181+
->method('getValidator')
182+
->willReturn($this->createMock(ValidatorInterface::class));
183+
184+
$apiDefinition = $this->parser->parse(__DIR__ . '/../fixture/validator/requestBody.raml');
185+
$schemaHelper = $this->getMockBuilder(ValidatorSchemaHelper::class)
186+
->setConstructorArgs([$apiDefinition])
187+
->setMethods(['getRequestBody'])
188+
->getMock();
189+
$schemaHelper
190+
->expects($this->once())
191+
->method('getRequestBody')
192+
->with('post', '/songs', 'application/json')
193+
->willReturn($schemaBody);
194+
195+
$validator = new RequestValidator($schemaHelper, new Negotiator());
196+
$validator->validateRequest($this->request);
197+
}
198+
162199
/**
163200
* @param string $fixturePath
164201
* @return RequestValidator

tests/Validator/ResponseValidatorTest.php

+52
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
use Psr\Http\Message\ResponseInterface;
88
use Psr\Http\Message\StreamInterface;
99
use Psr\Http\Message\UriInterface;
10+
use Raml\Body;
1011
use Raml\Parser;
1112
use Raml\Validator\ResponseValidator;
1213
use Raml\Validator\ValidatorResponseException;
1314
use Raml\Validator\ValidatorSchemaHelper;
15+
use Raml\ValidatorInterface;
1416

1517
class ResponseValidatorTest extends TestCase
1618
{
@@ -169,4 +171,54 @@ public function shouldCatchInvalidBody()
169171
$validator = $this->getValidatorForSchema(__DIR__ . '/../fixture/validator/responseBody.raml');
170172
$validator->validateResponse($this->request, $this->response);
171173
}
174+
175+
/**
176+
* @test
177+
* @doesNotPerformAssertions
178+
*/
179+
public function shouldParseContentTypeHeader()
180+
{
181+
$json = '{}';
182+
183+
$headers = [
184+
'X-Required-Header' => ['123456'],
185+
'X-Long-Optional-Header' => ['Abcdefghijkl'],
186+
];
187+
188+
$map = [
189+
['X-Required-Header', [['123456']]],
190+
['X-Long-Optional-Header', [['Abcdefg', 'Abc']]],
191+
];
192+
193+
$body = $this->createMock(StreamInterface::class);
194+
$body->method('getContents')->willReturn($json);
195+
196+
$this->request->method('getMethod')->willReturn('post');
197+
$this->uri->method('getPath')->willReturn('/songs');
198+
$this->response->method('getStatusCode')->willReturn(200);
199+
$this->response->method('getHeader')->willReturnMap($map);
200+
$this->response->method('getHeaders')->willReturn($headers);
201+
$this->response->method('getHeaderLine')->with('Content-Type')->willReturn('application/json; charset=us-ascii');
202+
$this->response->method('getBody')->willReturn($body);
203+
204+
$schemaBody = $this->createMock(Body::class);
205+
$schemaBody
206+
->expects($this->atLeastOnce())
207+
->method('getValidator')
208+
->willReturn($this->createMock(ValidatorInterface::class));
209+
210+
$apiDefinition = $this->parser->parse(__DIR__ . '/../fixture/validator/responseBody.raml');
211+
$schemaHelper = $this->getMockBuilder(ValidatorSchemaHelper::class)
212+
->setConstructorArgs([$apiDefinition])
213+
->setMethods(['getResponseBody'])
214+
->getMock();
215+
$schemaHelper
216+
->expects($this->once())
217+
->method('getResponseBody')
218+
->with('post', '/songs', 200, 'application/json')
219+
->willReturn($schemaBody);
220+
221+
$validator = new ResponseValidator($schemaHelper);
222+
$validator->validateResponse($this->request, $this->response);
223+
}
172224
}

0 commit comments

Comments
 (0)