Skip to content

Commit

Permalink
Merge pull request #12 from ivastly/fix-content-type-handling
Browse files Browse the repository at this point in the history
Fix `Content-Type` header handling.
  • Loading branch information
vsouz4 committed Oct 16, 2020
2 parents 249173d + 3f31118 commit 0db0403
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [3.0.4] - 2020-10-16
### Fixed
- Fix `Content-type` header handling.

## [3.0.3] - 2020-10-15
### Fixed
- Serialize items from collection manually, since `json_encode` won't call `toArray` automatically as it used to do with `jsonSerialize`
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ The following environment variables are available:
| `CODE_STYLE` | no | {path-to-repository}/.php_cs.php | /client/myCodeStyle.php |
| `CLIENT_PHP_VERSION` | no | 7.0 | 7.0 |
| `COMPOSER_JSON_TEMPLATE_DIR` | no | {path-to-repository}/template/composer.json.twig | /path/composer.json.twig |
| `README_MD_TEMPLATE_DIR` | no | {path-to-repository}/template/README.md.twig | /path/README.md.twig |
| `README_MD_TEMPLATE_DIR` | no | {path-to-repository}/template/README.md.twig | /path/README.md.twig |


## Running tests

```bash
docker run -w /app -v $(pwd):/app php:7.4-cli-alpine php /app/vendor/bin/phpunit -c /app/phpunit.xml.dist --colors=always
```
1 change: 1 addition & 0 deletions src/Output/Copy/Response/ResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ResponseHandler
{
/** @var BodySerializer */
private $bodySerializer;

/** @var ResponseExceptionFactory */
private $responseExceptionFactory;

Expand Down
6 changes: 4 additions & 2 deletions src/Output/Copy/Serializer/BodySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ public function unserializeResponse(ResponseInterface $response): array

private function getContentTypeSerializer(string $contentType): ContentTypeSerializerInterface
{
$contentType = strtolower(trim(explode(';', $contentType)[0]));

if (!isset($this->contentTypeSerializers[$contentType])) {
throw new InvalidArgumentException(
\sprintf(
sprintf(
'Serializer for `%s` is not found. Supported: %s',
$contentType,
Json::encode(\array_keys($this->contentTypeSerializers))
Json::encode(array_keys($this->contentTypeSerializers))
)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

declare(strict_types=1);

namespace DoclerLabs\ApiClientGenerator\Test\Functional\Output\Copy\Serializer;

use DoclerLabs\ApiClientException\UnexpectedResponseException;
use DoclerLabs\ApiClientGenerator\Output\Copy\Serializer\BodySerializer;
use DoclerLabs\ApiClientGenerator\Output\Copy\Serializer\ContentType\FormUrlencodedContentTypeSerializer;
use DoclerLabs\ApiClientGenerator\Output\Copy\Serializer\ContentType\Json\Json;
use DoclerLabs\ApiClientGenerator\Output\Copy\Serializer\ContentType\JsonContentTypeSerializer;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Stream;
use PHPUnit\Framework\TestCase;

class BodySerializerTest extends TestCase
{
private BodySerializer $sut;

protected function setUp(): void
{
$this->sut = new BodySerializer();
$this->sut
->add('application/json', new JsonContentTypeSerializer())
->add('application/x-www-form-urlencoded', new FormUrlencodedContentTypeSerializer());
}

public function testUnserializeJsonResponseForEmptyBody(): void
{
$response = $this->getResponse('application/json', '');

self::assertEquals([], $this->sut->unserializeResponse($response));
}

public function testExceptionIsThrownForUnsupportedContentType(): void
{
$this->expectException(UnexpectedResponseException::class);
$response = $this->getResponse('video/h264', 'body content');

$this->sut->unserializeResponse($response);
}

public function testUnserializeFormUrlencodedResponse(): void
{
$response = $this->getResponse('application/x-www-form-urlencoded', 'key=value');

self::assertEquals(['key' => 'value'], $this->sut->unserializeResponse($response));
}

/**
* @dataProvider applicationJsonContentTypeStringsProvider
*/
public function testUnserializeJsonResponse(string $contentType): void
{
$body = ['key' => 'value'];
$encodedBody = Json::encode($body);

$response = $this->getResponse($contentType, $encodedBody);

self::assertEquals($body, $this->sut->unserializeResponse($response));
}

public function applicationJsonContentTypeStringsProvider(): array
{
return [
'default' => [
'application/json',
],
'with extra symbols' => [
'application/json; ',
],
'upper case' => [
'applicaTion/JSON',
],
'with charset' => [
'application/json; charset=ISO-8859-4',
],
];
}

private function getResponse(string $contentType, string $body): Response
{
/** @var Response $response */
$response = (new Response())->withHeader('Content-type', $contentType);

if ($body) {
$resource = fopen('php://memory', 'wb+');
$guzzleStream = new Stream($resource);
$guzzleStream->write($body);
$response = $response->withBody($guzzleStream);
}

return $response;
}
}

0 comments on commit 0db0403

Please sign in to comment.