-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace addContentLengthHeader with ContentLength Middleware #2254
Conversation
@akrabat
with this it works 😄 |
Or even better:
|
Ping @geggleto |
Slim/Middleware/ContentLength.php
Outdated
// Add Content-Length header if not already added | ||
$size = $response->getBody()->getSize(); | ||
if ($size !== null && !$response->hasHeader('Content-Length')) { | ||
$response = $response->withHeader('Content-Length', (string) $size); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.... why are we casting it to a string? ... my gut says that is a really bad idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
withHeader()
is defined as taking a string as the second parameter.
|
||
$newResponse = $mw($request, $response, $next); | ||
|
||
$this->assertEquals(4, $newResponse->getHeaderLine('Content-Length')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test should also assert that the body's content is actually 'Body'
@akrabat what about multi-byte character sets?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? The code under test doesn't touch the body and ensuring the body is the correct text is covered by tests on Response
and App
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Size of multi-byte character sets is an issue for Slim\Http\Stream()
and should be tested there I think?
i.e. I'm testing that the correct header is added as that's what this Middleware does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an argument that the test should be:
$this->assertEquals($response->getBody()->getSize(), $newResponse->getHeaderLine('Content-Length'));
or even:
$this->assertTrue($newResponse->hasHeader('Content-Length'));
Renamed to |
UPGRADING.md
Outdated
@@ -1,9 +1,11 @@ | |||
# How to upgrade | |||
|
|||
* [2254] - You need to add the `Middleware\ContentLength` middleware if you want Slim to add this automatically. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ContentLengthMiddleware
?
Maybe the OutputBuffering
should also be renamed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Thanks.
Yes, 'OutputBuffering' will need renaming too, but that's a separate PR if you fancy doing it?
This feature isn't needed in core and is better as optional middleware.
Rebased to fix conflicts |
This feature isn't needed in core and is better as optional middleware.