From f8f197447738d554d1b598ed9a1beaed335e5b12 Mon Sep 17 00:00:00 2001 From: oscarotero Date: Wed, 22 Mar 2017 21:00:34 +0100 Subject: [PATCH] Fix Content-Length header --- CHANGELOG.md | 10 ++++++++++ composer.json | 2 +- src/Encoder.php | 6 ++++++ tests/EncoderTest.php | 6 +++--- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b51cfb7..d6e7af7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 0.3.1 - 2017-03-22 + +### Fixed + +* Fixed `Content-Length` response + ## 0.3.0 - 2016-12-26 ### Changed @@ -21,3 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## 0.1.0 - 2016-10-11 First version + +[0.3.1]: https://github.com/middlewares/encoder/compare/v0.3.0...v0.3.1 +[0.3.0]: https://github.com/middlewares/encoder/compare/v0.2.0...v0.3.0 +[0.2.0]: https://github.com/middlewares/encoder/compare/v0.1.0...v0.2.0 diff --git a/composer.json b/composer.json index c9ca312..e01a539 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "require": { "php": "^5.6 || ^7.0", "http-interop/http-middleware": "^0.4", - "middlewares/utils": "~0.8" + "middlewares/utils": "~0.10" }, "require-dev": { "phpunit/phpunit": "^5.5", diff --git a/src/Encoder.php b/src/Encoder.php index 70cc103..be9076b 100644 --- a/src/Encoder.php +++ b/src/Encoder.php @@ -31,6 +31,12 @@ public function process(ServerRequestInterface $request, DelegateInterface $dele $stream = Utils\Factory::createStream(); $stream->write($this->encode((string) $response->getBody())); + if ($stream->getSize() !== null) { + $response = $response->withHeader('Content-Length', (string) $stream->getSize()); + } else { + $response = $response->withoutHeader('Content-Length'); + } + return $response ->withHeader('Content-Encoding', $this->encoding) ->withBody($stream); diff --git a/tests/EncoderTest.php b/tests/EncoderTest.php index 61270da..0ecfdc8 100644 --- a/tests/EncoderTest.php +++ b/tests/EncoderTest.php @@ -21,8 +21,8 @@ function () { }, ], $request); - $this->assertInstanceOf('Psr\\Http\\Message\\ResponseInterface', $response); $this->assertEquals('gzip', $response->getHeaderLine('Content-Encoding')); + $this->assertEquals($response->getBody()->getSize(), $response->getHeaderLine('Content-Length')); $this->assertEquals(gzencode('Hello world'), (string) $response->getBody()); } @@ -38,8 +38,8 @@ function () { }, ], $request); - $this->assertInstanceOf('Psr\\Http\\Message\\ResponseInterface', $response); $this->assertEquals('deflate', $response->getHeaderLine('Content-Encoding')); + $this->assertEquals($response->getBody()->getSize(), $response->getHeaderLine('Content-Length')); $this->assertEquals(gzdeflate('Hello world'), (string) $response->getBody()); } @@ -55,8 +55,8 @@ function () { }, ], $request); - $this->assertInstanceOf('Psr\\Http\\Message\\ResponseInterface', $response); $this->assertFalse($response->hasHeader('Content-Encoding')); + $this->assertFalse($response->hasHeader('Content-Length')); $this->assertEquals('Hello world', (string) $response->getBody()); } }