Skip to content

Commit

Permalink
Fix Content-Length header
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Mar 22, 2017
1 parent 8da2fbe commit f8f1974
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions src/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions tests/EncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -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());
}

Expand All @@ -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());
}
}

0 comments on commit f8f1974

Please sign in to comment.